This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Xenthera's profile picture

A way to detect if a key is down (not if you press a key)

Started by Xenthera, 13 July 2013 - 06:30 PM
Xenthera #1
Posted 13 July 2013 - 08:30 PM
Is there a way to detect what key is being pressed? Say you have a function that returns a key that is being held down.


function getKey()
  return (Key that is currently pressed)
end
while true do
  print(getKey())
  sleep(1)
end

Output:

nil
nil
nil
nil
*presses enter*
28
28
28
28
*releases enter*
nil
nil
nil

Sorry for the noobish sounding explanation… Not sure how to explain it any other way though.
Grim Reaper #2
Posted 13 July 2013 - 08:49 PM
The only way that I could think of how to do this is to check if the key got pressed, then to wait about a tenth of a second to see if the same event is fired again. This might mean that the key is being held down.

Other than that, I don't think that there is currently a way to do this with computer craft.
LucasShadow #3
Posted 13 July 2013 - 08:50 PM
You may find this forum post helpful: http://www.computercraft.info/forums2/index.php?/topic/12915-detect-if-a-key-is-being-holded/