Search code examples
pythonlag

Using Keyboard module in python cause lag in keyboard


So I'm trying to make a python script that would press specific keys in a sequence to do a mechanic/stunt in a game. I'm still working on the timing etc, but the major problem I'm having is that it is causing major keyboard delay/lag once I start the file. Once the file is started anything I do in the keyboard, and it doesn't have to be the specific keystroke, any key at this point lags, it could be a problem with the loop continuing to check for the key but I'm not very sure.

Here is the code:

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

print("Press Q to execute speedflip")

mouse = Controller()

while True:  # making a loop
    try:  # used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('5'):  # if key 'q' is pressed 
            print('Speedflip 5 executed')
            mouse.press(Button.left)
            keyboard.press('w')
            #part boost-start done
            keyboard.press('w')
            keyboard.press('a')
            time.sleep(0.18)
            keyboard.release('w')
            keyboard.release('a')
            #part-position-done
            time.sleep(0.6)
            keyboard.press('w')
            keyboard.press('e')
            mouse.click(Button.right, 1)
            time.sleep(0.1)
            mouse.click(Button.right, 1)
            time.sleep(0.1)
            keyboard.release('w')
            keyboard.release('e')
            #diagonal flip done
            keyboard.press('s')
            keyboard.press('d')
            time.sleep(0.9)
            keyboard.release('s')
            keyboard.release('d')
            #part diagonal flip cancel done
            time.sleep(1)
            mouse.release(Button.left)
            keyboard.release('w')
            #boost release donewa
            
                    
        if keyboard.is_pressed('6'):
            print('Speedflip 6 is executed')
            mouse.press(Button.left)
            keyboard.press('w')
            #part boost-start done
            keyboard.press('w')
            keyboard.press('a')
            time.sleep(0.05)
            keyboard.release('w')
            keyboard.release('a')
            #part-position-done
            time.sleep(0.6)
            keyboard.press('w')
            keyboard.press('e')
            mouse.click(Button.right, 1)
            time.sleep(0.1)
            mouse.click(Button.right, 1)
            time.sleep(0.1)
            keyboard.release('w')
            keyboard.release('e')
            #diagonal flip done
            keyboard.press('s')
            keyboard.press('d')
            time.sleep(0.9)
            keyboard.release('s')
            keyboard.release('d')
            #part diagonal flip cancel done
            time.sleep(1)
            mouse.release(Button.left)
            keyboard.release('w')
            #boost release done
            
    except:
        break

Solution

  • Instead of doing infinite loops with checking pressed keys (you can't even exit this loop guessing from your code), you better add a callback when the specific key is pressed. It's even shown on the main page of keyboard package (link, look at Example section)

    def speedflip5():
        print('Speedflip 5 executed')
        mouse.press(Button.left)
        keyboard.press('w')
        # do all the stuff here ...
    
    def speedflip6():
        print('Speedflip 6 executed')
        mouse.press(Button.left)
        keyboard.press('w')
        # do all the stuff here ...
    
    import keyboard
    
    keyboard.add_hotkey('5', speedflip5)
    keyboard.add_hotkey('6', speedflip6)
    keyboard.wait()
    

    Add some condition when to stop waiting by yourself