Difference between revisions of "Key (event)"

From ComputerCraft Wiki
Jump to: navigation, search
(Updated to ComputerCraft 1.74)
m
Line 1: Line 1:
 +
{{Lowercase}}
 
{{Event
 
{{Event
 
|name=key
 
|name=key

Revision as of 11:32, 28 June 2015


Grid Modem.png  Event key
Fired when any key except Escape is pressed while the terminal is focused.
Returned Object 1 The numerical key value of the key pressed
Returned Object 2 A boolean indicating whether the key event was generated while holding the key (true), rather than pressing it the first time (false).


Grid paper.png  Example
Print each key pressed on the keyboard whenever a "key" event is fired. Use Ctrl+T (Windows) to terminate the loop.
Code
while true do
  local event, key, isHeld = os.pullEvent("key")
  
  write( keys.getName( key ) )
  print( isHeld and " is being held." or " was pressed." )
end
Output Any key that is pressed.



Key scan codes

These scan codes are also available as constants in the keys API, and can be translated from numerical codes to strings using keys.getName.

Click for a larger copy of this image - contains keys and their event numbers.