keyboard.py example not working

Asked by Jack Ha

I ran the mouse.py example and it worked fine as I saw the mouse cursor moved. I then tried the keyboard.py example but I didn't see the 'hello' text appear in any window. Am I supposed to see the 'hello' text appears on the active window? I am using ubuntu 12.10. I did see an entry in /var/log/syslog when I ran the keyboard.py example.

Dec 26 00:04:45 ubuntu12 kernel: [32838.956550] input: python-uinput as /devices/virtual/input/input192

Question information

Language:
English Edit question
Status:
Solved
For:
python-uinput Edit question
Assignee:
No assignee Edit question
Solved by:
a
Solved:
Last query:
Last reply:
Revision history for this message
Best a (tuomasjjrasanen) said :
#1

Thanks for this excellent question!

Yes, this is a known "feature" and it has actually nothing to do with uinput nor python-uinput. The reason why you don't see any output when examples/keyboard.py is run is due to delays in the underlying infrastructure (=xorg + its mechanisms to assign handlers for input devices).

I think the whole process chain goes like this:
1. user runs examples/keyboard.py
2. examples/keyboard.py uses kernel's uinput-interface to create a device
3. xorg receives a notification that there is a new keyboard device available and starts assigning a handler for the device (ASYNCHRONOUS)
4. examples/keyboard.py emits keyboard events via uinput-interface
5. examples/keyboard.py has done its job, the device is removed and examples/keyboard.py exits

The keyword here is of course 'asynchronous'. examples/keyboard.py lives its own life and does not care about Xorg at all. Xorg (or any other UI manager) does its processing and input device handling asynchronously in the background. It seems that there isn't any buffering going on, and therefore all events emitted before Xorg has assigned a proper handler are lost.

Try adding a small delay after the device initialization as follows:

  device = uinput.Device(events)
  time.sleep(2) # seconds

And the "problem" should disappear. However, note that in any real application should not be a problem, since in most cases there is already a natural delay (caused by user actions or cpu processing) between the device initialization and event emitting.

Revision history for this message
Jack Ha (tpdug-jack) said :
#2

Thanks Tuomas Jorma Juhani Räsänen, that solved my question.