2

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?

tarabyte
  • 17,837
  • 15
  • 76
  • 117
  • Similar question here: http://stackoverflow.com/questions/41920301/is-it-efficient-to-use-epoll-with-devices-dev-event - You can use _epoll_, but you may add latency (doh!) – Emanuele Feb 03 '17 at 22:46

0 Answers0