Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,36 +113,29 @@ Reading events from a single device in an endless loop.
key event at 1337016190.284160, 57 (KEY_SPACE), up


Reading events (using :mod:`asyncore`)
Reading events (using :mod:`asyncio`)
======================================

.. note::

This requires Python 3.5+ for the async/await keywords.


::

>>> from asyncore import file_dispatcher, loop
>>> from asyncio import get_event_loop
>>> from evdev import InputDevice, categorize, ecodes
>>> dev = InputDevice('/dev/input/event1')

>>> class InputDeviceDispatcher(file_dispatcher):
... def __init__(self, device):
... self.device = device
... file_dispatcher.__init__(self, device)
...
... def recv(self, ign=None):
... return self.device.read()
...
... def handle_read(self):
... for event in self.recv():
... print(repr(event))

>>> InputDeviceDispatcher(dev)
>>> loop()
InputEvent(1337255905L, 358854L, 1, 30, 0L)
InputEvent(1337255905L, 358857L, 0, 0, 0L)

.. note::
>>> async def helper(d):
... async for ev in d.async_read_loop():
... print(repr(ev))

The :mod:`asyncore` module is deprecated in recent versions of Python.
Please consider using :mod:`asyncio`.
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(helper(d))
InputEvent(1527363738, 348740, 4, 4, 458792)
InputEvent(1527363738, 348740, 1, 28, 0)
InputEvent(1527363738, 348740, 0, 0, 0)


Reading events from multiple devices (using :mod:`select`)
Expand Down
12 changes: 6 additions & 6 deletions evdev/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
from evdev.events import InputEvent

try:
from evdev.eventio_async import EventIO
from evdev.eventio_async import EventIO, EvdevError
except ImportError:
from evdev.eventio import EventIO
from evdev.eventio import EventIO, EvdevError


#--------------------------------------------------------------------------
class EvdevError(Exception):
pass
import sys
if sys.version_info > (3,):
buffer = memoryview

#--------------------------------------------------------------------------
_AbsInfo = collections.namedtuple(
Expand Down Expand Up @@ -165,6 +164,7 @@ def __del__(self):

def _capabilities(self, absinfo=True):
res = {}

for etype, ecodes in self._rawcapabilities.items():
for code in ecodes:
l = res.setdefault(etype, [])
Expand Down
4 changes: 4 additions & 0 deletions evdev/eventio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from evdev import _input, _uinput, ecodes, util
from evdev.events import InputEvent

#--------------------------------------------------------------------------
class EvdevError(Exception):
pass


class EventIO(object):
'''
Expand Down
4 changes: 3 additions & 1 deletion evdev/eventio_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import select

from evdev import eventio
# needed for compatibility
from evdev.eventio import EvdevError


class EventIO(eventio.EventIO):
def _do_when_readable(self, callback):
loop = asyncio.get_event_loop()

def ready():
loop.remove_reader(self.fileno())
callback()
Expand Down Expand Up @@ -70,7 +73,6 @@ def __next__(self):
self.current_batch = self.device.read()
return next(self.current_batch)

@asyncio.coroutine
def __aiter__(self):
return self

Expand Down