0

I have connected a button to the GPIO on my raspberry Pi and I have tested it; it works. I have used the following code:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN)

finally:

GPIO.cleanup()

All I want is for that button to imitate a left mouse click. So that when it is pressed the pi thinks i have left clicked on the mouse.

Any help would be appreciated.

2 Answers 2

3

for mouse control, these posts can help you:

For GPIO manipulation, see this example on e-linux

You first need to monitor the state changes on the GPIO pin your button is wired to. (i.e. put it in a loop)

Then when it changes, call a function which role is to send a mouse click.
To do so, try using PyUserInput. (on the getting started part, there is an example doing a mouse click on the center of the screen).

Sign up to request clarification or add additional context in comments.

7 Comments

I have looked at both but none tell me how to make my push button register as a mouse click
well, you have to read (in a loop) your gpio state *the one with the button) and send a click through the post I've mentioned.
sorry dude. I'm a newbie and kinda need someone to tell me how to do it rather than pointing me at other articles which really don't answer the question.
I just did tell you.. make a loop reading the GPIO state on youtrbutton. When it (the GPIO state) changes, use one of the way pointed in the linked stackoverflow questions above to simulate the click. I'm sorry but it's pretty straightforward, no?
For you maybe but for me no.
|
1

So I only needed my GPIO button on pin 16 to left click a mouse button on a certain area of the screen so that I could activate an app without the Pi connected with HDMI or to a wireless keyboard.

I installed pyuserinput then used the following code:

importRPI.GPIO as GPIO
import time

from pymouse import PyMouse
m = PyMouse()

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN)

try:
   while True:
       if GPIO.input(16)!=0:m.click(300,275,1)
       time.sleep(0.2)

finally:
    GPIO.cleanup()

Through trial and error I found the right coordinates (300,275) and the 1 indicated left mouse click. I tried playing with the sleep settings to stop repeat registering of the inputted button. sleep 0.2 worked best.

1 Comment

To avoid the repetition problem, you can look for debounce. You can also have a record of the previous state, thus allowing you to do the mouse click only on GPIO state change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.