I am working with events coming from /dev/input/event0
, buttons. I'm currently polling in an infinite loop but would prefer to wait for an event.
import struct
# https://docs.python.org/3/library/struct.html#format-characters
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)
EV_KEY = 0x01
KEY_UP = 0
KEY_DOWN = 1
KEY_AUTO = 2
with open('/dev/input/event0', 'rb') as infile:
state = 0
while True:
event = infile.read(EVENT_SIZE)
(tv_sec, tv_usec, typ, code, value) = struct.unpack(FORMAT, event)
if 0 == state:
if EV_KEY == typ and KEY_DOWN == value:
print('You pressed a button')
state = 1
elif 1 == state:
if EV_KEY == typ and KEY_UP == value:
print('You released a button')
state = 0
Can I use epoll()
with python to accomplish this in much less code?