检测键是否被按下一次 [英] Detect if key was pressed once
本文介绍了检测键是否被按下一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做一个动作,只要按下我的 f 键.问题是它会向操作发送垃圾邮件.
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 = win32api.GetAsyncKeyState(0x46)
if f_keystate < 0:
print("Pressed!")
我想要这个没有按下!"被垃圾邮件但只打印一次.
I would like this without "Pressed!" being spammed but only printed once.
推荐答案
因为在 while
循环中,当按下 f 键时,GetAsyncKeyState
将检测到 f 键始终处于按下状态.结果,重复调用 print
语句.
Because in the while
loop, when the f key is pressed, GetAsyncKeyState
will detect that the f key is always in the pressed state. As a result, the print
statement is called repeatedly.
试试下面的代码,你会得到你想要的结果:
Try the following code, you will get the result you want:
import win32api
while True:
keystate = win32api.GetAsyncKeyState(0x46)&0x0001
if keystate > 0:
print('F pressed!')
这篇关于检测键是否被按下一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文