| 1 |
""" |
| 2 |
This module provides the :class:`InputEvent` class, which closely |
| 3 |
resembles the ``input_event`` struct defined in ``linux/input.h``: |
| 4 |
|
| 5 |
.. code-block:: c |
| 6 |
|
| 7 |
struct input_event { |
| 8 |
struct timeval time; |
| 9 |
__u16 type; |
| 10 |
__u16 code; |
| 11 |
__s32 value; |
| 12 |
}; |
| 13 |
|
| 14 |
This module also defines several :class:`InputEvent` sub-classes that |
| 15 |
know more about the different types of events (key, abs, rel etc). The |
| 16 |
:data:`event_factory` dictionary maps event types to these classes. |
| 17 |
|
| 18 |
Assuming you use the :func:`evdev.util.categorize()` function to |
| 19 |
categorize events according to their type, adding or replacing a class |
| 20 |
for a specific event type becomes a matter of modifying |
| 21 |
:data:`event_factory`. |
| 22 |
|
| 23 |
All classes in this module have reasonable ``str()`` and ``repr()`` |
| 24 |
methods:: |
| 25 |
|
| 26 |
>>> print(event) |
| 27 |
event at 1337197425.477827, code 04, type 04, val 458792 |
| 28 |
>>> print(repr(event)) |
| 29 |
InputEvent(1337197425L, 477827L, 4, 4, 458792L) |
| 30 |
|
| 31 |
>>> print(key_event) |
| 32 |
key event at 1337197425.477835, 28 (KEY_ENTER), up |
| 33 |
>>> print(repr(key_event)) |
| 34 |
KeyEvent(InputEvent(1337197425L, 477835L, 1, 28, 0L)) |
| 35 |
""" |
| 36 |
|
| 37 |
# event type descriptions have been taken mot-a-mot from: |
| 38 |
# http://www.kernel.org/doc/Documentation/input/event-codes.txt |
| 39 |
|
| 40 |
# pylint: disable=no-name-in-module |
| 41 |
from typing import Final |
| 42 |
from .ecodes import ABS, EV_ABS, EV_KEY, EV_REL, EV_SYN, KEY, REL, SYN, keys |
| 43 |
|
| 44 |
|
| 45 |
class InputEvent: |
| 46 |
"""A generic input event.""" |
| 47 |
|
| 48 |
__slots__ = "sec", "usec", "type", "code", "value" |
| 49 |
|
| 50 |
def __init__(self, sec: int, usec: int, type: int, code: int, value: int): |
| 51 |
#: Time in seconds since epoch at which event occurred. |
| 52 |
self.sec: int = sec |
| 53 |
|
| 54 |
#: Microsecond portion of the timestamp. |
| 55 |
self.usec: int = usec |
| 56 |
|
| 57 |
#: Event type - one of ``ecodes.EV_*``. |
| 58 |
self.type: int = type |
| 59 |
|
| 60 |
#: Event code related to the event type. |
| 61 |
self.code: int = code |
| 62 |
|
| 63 |
#: Event value related to the event type. |
| 64 |
self.value: int = value |
| 65 |
|
| 66 |
def timestamp(self) -> float: |
| 67 |
"""Return event timestamp as a float.""" |
| 68 |
return self.sec + (self.usec / 1000000.0) |
| 69 |
|
| 70 |
def __str__(self) -> str: |
| 71 |
msg = "event at {:f}, code {:02d}, type {:02d}, val {:02d}" |
| 72 |
return msg.format(self.timestamp(), self.code, self.type, self.value) |
| 73 |
|
| 74 |
def __repr__(self) -> str: |
| 75 |
msg = "{}({!r}, {!r}, {!r}, {!r}, {!r})" |
| 76 |
return msg.format(self.__class__.__name__, self.sec, self.usec, self.type, self.code, self.value) |
| 77 |
|
| 78 |
|
| 79 |
class KeyEvent: |
| 80 |
"""An event generated by a keyboard, button or other key-like devices.""" |
| 81 |
|
| 82 |
key_up: Final[int] = 0x0 |
| 83 |
key_down: Final[int] = 0x1 |
| 84 |
key_hold: Final[int] = 0x2 |
| 85 |
|
| 86 |
__slots__ = "scancode", "keycode", "keystate", "event" |
| 87 |
|
| 88 |
def __init__(self, event: InputEvent, allow_unknown: bool = False): |
| 89 |
""" |
| 90 |
The ``allow_unknown`` argument determines what to do in the event of an event code |
| 91 |
for which a key code cannot be found. If ``False`` a ``KeyError`` will be raised. |
| 92 |
If ``True`` the keycode will be set to the hex value of the event code. |
| 93 |
""" |
| 94 |
|
| 95 |
self.scancode: int = event.code |
| 96 |
|
| 97 |
if event.value == 0: |
| 98 |
self.keystate: int = KeyEvent.key_up |
| 99 |
elif event.value == 2: |
| 100 |
self.keystate: int = KeyEvent.key_hold |
| 101 |
elif event.value == 1: |
| 102 |
self.keystate: int = KeyEvent.key_down |
| 103 |
|
| 104 |
try: |
| 105 |
self.keycode: int = keys[event.code] |
| 106 |
except KeyError: |
| 107 |
if allow_unknown: |
| 108 |
self.keycode: int = "0x{:02X}".format(event.code) |
| 109 |
else: |
| 110 |
raise |
| 111 |
|
| 112 |
#: Reference to an :class:`InputEvent` instance. |
| 113 |
self.event: InputEvent = event |
| 114 |
|
| 115 |
def __str__(self) -> str: |
| 116 |
try: |
| 117 |
ks = ("up", "down", "hold")[self.keystate] |
| 118 |
except IndexError: |
| 119 |
ks = "unknown" |
| 120 |
|
| 121 |
msg = "key event at {:f}, {} ({}), {}" |
| 122 |
return msg.format(self.event.timestamp(), self.scancode, self.keycode, ks) |
| 123 |
|
| 124 |
def __repr__(self) -> str: |
| 125 |
return "{}({!r})".format(self.__class__.__name__, self.event) |
| 126 |
|
| 127 |
|
| 128 |
class RelEvent: |
| 129 |
"""A relative axis event (e.g moving the mouse 5 units to the left).""" |
| 130 |
|
| 131 |
__slots__ = "event" |
| 132 |
|
| 133 |
def __init__(self, event: InputEvent): |
| 134 |
#: Reference to an :class:`InputEvent` instance. |
| 135 |
self.event: InputEvent = event |
| 136 |
|
| 137 |
def __str__(self) -> str: |
| 138 |
msg = "relative axis event at {:f}, {}" |
| 139 |
return msg.format(self.event.timestamp(), REL[self.event.code]) |
| 140 |
|
| 141 |
def __repr__(self) -> str: |
| 142 |
return "{}({!r})".format(self.__class__.__name__, self.event) |
| 143 |
|
| 144 |
|
| 145 |
class AbsEvent: |
| 146 |
"""An absolute axis event (e.g the coordinates of a tap on a touchscreen).""" |
| 147 |
|
| 148 |
__slots__ = "event" |
| 149 |
|
| 150 |
def __init__(self, event: InputEvent): |
| 151 |
#: Reference to an :class:`InputEvent` instance. |
| 152 |
self.event: InputEvent = event |
| 153 |
|
| 154 |
def __str__(self) -> str: |
| 155 |
msg = "absolute axis event at {:f}, {}" |
| 156 |
return msg.format(self.event.timestamp(), ABS[self.event.code]) |
| 157 |
|
| 158 |
def __repr__(self) -> str: |
| 159 |
return "{}({!r})".format(self.__class__.__name__, self.event) |
| 160 |
|
| 161 |
|
| 162 |
class SynEvent: |
| 163 |
""" |
| 164 |
A synchronization event. Used as markers to separate events. Events may be |
| 165 |
separated in time or in space, such as with the multitouch protocol. |
| 166 |
""" |
| 167 |
|
| 168 |
__slots__ = "event" |
| 169 |
|
| 170 |
def __init__(self, event: InputEvent): |
| 171 |
#: Reference to an :class:`InputEvent` instance. |
| 172 |
self.event: InputEvent = event |
| 173 |
|
| 174 |
def __str__(self) -> str: |
| 175 |
msg = "synchronization event at {:f}, {}" |
| 176 |
return msg.format(self.event.timestamp(), SYN[self.event.code]) |
| 177 |
|
| 178 |
def __repr__(self) -> str: |
| 179 |
return "{}({!r})".format(self.__class__.__name__, self.event) |
| 180 |
|
| 181 |
|
| 182 |
#: A mapping of event types to :class:`InputEvent` sub-classes. Used |
| 183 |
#: by :func:`evdev.util.categorize()` |
| 184 |
event_factory: dict[int, type[InputEvent]] = { |
| 185 |
EV_KEY: KeyEvent, |
| 186 |
EV_REL: RelEvent, |
| 187 |
EV_ABS: AbsEvent, |
| 188 |
EV_SYN: SynEvent, |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
__all__ = ("InputEvent", "KeyEvent", "RelEvent", "SynEvent", "AbsEvent", "event_factory") |