post this here too
a function that will let you check if any alphanumeric key was pressed - i even added an option to allow you to specify if it's case sensitive or not
(i used the g_InKey as it's more user friendly than trying to memorise / workout which scan code you need so the actual end use function is way nicer but scancode will allow you to target the entire keyboard so it's probably best to have a function for both)
edit: new version that will detect correctly from multiple scripts
function GetKeyPressed(e, key, ignorecase)
key = key or ""
ignorecase = ignorecase or false
lastpressed = lastpressed or {}
e = e or 0
lastpressed[e] = lastpressed[e] or {}
local inkey = g_InKey
if ignorecase then
key = string.lower(key)
inkey = string.lower(g_InKey)
end
local waspressed
if inkey == key and lastpressed[e] ~= g_InKey then
waspressed = g_InKey
else
waspressed = "false"
end
lastpressed[e] = g_InKey
return waspressed
end
example usages
(shows the key presses locally and counts the number of times w or W is pressed)
local wcount = 0
function keypresscheck2_init(e)
end
function keypresscheck2_main(e)
local key = GetKeyPressed(e, g_InKey)
if key == g_InKey then
PromptLocal(e,"pressed : "..key)
end
if string.lower(key) == "w" then
wcount = wcount + 1
end
TextCenterOnX(50,10,3,"you pressed the 'W' key "..wcount.." times so far")
end
(shows the key presses locally and counts the number of times R (not r) is pressed)
local rcount = 0
function keypresscheck_init(e)
end
function keypresscheck_main(e)
local key = GetKeyPressed(e, g_InKey)
if key == g_InKey then
PromptLocal(e,"pressed : "..key)
end
if key == "r" then
rcount = rcount + 1
end
TextCenterOnX(50,80,3,"pressed 'r' key "..rcount.." times so far")
end