1

I have tried to search google and other websites such as GitHub but I cant find a way to detect if the right key has been pressed. I am using the two modules Keyboard and Pyautogui to make a auto clicker but all the ideas that I have come up with have failed. Here is my code:

import keyboard
import pyautogui

pyautogui.PAUSE = 0.1
while True:
    if keyboard.is_pressed('h'):
        pyautogui.rightClick()
    if keyboard.is_pressed('g'):
        pyautogui.click()

I want a way to replace the h and g with right click and left click any ideas?

DA Skull
  • 23
  • 7
  • 1
    Do you mean the left and right arrow keys on the keyboard? If you are trying to check for left and right mouse clicks why do you expect the **keyboard** module to do that for you? – QuantumMecha Oct 22 '21 at 01:54
  • I mean that if I press right click on my mouse it executes the if function. Here is a example `if keyboard.is_pressed('right click'): pyautogui.rightClick()` – DA Skull Oct 22 '21 at 22:47

1 Answers1

0

If you are trying to check for just mouse clicks, the pynput library could work.

from pynput import mouse

# mouse.Events looks at all events, you could use 
# events = mouse.Events().get(1) to look at just the events in the last second
with mouse.Events() as events:
    for event in events:
        if isinstance(event, mouse.Events.Click):
            if event.button == mouse.Button.right and event.pressed:
                #Do stuff for right click
            else:
                print('Received event {}'.format(event))

I am using Click so that movements are not tracked. Similarly, press down/up are counted as separate events, so to filter those out using event.pressed

Take a look at https://pynput.readthedocs.io/en/latest/mouse.html for other mouse listener ideas