Module

Keyboard


Description

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.

Methods

Name

Parameters

Returns

Shared

AsyncKeyDown

keyCode As Integer

Boolean

Keyname

keyCode As Integer

String

Property descriptions


Keyboard.AlternateMenuShortCutKey

AlternateMenuShortCutKey As Boolean

If True, the alternate menu shortcut modifier is depressed when the current method or event handler began. This checks the Shift key on all platforms.

This property is read-only.

This example detects the Shift key. It is in the Action event of a Timer.

If Keyboard.AlternateMenuShortcutKey Then
  ' handle the keyboard event here....
End If

Keyboard.AltKey

AltKey As Boolean

If True, the Alt key on Windows and Linux and Option key on Macintosh was depressed when the current method or event handler began.

This property is read-only.

This example detects the Alt or Option key. It is in the Action event of a Timer.

If Keyboard.AltKey Then
  MessageBox("Option/Alt key")
End If

Keyboard.AsyncAlternateMenuShortCutKey

AsyncAlternateMenuShortCutKey As Boolean

If True, the alternate menu shortcut key is depressed. This checks the Shift key on all platforms.

This property is read-only.

The following example detects the Shift key. The code is in the Action event of a Timer.

If Keyboard.AsyncAlternateMenuShortcutKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.AsyncAltKey

AsyncAltKey As Boolean

If True, the Alt key (Windows and Linux) or Option key (MacOS) is depressed.

This property is read-only.

This example detects whether the Alt/Option key is depressed.

If Keyboard.AsyncAltKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.AsyncCommandKey

AsyncCommandKey As Boolean

If True, the Command key is depressed on Macintosh (or Windows/OS key on Windows/Linux).

This property is read-only.

If Keyboard.AsyncCommandKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.AsyncControlKey

AsyncControlKey As Boolean

If True, the Control key is depressed.

This property is read-only.

This example detects whether the Control key is depressed.

If Keyboard.AsyncControlKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.AsyncMenuShortcutKey

AsyncMenuShortcutKey As Boolean

If True, the menu shortcut modifier key is depressed. This property tests the Control key on Windows and Linux and the Command key on Macintosh.

This property is read-only.

The following example detects the menu modifier on the user's platform.

If Keyboard.AsyncMenuShortcutKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.AsyncOptionKey

AsyncOptionKey As Boolean

If True, the Option key (macOS) or Alt key (Windows and Linux) is depressed.

This property is read-only.

The following code detects whether the Option key on Macintosh was depressed.

If Keyboard.AsyncOptionKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.AsyncOSKey

AsyncOSKey As Boolean

If True, the OS key is depressed. On Windows and Linux, this is the key with the Windows flag; on Macintosh, it is the Command key.

This property is read-only.

This example detects whether the OS key was depressed. It is in the Action event of a Timer.

If Keyboard.AsyncOSKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.AsyncShiftKey

AsyncShiftKey As Boolean

If True, the Shift key is depressed.

This property is read-only.

This example detects the Shift key. The code is in the Action event of a Timer.

If Keyboard.AsyncShiftKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.CommandKey

CommandKey As Boolean

If True, the Command key is depressed on Macintosh (or Windows/OS key on Windows/Linux) when the current method or event handler began.

This property is read-only.

The following code detects whether the Command key is depressed. The code is in the Action event of a Timer.

If Keyboard.CommandKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.ControlKey

ControlKey As Boolean

If True, the Control key was depressed when the current method or event handler began.

This property is read-only.

This example detects whether the Control key is depressed. The code is in the Action event of a Timer.

If Keyboard.ControlKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.MenuShortcutKey

MenuShortcutKey As Boolean

If True, the menu shortcut modifier key was depressed when the current method or event handler began. This property tests the Control key on Windows and Linux and the Command key on Macintosh.

This property is read-only.

This example detects whether the menu shortcut modifier key was depressed. The code is in the Action event of a Timer.

If Keyboard.MenuShortcutKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.OptionKey

OptionKey As Boolean

If True, the Alt key (Windows and Linux) or Option key (MacOS) is depressed when the method/event began.

This property is read-only.

This code detects whether the Option key was depressed. The code is in the Action event of a Timer:

If Keyboard.OptionKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.OSKey

OSKey As Boolean

If True, the OS key was depressed when the current method or event handler began. On Windows and Linux, this is the Windows flag key; on Macintosh, it is the Command key.

This property is read-only.

This example detects whether the OS key was depressed on the user's platform. The code is in the Action event of a Timer.

If Keyboard.OSKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Keyboard.ShiftKey

ShiftKey As Boolean

If True, the Shift key was depressed when the current method or event handler began.

This property is read-only.

This example detects whether the Shift key was depressed. The code is in the Action event of a Timer.

If Keyboard.ShiftKey Then
  ' handle the keyboard event here....
  MessageBox("keyboard event detected...")
End If

Method descriptions


Keyboard.AsyncKeyDown

AsyncKeyDown(keyCode As Integer) As Boolean

If True, the key whose KeyCode was passed is depressed. See the tables in the Keyboard Notes section for the values of KeyCode for the English (American) Keyboard.

The following example monitors the arrow keys and detects whether one of them is depressed. The code is in the Action event of a Timer control.

If Keyboard.AsyncKeyDown(&h7B) Then
  ' do something with the left arrow key
End If
If Keyboard.AsyncKeyDown(&h7C) Then
  ' do something with the right arrow key...
End If
If Keyboard.AsyncKeyDown(&h7D) Then
  ' do something with the down arrow key...
End If
If Keyboard.AsyncKeyDown(&h7E) Then
  ' do something with the Up arrow key...
End If

Keyboard.Keyname

Keyname(keycode As Integer) As String

Returns as a String the name of the keycode passed.

Refer to the Keyboard Notes to a list of key codes.

This returns a value that indicates the character the key produces. In most cases, special keys will be named as follows: Esc, Tab, CapsLock, Shift, Control, Option, Alt, Command, Win, Return, Enter, Space, Fn, Delete, Backspace, Help, Insert, Home, PageUp, Del, End, PageDown, F1` through F15, PrintScreen, ScrollLock, Pause/Break, Clear, NumLock, KP=, KP/, KP*, KP-, KP0 through KP9, Up, Left, Down and Right.

The following example returns the name of character &h7D. It is the Down arrow key.

MessageBox(Keyboard.keyname(&h7D))

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

Compatibility

All project types on all supported operating systems.