-1

how do I trigger the mouse being clicked rapidly when a key is held down (shift key in this instance). My code is currently

import win32api                               
import win32con                               
import time                                   
from pynput.mouse import Button, Controller   
mouse = Controller()                          
while True:                                  
    keystate = win32api.GetAsyncKeyState(0x10)                          
    if keystate < 0:                          
        mouse.click(Button.left, 2)                         
    else:                                     
        pass                                  

The issue I'm having is everytime I actually press the shift key the program crashes. When printing out the keystate it shows 0 until shift is pressed in which it shows -32767 and then it stops printing and I have to kill the process. How do I stop this from happening.

Bob Stone
  • 90
  • 1
  • 13
  • maybe you should use `pynput.mouse.Listener` to catch when `shift` it pressed ? – furas Sep 02 '20 at 00:04
  • program runs very fast and it can check keys very many times in one second - so it may click button very many times in one second and it can make problem. You may have to use some variables to control if button was pressed only once. – furas Sep 02 '20 at 00:07
  • code `else: pass` is useless - you can remove it. – furas Sep 02 '20 at 00:09
  • you may also try to use second variable which remeber `previous_keystate` and click button only when `previous_keystate` is `0` - and at the end of loop always use `previous_keystate = keystate`. This way it should click button only once when you press shift and when you keep it pressed. – furas Sep 02 '20 at 00:10

1 Answers1

0

I don't use Windows but Linux so I can't check it but program runs very fast and it runs loop a lot of times in one second, and it checks shift a lot of times in one second, and it click button a lot of times in one second, and this may make problem.

You can use second variable to remeber previous_keystate and click button only when previous_keystate == 0

import win32api                               
import win32con                               
import time                                   
from pynput.mouse import Button, Controller, Listener

mouse = Controller()

previous_keystate = 0

while True:                                  
    keystate = win32api.GetAsyncKeyState(0x10)  
                        
    if previous_keystate == 0 and keystate < 0:                          
        mouse.click(Button.left, 2)

    previous_keystate = keystate

BTW: as I rember in documentation you can find information that click(Button.left, 2) may not work on some systems.


I would use pynput.keyboard.Listener to get pressed key - it execute function only once when key is pressed (and not repeate function when key is keep pressed). And it works also on Linux.

from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Listener

# --- functions ---

def on_press(key):
    #print(key)
    
    if key == Key.shift:
        mouse.click(Button.left, 2)
        
    # to stop on ESC    
    if key == Key.esc:
        return False

# --- main ---
            
mouse = Controller()

with Listener(on_press=on_press) as listener:
    # ... some code ...
    listener.join()

or

# --- main ---
            
mouse = Controller()

listener = Listener(on_press=on_press)
listener.start()
# ... some code ...
listener.join()
furas
  • 134,197
  • 12
  • 106
  • 148