Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you use the "KeyDown" event, and what's the opposite of it?

How do you use KeyDown, and what's the opposite event of it, like when you don't press it?

1 answer

✓ Accepted answer

KeyDown fires the moment a key begins being pressed. KeyUp fires the moment a key stops being pressed (is released).

To keep track of what keys are held at any given time, you could record that a key is down when it's pressed, and then record it's up when released. You just check the record for that particular key to know whether or not it's down (the most recent key event was down) at that moment.

local keyboard = {};
function keyup(key)
	keyboard[key:upper()] = false;
end

function keydown(key)
	keyboard[key:upper()] = true;
end

--[[
 Connection of keyup/keydown
--]]

--[[
 Whatever code else going on, then:
--]]

if keyboard["A"] then
	-- Currently pressing A
else
	-- NOT currently holding A
end

This discussion is preserved as it was. New answers, comments, and votes are closed.