Difference between revisions of "Key (event)"
From ComputerCraft Wiki
m (better coding habits) |
MKlegoman357 (Talk | contribs) (Updated to ComputerCraft 1.74) |
||
Line 3: | Line 3: | ||
|desc=Fired when any key except Escape is pressed while the terminal is focused. | |desc=Fired when any key except Escape is pressed while the terminal is focused. | ||
|return1=The numerical key value of the key pressed | |return1=The numerical key value of the key pressed | ||
+ | |return2=A boolean indicating whether the key event was generated while holding the key (<var>true</var>), rather than pressing it the first time (<var>false</var>). | ||
}} | }} | ||
{{Example | {{Example | ||
− | |desc=Print each key pressed on the keyboard whenever a ''key'' event is fired. Use Ctrl+T (Windows)<!--- or ???+T (Mac)---> to terminate the loop. | + | |desc=Print each key pressed on the keyboard whenever a ''"key"'' event is fired. Use Ctrl+T (Windows)<!--- or ???+T (Mac)---> to terminate the loop. |
|code= | |code= | ||
while true do | while true do | ||
− | local event, | + | local event, key, isHeld = '''[[os.pullEvent]]("key")''' |
− | + | ||
+ | [[write]]( [[keys.getName]]( key ) ) | ||
+ | [[print]]( isHeld and " is being held." or " was pressed." ) | ||
end | end | ||
|output=Any key that is pressed. | |output=Any key that is pressed. | ||
Line 15: | Line 18: | ||
==Key scan codes == | ==Key scan codes == | ||
− | |||
− | |||
These scan codes are also available as constants in the [[Keys (API)|keys API]], and can be translated from numerical codes to strings using [[keys.getName]]. | These scan codes are also available as constants in the [[Keys (API)|keys API]], and can be translated from numerical codes to strings using [[keys.getName]]. | ||
+ | |||
+ | [[File:CC-Keyboard-Charcodes.png|center|frame|250x250px|Click for a larger copy of this image - contains keys and their event numbers.]] |
Revision as of 11:07, 28 June 2015
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.