Keyboard

From Xojo Documentation


Module

An intrinsic object that represents the current state of the keyboard. A convenient place to test for keyboard events is the Action event of a Timer.

Properties
AltKey fa-lock-32.png AsyncKeyDown fa-lock-32.png ControlKey fa-lock-32.png
AlternateMenuShortCutKey fa-lock-32.png AsyncMenuShortcutKey fa-lock-32.png MenuShortcutKey fa-lock-32.png
AsyncAltKey fa-lock-32.png AsyncOSKey fa-lock-32.png OSKey fa-lock-32.png
AsyncAlternateMenuShortCutKey fa-lock-32.png AsyncOptionKey fa-lock-32.png OptionKey fa-lock-32.png
AsyncCommandKey fa-lock-32.png AsyncShiftKey fa-lock-32.png ShiftKey fa-lock-32.png
AsyncControlKey fa-lock-32.png CommandKey fa-lock-32.png


Methods
Keyname

Notes

The Keyboard module is used to determine if particular keys are being pressed. The async version of each keyboard property tells you the immediate state of the key. If you want to know the state of the key when an event was queued, you should use the non-async version of the properties. These properties are updated constantly during code execution.

Windows

You can pass raw Win32 key codes to AsyncKeyDown. Add 1 to the raw keyCode and put it in the high word of the Integer you are passing. For example:

Keyboard.AsyncKeyDown((rawKeyCode + 1) * &hFFFF)

The "regular" way of detecting a particular keyCode, illustrated by the first example in the Examples section, also works for Windows.

KeyCodes

The following tables give the keycodes for the US keyboard. Keyboards for other languages may differ. These values are given in hex. You can pass values in another base if you wish. See the Examples section for examples that use Decimal and Hex values.

The AsyncKeyDown property returns True when the keycode passed to it is pressed.

Letters of the alphabet

Key KeyCode (hex value)
a 00
b 0B
c 08
d 02
e 0E
f 03
g 05
h 04
i 22
j 26
k 28
l 25
m 2E
n 2D
o 1F
p 23
q 0C
r 0F
s 01
t 11
u 20
v 09
w 0D
x 07
y 10
z 06

Numbers

Key KeyCode (hex value)
0 1D
1 12
2 13
3 14
4 15
5 17
6 16
7 1A
8 1C
9 19

Function and special keys

Key KeyCode (hex value)
F1 7A
F2 78
F3 63
F4 76
F5 60
F6 61
F7 62
F8 64
F9 65
F10 6D
F11 67
F12 6F
F13 69
F14 6B
F15 71
ESC 35
Space 31
Tab 30
Return 24
Home 73
Backspace 33
End 77
Page Up 74
Page Down 79
Delete 75
Help 72
Left 7B
Right 7C
Up 7E
Down 7D
= 18
1B
[ 21
] 1E
\ 2A
' 32
' 27
; 29
/ 2C
, 2B
. 2F
, 2B

Numeric Keypad codes

Key KeyCode (hex value)
1 53
2 54
3 55
4 56
5 57
6 58
7 59
8 5B
9 5C
0 52
+ 45
4E
/ 4B
* 43
Enter 4C
. 41
Clear 47

Sample Code

The following code tests whether the key for the letter "A" was pressed:

If Keyboard.AsynckeyDown(&h00) Then
// do something with this key here
End If

This code monitors the arrow keys and detects when an arrow key is pressed. Base 10 values are passed. Place this code in the Action event of a Timer and it will monitor the keyboard continuously.

If Keyboard.AsyncKeyDown(123) Then
// do something with the left arrow key
End If
If Keyboard.AsyncKeyDown(124) Then
// do something with the right arrow key...
End If
If Keyboard.AsyncKeyDown(125) Then
// do something with the down arrow key...
End If
If Keyboard.AsyncKeyDown(126) Then
// do something with the Up arrow key...
End If

See Also

RectControl.KeyDown, RectControl.KeyUp methods