Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Uinput on raspberry pi

Sun Jul 05, 2015 10:03 pm

I have problem with using Uinput on Python 3 Raspberry pi. I installed uinput to python 3 and wrote the code:

Code: Select all

import uinput
with uinput.Device([uinput.REL_X, uinput.REL_Y,
                      uinput.BTN_LEFT, uinput.BTN_RIGHT]) as device:
      for i in range(20):
          device.emit(uinput.REL_X, 5)
          device.emit(uinput.REL_Y, 5)
I start this code from console in raspberry pi (NO putty), but nothing happens. What is my error?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 8:38 am

Have you added the uinput module?

Code: Select all

sudo modprobe uinput
I may be wrong, but other posts on the forum also suggest that scripts need to be run as root.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 9:45 am

Thank you for reply, and how to install modprobe command?
Last edited by Ivan219 on Mon Jul 06, 2015 11:48 am, edited 1 time in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 9:51 am

You don't need to install modprobe. Just run the command as I've typed it.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 10:01 am

Ok, I run the command as You've typed it. Then I run reboot, and then - sudo python3 -i /home/pi/Desktop/keyboard.py
But nothing happens again, may be error in code?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 10:06 am

Don't reboot after running that command (it will undo the effect). Just type the command and then run your script.

If that works we'll then set it up so you don't need to run the modprobe command every time (I've not told you how to do this yet as I'm not sure if it's the solution or not!).
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 10:19 am

Ok, sorry. I run: sudo modprobe uinput then sudo python3 -i /home/pi/Desktop/keyboard.py. Nothing happens(
And if I take example from example folder:

Code: Select all

import time
import uinput

def main():
    events = (
        uinput.KEY_E,
        uinput.KEY_H,
        uinput.KEY_L,
        uinput.KEY_O,
        )

    with uinput.Device(events) as device:
        time.sleep(1) # This is required here only for demonstration
                      # purposes. Without this, the underlying machinery might
                      # not have time to assign a proper handler for our device
                      # before the execution of this script reaches the end and
                      # the device is destroyed. At least this seems to be the
                      # case with X11 and its generic event device
                      # handlers. Without this magical sleep, "hello" might not
                      # get printed because this example exits before X11 gets
                      # its handlers ready for processing events from this
                      # device.
        device.emit_click(uinput.KEY_H)
        device.emit_click(uinput.KEY_E)
        device.emit_click(uinput.KEY_L)
        device.emit_click(uinput.KEY_L)
        device.emit_click(uinput.KEY_O)

if __name__ == "__main__":
    main()
And i had errors, than I replace "with uinput.Device(events) as device:" to "device=with uinput.Device(events)", no erros and nothing happens.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 10:27 am

What errors did you get?

Your suggested change doesn't make sense. The first bit of code

Code: Select all

with uinput.Device(events) as device:"
looks correct, whereas your changed code

Code: Select all

device=with uinput.Device(events)
doesn't look right at all.

Also, that piece of code looks like it should type "HELLO". However, I would assume that the active window needs to be something other than the python script (e.g. a text editor) because I would expect that key presses while the code is running won't show.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 10:37 am

I have this error:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/keyboard2.py", line 31, in <module>
    main()
  File "/home/pi/Desktop/keyboard2.py", line 13, in main
    with uinput.Device(events) as device:
AttributeError: __exit__
Yes, I know, when I run the code I click to TextEditor window. And may be error because I use remote control?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 10:51 am

OK. That's interesting. That sounds like the uinput.Device class doesn't have an "__exit__" method (which is needed for using the "with" context formatting).

Try this:

Code: Select all

import time
import uinput

def main():
    events = (
        uinput.KEY_E,
        uinput.KEY_H,
        uinput.KEY_L,
        uinput.KEY_O,
        )

    device = uinput.Device(events):
    time.sleep(1) # This is required here only for demonstration
                      # purposes. Without this, the underlying machinery might
                      # not have time to assign a proper handler for our device
                      # before the execution of this script reaches the end and
                      # the device is destroyed. At least this seems to be the
                      # case with X11 and its generic event device
                      # handlers. Without this magical sleep, "hello" might not
                      # get printed because this example exits before X11 gets
                      # its handlers ready for processing events from this
                      # device.
    device.emit_click(uinput.KEY_H)
    device.emit_click(uinput.KEY_E)
    device.emit_click(uinput.KEY_L)
    device.emit_click(uinput.KEY_L)
    device.emit_click(uinput.KEY_O)

if __name__ == "__main__":
    main()
This seems to be more in line with the examples on the site: http://tjjr.fi/sw/python-uinput/#genera ... ard-clicks

It's slightly different to the change in your earlier post (unless that was just a typo...)
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:04 am

Unfortunately, It doesn't works(

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:11 am

Do you mean it runs but there are no errors? if so, I'm running out of suggestions!

Can you post the output of

Code: Select all

lsmod
I just want to confirm that the module is installed.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:24 am

Yes, no errors and no happens.
What must I do with lsmod?
Last edited by Ivan219 on Mon Jul 06, 2015 11:48 am, edited 1 time in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:26 am

Just run it in the terminal. It just gives you a tidy list of the modules that are currently installed.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:32 am

Ivan219 wrote:I have this error:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/keyboard2.py", line 31, in <module>
    main()
  File "/home/pi/Desktop/keyboard2.py", line 13, in main
    with uinput.Device(events) as device:
AttributeError: __exit__
Yes, I know, when I run the code I click to TextEditor window. And may be error because I use remote control?
Looking at the source code of this I can see that there is an __exit__ method.

Are you sure you've got the latest version of the code?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:46 am

Lsmod:

Code: Select all

Module                  Size  Used by
evdev                   9950  0 
ctr                     3709  2 
ccm                     7771  2 
uinput                  7096  0 
snd_bcm2835            18649  6 
snd_pcm                73475  1 snd_bcm2835
snd_seq                53078  0 
snd_seq_device          5628  1 snd_seq
snd_timer              17784  2 snd_pcm,snd_seq
snd                    51038  17 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
arc4                    1745  2 
rt2800usb              17886  0 
rt2800lib              71839  1 rt2800usb
rt2x00usb               8517  1 rt2800usb
rt2x00lib              36704  3 rt2x00usb,rt2800lib,rt2800usb
mac80211              479400  3 rt2x00lib,rt2x00usb,rt2800lib
cfg80211              386508  2 mac80211,rt2x00lib
crc_ccitt               1153  1 rt2800lib
rfkill                 16651  2 cfg80211
uio_pdrv_genirq         2958  0 
uio                     8119  1 uio_pdrv_genirq
I install uinput from easy_install3.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:51 am

Thanks. That shows that the module is installed.

It's going to be very hard for me to recreate this as all my Pis run headless. I can see if I can get the code working on my laptop (linux) later tonight and see if I come across any other issues. Failing that, you may need some help from someone with direct experience of getting it to work on a Pi (have a search on the forum, there have been other people trying to use this too).
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 11:54 am

Thank you for your diligent assistance!)) I'll wait.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 12:11 pm

