File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed
Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change 33
44robot = Robot (left = (4 , 14 ), right = (17 , 18 ))
55
6+ # Get the list of available input devices
67devices = [InputDevice (device ) for device in list_devices ()]
7- keyboard = devices [0 ] # this may vary
8+ # Filter out everything that's not a keyboard. Keyboards are defined as any
9+ # device which has keys, and which specifically has keys 1..31 (roughly Esc,
10+ # the numeric keys, the first row of QWERTY plus a few more) and which does
11+ # *not* have key 0 (reserved)
12+ must_have = {i for i in range (1 , 32 )}
13+ must_not_have = {0 }
14+ devices = [
15+ device
16+ for device in devices
17+ for keys in (set (device .capabilities ().get (ecodes .EV_KEY , [])),)
18+ if must_have .issubset (keys )
19+ and must_not_have .isdisjoint (keys )
20+ ]
21+ # Pick the first keyboard
22+ keyboard = devices [0 ]
823
924keypress_actions = {
1025 ecodes .KEY_UP : robot .forward ,
1429}
1530
1631for event in keyboard .read_loop ():
17- if event .type == ecodes .EV_KEY :
32+ if event .type == ecodes .EV_KEY and event . code in keypress_actions :
1833 if event .value == 1 : # key down
1934 keypress_actions [event .code ]()
2035 if event .value == 0 : # key up
Original file line number Diff line number Diff line change @@ -345,11 +345,16 @@ Use up/down/left/right keys to control a robot:
345345 recipe will *not * work in environments like IDLE.
346346
347347If you prefer a version that works under IDLE, the following recipe should
348- suffice, but will require that you install the evdev library with ``sudo pip3
349- install evdev `` first:
348+ suffice:
350349
351350.. literalinclude :: examples/robot_keyboard_2.py
352351
352+ .. note ::
353+
354+ This recipe uses the third-party ``evdev `` module. Install this library
355+ with ``sudo pip3 install evdev `` first. Be aware that ``evdev `` will only
356+ work with local input devices; this recipe will *not * work over SSH.
357+
353358
354359Motion sensor robot
355360===================
You can’t perform that action at this time.
0 commit comments