On a linux machine (Debian wheezy) I am trying to write an event-based server that does the following:
Grab exclusive input to the input device (a special keyboard) to prevent the keystroke get into the usual event chain.
Register for events in the twisted reactor
Register callback at the deferred returned from waiting for events. This callback would then send an HTTP request after a special key sequence is received.
This is the sample code from the pyevdev
package. It works that I get notified and receive the keystrokes accordingly.
By looking at the source code of the read_loop()
command it is also using the select
statement similar to twisted.
My question
How can I integrate this code into python Twisted? One idea would be to look at the underlying character device /dev/input/event0
and read from it in a non-blocking way. If if would be a regular file, I would use something along the lines of inotify but in this case I do not know.
sample code from the evdev package
from evdev import InputDevice, categorize, ecodes, list_devices
devices = [InputDevice(fn) for fn in list_devices()]
for dev in devices:
print(dev.fn, dev.name, dev.phys)
dev = InputDevice('/dev/input/event0')
# get exclusive access to input device
dev.grab()
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
print categorize(event)
evdev.device.InputDevice
has afileno()
method , which means that you can hook it up to a TwistedIReactorFDSet
; pretty much all reactors available on Linux whereevdev
is relevant implement this interface. Since an event device is an object with a file descriptor that you can mostly just read from, you need anIReadDescriptor
to wrap it.An implementation of roughly the same logic as your example, but using the reactor to process the events, might look like so:
Please note that this code is totally untested, so it may not work quite right at first, but it should at least give you an idea of what is required.