Page 1 of 1

Mouse mover

Posted: Thu May 04, 2017 7:02 am
by louiepi
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.

Re: Mouse mover

Posted: Thu May 04, 2017 9:13 am
by scotty101
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.

Re: Mouse mover

Posted: Thu May 04, 2017 4:47 pm
by louiepi
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.

Re: Mouse mover

Posted: Thu May 04, 2017 5:13 pm
by Paeryn
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.

Re: Mouse mover

Posted: Thu May 04, 2017 5:49 pm
by louiepi
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.

Re: Mouse mover

Posted: Thu May 04, 2017 6:00 pm
by ghp
Hello,
did you install with root permission ?
sudo pip install python-uinput
sudo pip3 install python-uinput

Hope this helps,
Gerhard

Re: Mouse mover

Posted: Thu May 04, 2017 6:31 pm
by louiepi
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

Re: Mouse mover

Posted: Thu May 04, 2017 7:44 pm
by louiepi
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

Re: Mouse mover

Posted: Thu May 04, 2017 9:02 pm
by Paeryn
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.

Re: Mouse mover

Posted: Fri May 05, 2017 2:08 pm
by louiepi
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.

Re: Mouse mover

Posted: Fri May 05, 2017 4:08 pm
by Paeryn
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.

Re: Mouse mover

Posted: Fri May 05, 2017 4:50 pm
by louiepi
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

Re: Mouse mover

Posted: Fri May 05, 2017 6:02 pm
by ghp

Re: Mouse mover

Posted: Sat May 06, 2017 6:20 am
by louiepi
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.

Re: Mouse mover

Posted: Wed Jul 12, 2017 1:54 pm
by nokel
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!

Re: Mouse mover

Posted: Wed Jul 12, 2017 4:42 pm
by Paeryn
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)

Re: Mouse mover

Posted: Thu Jul 13, 2017 3:20 pm
by nokel
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

Re: Mouse mover

Posted: Thu Jul 13, 2017 3:31 pm
by Paeryn
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.

Re: Mouse mover

Posted: Fri Jul 14, 2017 12:17 pm
by nokel
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?

Re: Mouse mover

Posted: Fri Jul 14, 2017 12:36 pm
by elParaguayo
python is case sensitive. Try gpio.input

Re: Mouse mover

Posted: Fri Jul 14, 2017 12:39 pm
by Paeryn
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).

Re: Mouse mover

Posted: Fri Jul 14, 2017 2:02 pm
by nokel
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!