2 回答
TA贡献1784条经验 获得超7个赞
我们可以使用一个集合来检查当前正在按下哪个键。
pressing_keys_set = set()
def on_press(key):
if key not in pressing_keys_set:
print('{0} pressed'.format(key))
pressing_keys_set.add(key)
def on_release(key):
print('{0} release'.format(key))
pressing_keys_set.remove(key)
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
TA贡献2080条经验 获得超4个赞
此代码必须帮助您
from pynput.keyboard import Key, Listener
is_released=False
def on_press(key):
global is_released
if is_released:
print('{0} pressed'.format(key))
is_released=False
def on_release(key):
global is_released
print('{0} release'.format( key))
is_released=True
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
添加回答
举报