A
A
Anton Kuzmichev2014-08-27 16:23:00
linux
Anton Kuzmichev, 2014-08-27 16:23:00

How to receive events simultaneously from several devices?

Good afternoon!
Preamble: I bought a trackball that does not have a scroll wheel, but this is perhaps its only drawback. Through xorg.conf, you can configure it so that, while holding one of the buttons, scroll with the ball. But, as luck would have it, the button I needed (the most convenient) did not fit for this.
I am writing a small Python script (under ubuntu), which, analyzing the flow of events from input devices, sends messages to the system as if I am scrolling when certain conditions occur.
Well, for example, "if I drove very slowly for 0.5 seconds (delta_x and delta_y no more than 1-2), then scroll further until I move the ball strongly", or "if the left mouse button was pressed for at least 0.5 seconds, and the ball did not moved, then scroll until I pull the ball"
There are some good results, but still something is not right.
I want to experiment with the keyboard. I suspect that it will be much more convenient if you hold down some key that is not particularly needed in Linux (for example, win), and at the same time emulate scrolling through ball movements.
This is how I listen to the device:

from evdev.device import InputDevice
input_device = InputDevice('/dev/input/event12')
for event in input_device.read_loop():
  # обрабатываем поступающие события
  pass

As I understand it, read_loop() is a generator that returns (or does not return) events from the input device in an infinite loop. If I try to connect to another device (to the keyboard), then I need to receive events from it in the same cycle. Thus, I already have 2 infinite generators, which I have so far unsuccessfully tried to combine, something like this:
input_device = InputDevice('/dev/input/event12')
input_device2 = InputDevice('/dev/input/event3')
for event in itertools.chain(input_device.read_loop(), input_device2.read_loop()):
  # сюда попадают события только от того генератора, что указан первым
  pass

Is there any way to combine these 2 generators? Or, is there a more kosher way to listen to (and receive) events from multiple input devices at once in linux?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Misha Krinkin, 2014-08-27
@Assargin

itertools.izip concatenates two iterators the way you want, but that's probably not what you want, since events from different device files can occur at different rates. Look towards select is better.
UPDATE: even an example is available at python-evdev.readthedocs.org/en/latest/tutorial.ht...

M
Maxim Vasiliev, 2014-08-27
@qmax

itertools.zip?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question