/dev/usb/kbd

From WiiBrew
< /dev‎ | usb
Jump to navigation Jump to search

/dev/usb/kbd is the interface to the USB keyboard driver.

Reads, seeks and ioctlvs always fail. Writes are used to send commands. Ioctls are used to receive events.

The device can be opened and closed even without a keyboard connected. Performing an ioctl on the device in this state will simply block forever.

Write

Writes are used the send commands to the driver. This is for controlling the keyboard LEDs. Writes are always 5 bytes in length.

Available commands:
Write data Function
00 00 00 00 80 Turn off keyboard LEDs.
?? ?? ?? ?? 00 Unknown
?? ?? ?? ?? 00 Unknown
?? ?? ?? ?? 00 Unknown
?? ?? ?? ?? 00 Unknown

Ioctl

Ioctls are used to read keyboard events. All parameters except the output buffer are ignored. The buffer should be 16 bytes in size and will be filled with the event data. Note that the ioctl is blocking.

Event data format
Offset Length Contents
0 4 Message type
4 4 Unknown (keyboard identifier?)
8 1 Modifiers
9 1 Unknown
10 6 Pressed keys

Message type

Message type is one of the following:

Hex value Description
00 00 00 00 Keyboard connect
00 00 00 01 Keyboard disconnect
00 00 00 02 Event

You will always be properly notified of available keyboards. This means that when the device is opened after the keyboard is connected, a keyboard connect event is still received as the very first message.

Modifiers

The modifiers are a bitmask of the following:

Bit no. Hex value Description
1 01 Left control
2 02 Left shift
3 04 Left alt / left meta
4 08 Left super / left windows key
Bit no. Hex value Description
5 10 Right control
6 20 Right shift
7 40 Right alt / right meta / alt gr
8 80 Right super / right windows key

Key data

The key data is an array of bytes with key codes. The codes are probably passed directly from the keyboard as they correspond to the table in the USB HID Usage Tables Chapter 10 (Keyboard/Keypad Page), available here.

Each byte represents a pressed key. When a key is pressed, a new event will arrive with the key added to the array. Once a key is released, a new event will arrive with the key simply removed from the array.

The array is 6 bytes long, so no more than 6 simultaneous key presses can be detected. On top of that, most keyboards will barf on certain combinations. If an error occurs, a new event will arrive with all elements of the array set to 0x01.

Special keys that some keyboards provide seem to be ignored.

The following table of key codes was pulled from a Logitech Internet Navigator keyboard, which is a 105 key US layout keyboard with some special keys:

Dec Hex Description
4 04 a
5 05 b
6 06 c
7 07 d
8 08 e
9 09 f
10 0a g
11 0b h
12 0c i
13 0d j
14 0e k
15 0f l
16 10 m
17 11 n
18 12 o
19 13 p
20 14 q
21 15 r
22 16 s
23 17 t
24 18 u
25 19 v
26 1a w
27 1b x
28 1c y
29 1d z
Dec Hex Description
31 1f 1
32 20 2
33 21 3
34 22 4
35 23 5
36 24 6
37 25 7
38 26 8
39 27 9
40 28 Enter
41 29 Escape
42 2a Backspace
43 2b Tab
44 2c Spacebar
45 2d -
46 2e =
47 2f [
48 30 ]
49 31 Unknown
50 32 Right \
51 33 ;
52 34 '
53 35 ~
54 36 ,
55 37 .
56 38 /
57 39 Caps Lock
Dec Hex Description
58 3a F1
59 3b F2
60 3c F3
61 3d F4
62 3e F5
63 3f F6
64 40 F7
65 41 F8
66 42 F9
67 43 F10
68 44 F11
69 45 F12
70 46 Print screen
71 47 Scroll lock
72 48 Pause / Break
73 49 Insert
74 4a Home
75 4b Page Up
76 4c Delete
77 4d End
78 4e Page Down
79 4f Arrow Right
80 50 Arrow Left
81 51 Arrow Down
82 52 Arrow Up
Dec Hex Description
83 53 Numlock
84 54 Numpad /
85 55 Numpad *
86 56 Numpad -
87 57 Numpad +
88 58 Numpad Enter
89 59 Numpad 1
90 5a Numpad 2
91 5b Numpad 3
92 5c Numpad 4
93 5d Numpad 5
94 5e Numpad 6
95 5f Numpad 7
96 60 Numpad 8
97 61 Numpad 9
98 62 Numpad 0
99 63 Numpad .
100 64 Left \
101 65 Menu key

I think the table above is wrong. My iMac keyboard gives 30 for '1' through 39 for '0'. Also, I get 39 for backslash. My F13, F14, and F15 keys are 104, 105, and 106. Everything else seems the same. [aksommerville]