Skip to content

Commit d4bf97d

Browse files
committed
Add example using the selectors module
1 parent 8261808 commit d4bf97d

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

‎doc/tutorial.rst‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ Reading events from multiple devices
117117
>>> from evdev import InputDevice
118118
>>> from select import select
119119

120+
# A mapping of file descriptors (integers) to InputDevice instances.
120121
>>> devices = map(InputDevice, ('/dev/input/event1', '/dev/input/event2'))
121-
>>> devices = {dev.fd : dev for dev in devices}
122+
>>> devices = {dev.fd: dev for dev in devices}
122123

123124
>>> for dev in devices.values(): print(dev)
124125
device /dev/input/event1, name "Dell Dell USB Keyboard", phys "usb-0000:00:12.1-2/input0"
125126
device /dev/input/event2, name "Logitech USB Laser Mouse", phys "usb-0000:00:12.0-2/input0"
126127

127128
>>> while True:
128-
... r,w,x = select(devices, [], [])
129+
... r, w, x = select(devices, [], [])
129130
... for fd in r:
130131
... for event in devices[fd].read():
131132
... print(event)
@@ -135,6 +136,29 @@ Reading events from multiple devices
135136
event at 1351116708.782237, code 02, type 01, val 01
136137

137138

139+
This can also be achieved using the selectors_ module in Python 3.4:
140+
141+
::
142+
143+
from evdev import InputDevice
144+
from selectors import DefaultSelector, EVENT_READ
145+
146+
selector = selectors.DefaultSelector()
147+
148+
mouse = evdev.InputDevice('/dev/input/event1')
149+
keybd = evdev.InputDevice('/dev/input/event2')
150+
151+
# This works because InputDevice has a `fileno()` method.
152+
selector.register(mouse, selectors.EVENT_READ)
153+
selector.register(keybd, selectors.EVENT_READ)
154+
155+
while True:
156+
for key, mask in selector.select():
157+
device = key.fileobj
158+
for event in device.read():
159+
print(event)
160+
161+
138162
Accessing evdev constants
139163
=========================
140164

@@ -262,3 +286,6 @@ Specifying ``uinput`` device options
262286
>>> ui.write(e.EV_ABS, e.ABS_X, 20)
263287
>>> ui.write(e.EV_ABS, e.ABS_Y, 20)
264288
>>> ui.syn()
289+
290+
291+
.. _selectors: https://docs.python.org/3/library/selectors.html

0 commit comments

Comments
 (0)