Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by 10 years ago

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

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
Ad

Answer this question