Detect if key was pressed once

后端 未结 2 1887
有刺的猬
有刺的猬 2021-01-28 21:49

I wanted to do an action, as soon as my f key is pressed. The problem is that it spams the action.

import win32api

while True:
    f_keystate = win32         


        
2条回答
  •  长发绾君心
    2021-01-28 22:08

    You need a state variable that will keep track of whether the key has been pressed after it's been released.

    f_pressed = False
    
    while not f_pressed:
        f_pressed = win32api.GetAsyncKeyState(0x46) < 0
    
    print("Pressed!")
    

提交回复
热议问题