How to hold keys down with pynput?

前端 未结 2 592
悲哀的现实
悲哀的现实 2021-01-22 20:37

I\'m using pynput and I would like to be able to hold keys down, specifically wasd but when I try and run this code it only presses the key and doesn\'t hold it for 2 seconds. I

相关标签:
2条回答
  • 2021-01-22 21:15

    You could make a two second loop. (I don't have enough reputation to comment.)

    0 讨论(0)
  • 2021-01-22 21:23

    Maybe try PyAutoGui. It is easier, and can be used within a few lines of code. I got the code from here

    >>> import pyautogui
    >>> screenWidth, screenHeight = pyautogui.size()
    >>> currentMouseX, currentMouseY = pyautogui.position()
    >>> pyautogui.moveTo(100, 150)
    >>> pyautogui.click()
    >>> pyautogui.moveRel(None, 10)  # move mouse 10 pixels down
    >>> pyautogui.doubleClick()
    >>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)  # use tweening/easing function to move mouse over 2 seconds.
    >>> pyautogui.typewrite('Hello world!', interval=0.25)  # type with quarter-second pause in between each key
    >>> pyautogui.press('esc')
    >>> pyautogui.keyDown('shift')
    >>> pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
    >>> pyautogui.keyUp('shift')
    >>> pyautogui.hotkey('ctrl', 'c')
    

    If you want to just press a key down then do

    from pyautogui import*
    from time import sleep
    keyDown("a") #pressing down key 'a'
    sleep() #how ever long you want
    keyUp("a") #stop pressing key 'a' down
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题