louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Mouse mover

Thu May 04, 2017 7:02 am

I am trying to create a program which will move the mouse cursor on the screen automatically using buttons connected up to the GPO pins of the Raspberry pi. However I am having trouble using uinput. Having downloaded it, when I run the code it says Uinput is not installed. Does anyone know what the issue is? Thank you in advance.

scotty101
Posts: 3866
Joined: Fri Jun 08, 2012 6:03 pm

Re: Mouse mover

Thu May 04, 2017 9:13 am

Probably to do with the version of python you are using. It is likely that you installed uinput for one version of python but not the one you are using.

Can you post the following?
A. Which version of python you are using
B. What command you used to install uinput
C. What error message you get when the code runs
D. Your code.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Re: Mouse mover

Thu May 04, 2017 4:47 pm

scotty101 wrote:Probably to do with the version of python you are using. It is likely that you installed uinput for one version of python but not the one you are using.

Can you post the following?
A. Which version of python you are using
B. What command you used to install uinput
C. What error message you get when the code runs
D. Your code.

Python 3.4.2

pip install python-uinput

ImportError: No module named uinput

The code is just in the examples folder which downloaded when I downloaded uinput and just says at the top import uinput.

User avatar
Paeryn
Posts: 2839
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Mouse mover

Thu May 04, 2017 5:13 pm

louiepi wrote:Python 3.4.2

pip install python-uinput

ImportError: No module named uinput

The code is just in the examples folder which downloaded when I downloaded uinput and just says at the top import uinput.
You need to use pip3 to install for Python3. pip installs for Python2.
She who travels light — forgot something.

louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Re: Mouse mover

Thu May 04, 2017 5:49 pm

Paeryn wrote:
louiepi wrote:Python 3.4.2

pip install python-uinput

ImportError: No module named uinput

The code is just in the examples folder which downloaded when I downloaded uinput and just says at the top import uinput.
You need to use pip3 to install for Python3. pip installs for Python2.
After I run the install command, it still says there is no module named uinput. Is there something else I need to do or have I done I think wrong.

ghp
Posts: 1466
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Mouse mover

Thu May 04, 2017 6:00 pm

Hello,
did you install with root permission ?
sudo pip install python-uinput
sudo pip3 install python-uinput

Hope this helps,
Gerhard

louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Re: Mouse mover

Thu May 04, 2017 6:31 pm

ghp wrote:Hello,
did you install with root permission ?
sudo pip install python-uinput
sudo pip3 install python-uinput

Hope this helps,
Gerhard

Thanks very much

louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Re: Mouse mover

Thu May 04, 2017 7:44 pm

I am trying to learn how uinput works but can find very little online showing how to use it. If anybody knows of somewhere to learn about it or can say how to use it then please help.
Thanks in advance

User avatar
Paeryn
Posts: 2839
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Mouse mover

Thu May 04, 2017 9:02 pm

At its basic, you create a device with a list (or tuple) saying what event types you want to be able to send, e.g.

Code: Select all

    events = (uinput.REL_X, uinput.REL_Y,)
    device = uinput.Device(events)
Then when you want to send an event pass the device the event type and data, e.g. move mouse right 5

Code: Select all

    device.emit(uinput.REL_X, 5)
If you want to send both a move x and move y you can make two calls back-to-back with the first having syn=False

Code: Select all

    device.emit(uinput.REL_X, 5, syn=False)
    device.emit(uinput.REL_Y, 5)
When you've finished with it destroy the device

Code: Select all

    device.destroy()
Alternatively do the creating in a with statement and it will automatically get destroyed when it goes out of scope.

Code: Select all

    events = (uinput.REL_X, uinput.REL_Y,)
    with uinput.Device(events) as device:
        device.emit(uinput.REL_X, 5)
Mouse events are sent using emit(), keyboard & joystick events are sent using emit_click() (single key presses) and emit_combo() (multiple key presses at the same time).

By default you generally need to be running as root to create a connection to uinput.
She who travels light — forgot something.

louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Re: Mouse mover

Fri May 05, 2017 2:08 pm

Paeryn wrote:At its basic, you create a device with a list (or tuple) saying what event types you want to be able to send, e.g.

Code: Select all

    events = (uinput.REL_X, uinput.REL_Y,)
    device = uinput.Device(events)