I connect raspberry to TV and see that your's code works!! It's great! Thank you! Please, tell me how to run modprobe every time automatically. And do you know how Uinput can write keyboard presses when I do remote control in Windows from XRDP on raspberry? Because when I start remote control in Windows - raspberry creates the second desktop.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 12:38 pm

Simplest way would be to run the following command:

Code: Select all

sudo sh -c "echo uinput >> /etc/modules"
This will add uinput to your modules file meaning that it will be loaded every time you boot the pi.

Enjoy!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 12:42 pm

Looking at the source code again, if the "with" method didn't work for you, you should probably call the "destroy" method when you're finished:
Destroy the device. This method destroys the device file and closes the underlying file descriptor. This method must be called exactly once for each created device.
The code would look like this:

Code: Select all

import time
import uinput

def main():
    events = (
        uinput.KEY_E,
        uinput.KEY_H,
        uinput.KEY_L,
        uinput.KEY_O,
        )

    device = uinput.Device(events):
    time.sleep(1) # This is required here only for demonstration
                      # purposes. Without this, the underlying machinery might
                      # not have time to assign a proper handler for our device
                      # before the execution of this script reaches the end and
                      # the device is destroyed. At least this seems to be the
                      # case with X11 and its generic event device
                      # handlers. Without this magical sleep, "hello" might not
                      # get printed because this example exits before X11 gets
                      # its handlers ready for processing events from this
                      # device.
    device.emit_click(uinput.KEY_H)
    device.emit_click(uinput.KEY_E)
    device.emit_click(uinput.KEY_L)
    device.emit_click(uinput.KEY_L)
    device.emit_click(uinput.KEY_O)
    device.destroy()

if __name__ == "__main__":
    main()
If that gives an error, just remove the "device.destroy()" line.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 4:56 pm

Destroy doesn't works. And when I use remote control and start script keyboard works on TV, no on remote screen. How it can be resolve? How make remote control mirroring?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 6:25 pm

I just installed this on my laptop. The version I installed (via pip) is not the same as the one on Github and so explains why using "with" and "destroy" don't work. Just remove the destroy line for now.

Can you explain what you're actually trying to achieve. I would have thought that the purpose of the remote desktop was that so you can use your keyboard and mouse on a different machine to control the Pi. I'm obviously missing something in your plans so please can you provide some more detail.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Uinput on raspberry pi

Mon Jul 06, 2015 6:41 pm

Yes, I delete line with destroy. It works. I want to do game device, where player can play in keyboard games, without keyboard, fron gpio buttons. And to debug this I want to use uinput, I control raspberry by remote control computer now. And when I run python script with uinput from remote control computer program the result of execution only on raspberry hdmi monitor, no on remote control computer program, And I want that result was on remote control computer program, I hope that you understand me, because English is not my native language.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Uinput on raspberry pi

Mon Jul 06, 2015 6:47 pm

It seems to me that you only want it to work on the remote desktop for debugging purposes. I don't know how to fix that but if it's working on the main pi screen then I suggest you test your uinput code when connected to that screen.

Alternatively, someone else may have an idea for you.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”