Skip to content

🕹️ Input API

linuxpy.input.device

Human friendly interface to linux Input subsystem.

The heart of linuxpy input library is the Device class. The recommended way is to use one of the find methods to create a Device object and use it within a context manager like:

from linuxpy.input.device import find_gamepad

with find_gamepad() as gamepad:
    print(f"Gamepad name: {gamepad.name}")

InputError

Bases: Exception

Input error

Event(event: input_event)

Event generated by an input device

timestamp: float property

The timestamp associated with the event

type: EventType property

The type of event

code property

The event code

value: int property

The event value

Device(*args, **kwargs)

Bases: BaseDevice

Central linux input subsystem class.

You can create an instance directly if you know the device name:

from linuxpy.input.device import Device

with Device("/dev/input11") as i11:
    print(i11.name)

... but it is generally easier to use the find helper to get a device with a certain condition. Example:

from linuxpy.input.device import find

track_point = find(name="TPPS/2 Elan TrackPoint")

name: str cached property

The device name

version: Version cached property

The version

physical_location: str cached property

The physical location

device_id: input_id cached property

The device input ID

capabilities cached property

The device capabilities

active_keys property

All active keys at the moment of calling this

x property

Current absolute X value

y property

Current absolute Y value

z property

Current absolute Z value

rx property

Current relative X value

ry property

Current relative Y value

rz property

Current relative Z value

__iter__() -> Iterable[Event]

Build an infinite iterator that streams input events. You'll need an open Device before using it:

from linuxpy.input.device import find_mouse

with find_mouse() as mouse:
    for event in mouse:
        print(event)

__aiter__() -> AsyncIterable[Event] async

Build an infinite async iterator that streams input events. You'll need an open Device before using it:

import asyncio
from linuxpy.input.device import find_mouse

async def main():
    with find_mouse() as mouse:
        async for event in mouse:
            print(event)

asyncio.run(main())

get_abs_info(abs_code)

Absolute information for the given abs code

read_event()

Read event. Event must be available to read or otherwise will raise an error

grab()

Grab the device for exclusive use

ungrab()

Release (ungrab) the device

Grab(device: Device)

Context manager which grabs the device on enter and releases (ungrabs) it on exit.

The device should be open for operation before the object is called on the with statement.

This context manager is reusable but not reentrant and not thread safe.

Example:

from linuxpy.input.device import find_mouse, Grab
with find_mouse() as mouse:
    with Grab(mouse):
        print(mouse.active_keys)

BaseUDevice(filename=PATH, bus=Bus.VIRTUAL, vendor_id=1, product_id=1, name='linuxpy emulated device')

Bases: BaseDevice

A uinput device with no capabilities registered

iter_input_files(path: PathLike = '/dev/input', pattern: str = 'event*')

List readable character devices in the given path.

event_batch_stream(fd) -> Iterable[Sequence[Event]]

Yields packets of events occurring at the same moment in time.

async_event_batch_stream(fd, maxsize: int = 1000) -> AsyncIterable[Sequence[Event]] async

Yields packets of events occurring at the same moment in time.

find(find_all: bool = False, custom_match: Optional[Callable] = None, **kwargs) -> Union[Device, Iterable[Device], None]

If find_all is False:

Find a device follwing the criteria matched by custom_match and kwargs. If no device is found matching the criteria it returns None. Default is to return a random first device.

If find_all is True:

The result is an iterator. Find all devices that match the criteria custom_match and kwargs. If no device is found matching the criteria it returns an empty iterator. Default is to return an iterator over all input devices found on the system.

find_gamepad(find_all: bool = False, custom_match: Optional[Callable] = None, **kwargs) -> Union[Device, Iterable[Device], None]

If find_all is False:

Find a gamepad device follwing the criteria matched by custom_match and kwargs. If no device is found matching the criteria it returns None. Default is to return a random first gamepad.

If find_all is True:

The result is an iterator. Find all gamepad devices that match the criteria custom_match and kwargs. If no gamepad is found matching the criteria it returns an empty iterator. Default is to return an iterator over all gamepad devices found on the system.

find_keyboard(find_all: bool = False, custom_match: Optional[Callable] = None, **kwargs) -> Union[Device, Iterable[Device], None]

If find_all is False:

Find a keyboard device follwing the criteria matched by custom_match and kwargs. If no device is found matching the criteria it returns None. Default is to return a random first keyboard.

If find_all is True:

The result is an iterator. Find all keyboard devices that match the criteria custom_match and kwargs. If no keyboard is found matching the criteria it returns an empty iterator. Default is to return an iterator over all keyboard devices found on the system.

find_mouse(find_all: bool = False, custom_match: Optional[Callable] = None, **kwargs) -> Union[Device, Iterable[Device], None]

If find_all is False:

Find a mouse device follwing the criteria matched by custom_match and kwargs. If no device is found matching the criteria it returns None. Default is to return a random first mouse.

If find_all is True:

The result is an iterator. Find all mouse devices that match the criteria custom_match and kwargs. If no mouse is found matching the criteria it returns an empty iterator. Default is to return an iterator over all mouse devices found on the system.