Then when you want to send an event pass the device the event type and data, e.g. move mouse right 5

Code: Select all

    device.emit(uinput.REL_X, 5)
If you want to send both a move x and move y you can make two calls back-to-back with the first having syn=False

Code: Select all

    device.emit(uinput.REL_X, 5, syn=False)
    device.emit(uinput.REL_Y, 5)
When you've finished with it destroy the device

Code: Select all

    device.destroy()
Alternatively do the creating in a with statement and it will automatically get destroyed when it goes out of scope.

Code: Select all

    events = (uinput.REL_X, uinput.REL_Y,)
    with uinput.Device(events) as device:
        device.emit(uinput.REL_X, 5)
Mouse events are sent using emit(), keyboard & joystick events are sent using emit_click() (single key presses) and emit_combo() (multiple key presses at the same time).

By default you generally need to be running as root to create a connection to uinput.

Thank you very much, Please could you give an example of this all in 1 piece of code, where when it receives an input through one of the GPIO pins, it moves the mouse to the left, and if held down will continue to do so. I am going to do this for 4 buttons, so the mouse can move in 4 directions. Please could you also say how to trigger a mouse click, both left and right, so that when it receives an input on one of the GPIO pins it will trigger a mouse click.

User avatar
Paeryn
Posts: 2839
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Mouse mover

Fri May 05, 2017 4:08 pm

louiepi wrote:Thank you very much, Please could you give an example of this all in 1 piece of code, where when it receives an input through one of the GPIO pins, it moves the mouse to the left, and if held down will continue to do so. I am going to do this for 4 buttons, so the mouse can move in 4 directions. Please could you also say how to trigger a mouse click, both left and right, so that when it receives an input on one of the GPIO pins it will trigger a mouse click.
The main mouse buttons are uinput.BTN_LEFT, uinput.BTN_MIDDLE and uinput.BTN_RIGHT, there are other mouse buttons too, they are all listed in the source code to python-uinput.

As to full code, what have you tried? There are plenty of examples around that show how to read the GPIOs.
She who travels light — forgot something.

louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Re: Mouse mover

Fri May 05, 2017 4:50 pm

Paeryn wrote:
louiepi wrote:Thank you very much, Please could you give an example of this all in 1 piece of code, where when it receives an input through one of the GPIO pins, it moves the mouse to the left, and if held down will continue to do so. I am going to do this for 4 buttons, so the mouse can move in 4 directions. Please could you also say how to trigger a mouse click, both left and right, so that when it receives an input on one of the GPIO pins it will trigger a mouse click.
The main mouse buttons are uinput.BTN_LEFT, uinput.BTN_MIDDLE and uinput.BTN_RIGHT, there are other mouse buttons too, they are all listed in the source code to python-uinput.

As to full code, what have you tried? There are plenty of examples around that show how to read the GPIOs.
Thank you for the help. I unspderstand how to get input from the GPIO pins but I'm not sure how to use them to trigger an event using uinput

ghp
Posts: 1466
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Mouse mover

Fri May 05, 2017 6:02 pm


louiepi
Posts: 112
Joined: Mon Mar 13, 2017 6:54 pm

Re: Mouse mover

Sat May 06, 2017 6:20 am

Please could you show me how to use the uinput, as I understand how to get inputs fromGPIO pins, but don't know how to use uinput. Thanks.

nokel
Posts: 4
Joined: Wed Jul 12, 2017 1:31 pm

Re: Mouse mover

Wed Jul 12, 2017 1:54 pm

Hi, I tried using the uinput as a "send enter key" kind of command, but I'm getting back a syntax error for some reason despite what I'm pretty sure is following the rules properly, what I've got as the code is as follows;

Code: Select all

