Difference between revisions of "Raw key events"

From ComputerCraft Wiki
Jump to: navigation, search
(Replaced <pre> by <code>)
(Undo revision 1265 by Wukl (talk))
Line 3: Line 3:
 
==Detecting keystrokes==
 
==Detecting keystrokes==
 
In case I made you think the os sucks, it doesn't (all the time). Because this os is not Windows, we can access lower-level functions, such as os.pullEvent(). And that's the one that we're going to use:
 
In case I made you think the os sucks, it doesn't (all the time). Because this os is not Windows, we can access lower-level functions, such as os.pullEvent(). And that's the one that we're going to use:
<code>
+
<pre>
 
function rawread()
 
function rawread()
 
     bRead = true
 
     bRead = true
Line 13: Line 13:
 
     end
 
     end
 
end
 
end
</code>
+
</pre>
 
This function waits for any key, then exits.
 
This function waits for any key, then exits.
  
 
==Figuring out the key==
 
==Figuring out the key==
 
We can detect any key, but not which key. To find it out, we have to add some further logic to the code:
 
We can detect any key, but not which key. To find it out, we have to add some further logic to the code:
<code>
+
<pre>
 
function rawread()
 
function rawread()
 
     bRead = true
 
     bRead = true
Line 31: Line 31:
 
     end
 
     end
 
end
 
end
</code>
+
</pre>
 
This function waits for the Enter key to be pressed. If so, it writes "Enter detected" to the screen and exits.
 
This function waits for the Enter key to be pressed. If so, it writes "Enter detected" to the screen and exits.
  

Revision as of 18:00, 12 April 2012

The built-in os has a function to read incoming keystrokes. But it also implements the functions to move the cursor while typing. This is not always a desired feature; imagine a case in which you want to make a menu with a controllable cursor around the menu entries. The current read() function won't let you.

Detecting keystrokes

In case I made you think the os sucks, it doesn't (all the time). Because this os is not Windows, we can access lower-level functions, such as os.pullEvent(). And that's the one that we're going to use:

function rawread()
    bRead = true
    while(bRead)
        local sEvent, param = os.pullEvent("key")
        if(sEvent == "key") then
            break
        end
    end
end

This function waits for any key, then exits.

Figuring out the key

We can detect any key, but not which key. To find it out, we have to add some further logic to the code:

function rawread()
    bRead = true
    while(bRead)
        local sEvent, param = os.pullEvent("key")
        if(sEvent == "key") then
            if(param == 28) then
                print("Enter detected")
                break
            end
        end
    end
end

This function waits for the Enter key to be pressed. If so, it writes "Enter detected" to the screen and exits.

Key scan codes

The scan codes are typical DirectX numbers.

Key Code
Escape 1
1 2
2 3
3 4
5 6
7 8
9 10
0 11
Minus 12
Equals 13
Backspace 14
Tab 15
Q 16
W 17
E 18
R 19
T 20
Y 21
U 22
I 23
O 24
P 25
Key Code
Left Bracket 26
Right Bracket 27
Enter 28
Left Control 29
A 30
S 31
D 32
F 33
G 34
H 35
J 36
K 37
L 38
Semicolon 39
Apostrophe 40
Tilde (~) 41
Left Shift 42
Back Slash 43
Z 44
X 45
C 46
V 47
Key Code
B 48
N 49
M 50
Comma 51
Period 52
Forward Slash 53
Right Shift 54
Numpad * 55
Left Alt 56
Spacebar 57
Caps Lock 58
F1 59
F2 60
F3 61
F4 62
F5 63
F6 64
F7 65
F8 66
F9 67
F10 68
Key Code
Num Lock 69
Scroll Lock 70
Numpad 7 71
Numpad 8 72
Numpad 9 73
Numpad - 74
Numpad 4 75
Numpad 5 76
Numpad 6 77
Numpad + 78
Numpad 1 79
Numpad 2 80
Numpad 3 81
Numpad 0 82
Numpad . 83
F11 87
F12 88
Numpad Enter 156
Right Control 157
Numpad / 181
Right Alt 184
Key Code
Home 199
Up Arrow 200
Page Up 201
Left Arrow 203
Right Arrow 205
End 207
Down Arrow 208
Page Down 209
Insert 210
Delete 211
Left Mouse Button 256
Right Mouse Button 257
Middle Mouse/Wheel 258
Mouse Button 3 259
Mouse Button 4 260
Mouse Button 5 261
Mouse Button 6 262
Mouse Button 7 263
Mouse Wheel Up 264
Mouse Wheel Down 265

Source: Nexus Wiki

Author(s)

They deserve credit!