2DWorlds Forums

Full Version: Easy key events in tools
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is some code to make key controls easier to work with in a tool.
This will be placed in a script inside a Tool
[lua]local keys={}
function iskeydown(k)
for i,v in pairs(keys) do
if v==k then return true end
end
return false
end
function keydown(k)
if not iskeydown(k) then
table.insert(keys,k)
end
end
function keyup(k)
if iskeydown(k) then
local ind=0
for i,v in pairs(keys) do
if v==k then ind=i end
end
table.remove(keys,ind)
end
end
link(script.Parent.KeyDown,keydown)
link(script.Parent.KeyUp,keyup)[/lua]

With this code you ca later say
[lua]if iskeydown("KEY HERE") then[/lua]
To see if that key is currently being pressed.
Cool, thanks a lot.