import RPi.GPIO as gpio
import time
import uinput
device = uinput.Device(uinput.KEY_ENTER)
GPIO.setmode(BCM)
GPIO.setup(26, gpio.IN, pull_up_down=gpio.PUD_UP)
buttonReleased = True
while buttonReleased:
        gpio.wait_for_edge(26, gpio.FALLING)
        if (GPIO.Input(26)==False
        device.emit_click(input.KEY_ENTER)
        time.sleep(0.01)

What I'm trying to do is get the enter button to be pressed when I press the button on GPIO 26
what I get instead is a syntax error saying that "device" is invalid syntax though? (but it doesn't matter if I change it to anything else either, so I don't know what's wrong despite following the uinput example... I'm really confused!

User avatar
Paeryn
Posts: 2839
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Mouse mover

Wed Jul 12, 2017 4:42 pm

nokel wrote:

Code: Select all

while buttonReleased:
        gpio.wait_for_edge(26, gpio.FALLING)
        if (GPIO.Input(26)==False
        device.emit_click(input.KEY_ENTER)
        time.sleep(0.01)
What I'm trying to do is get the enter button to be pressed when I press the button on GPIO 26
what I get instead is a syntax error saying that "device" is invalid syntax though? (but it doesn't matter if I change it to anything else either, so I don't know what's wrong despite following the uinput example... I'm really confused!
Your if line has a superfluous open bracket and no colon at the end. And the line(s) under it isn't indented.

Code: Select all

while buttonReleased:
        gpio.wait_for_edge(26, gpio.FALLING)
        if GPIO.Input(26)==False:
            device.emit_click(input.KEY_ENTER)
        time.sleep(0.01)
She who travels light — forgot something.

nokel
Posts: 4
Joined: Wed Jul 12, 2017 1:31 pm

Re: Mouse mover

Thu Jul 13, 2017 3:20 pm

Oh, thanks! I don't really know why that bracket was there in the first place but that specific error seems to have gone now.

However, a new issue seems to have appeared;

Code: Select all

Traceback (most recent call last):
  File "GPIO4buttons.py", line 4, in <module>
    device = uinput.Device(uinput.KEY_ENTER)
  File "/usr/local/lib/python2.7/dist-packages/uinput/__init__.py", line 180, in __init__
    ev_type, ev_code = ev_spec[:2]
TypeError: 'int' object has no attribute '__getitem__'
as an end result I'd like to use this program in the background so I can play music from ncmpcpp

User avatar
Paeryn
Posts: 2839
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Mouse mover

Thu Jul 13, 2017 3:31 pm

You have to pass a sequence (e.g. a tuple or list) to uinput.Device() not an integer.

Code: Select all

device = uinput.Device( (uinput.KEY_ENTER,) )
Note you have to put a comma inside the brackets to create a tuple with only one item.
She who travels light — forgot something.

nokel
Posts: 4
Joined: Wed Jul 12, 2017 1:31 pm

Re: Mouse mover

Fri Jul 14, 2017 12:17 pm

Ok, so it's been a little touch and go after what you told me to do and now I've gotten it to detect the button press and now it's saying;

Code: Select all

Traceback (most recent call last):
  File "GPIO4buttons.py", line 10, in <module>
    if gpio.Input(26, gpio.BCM)==False:
AttributeError: 'module' object has no attribute 'Input'
the code is basically the same but slightly different

Code: Select all

import RPi.GPIO as gpio
import time
import uinput
device = uinput.Device( (uinput.KEY_ENTER,) )
gpio.setmode(gpio.BCM)
gpio.setup(26, gpio.IN, pull_up_down=gpio.PUD_UP)
buttonReleased = True
while buttonReleased:
        gpio.wait_for_edge(26, gpio.FALLING)
        if gpio.Input(26, gpio.BCM)==False:
                device.emit_click(input.KEY_ENTER)
        time.sleep(0.01)
also for some reason I've got to type in "modprobe uinput" before it gets to a point where it'll recognise a button push... I thought putting "import uinput" would activate it, but apparently not?

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

Re: Mouse mover

Fri Jul 14, 2017 12:36 pm

python is case sensitive. Try gpio.input
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Paeryn
Posts: 2839
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Mouse mover

Fri Jul 14, 2017 12:39 pm

Check the documentation for RPi.GPIO, the function name is input, no capital letter.

As to having to modprobe uinput, I've never had to do that myself (not that I generally use uinput).
She who travels light — forgot something.

nokel
Posts: 4
Joined: Wed Jul 12, 2017 1:31 pm

Re: Mouse mover

Fri Jul 14, 2017 2:02 pm

Sweet! It's working :D

So now if I want to add three more buttons does that mean I need to add three more while if phrases? or can I use the current one and two more ifs?

(also appologies for my noobishness, the last time I did python was about 9 years ago and it wasn't a very long lesson, so I'm basically completely new to it)

Also thanks a lot for bearing with me!

Return to “Python”