如何避免 "on_press" 侦听器事件循环?
How to avoid loop on "on_press" listener event?
我正在写一段非常简单的代码。
- 我想听键盘。
- 当我按 F11 时,消息 "Activated..." 必须显示一次。
- 当我再次按 F11 时,消息 "Deactivated..." 必须显示,而且只显示一次。
- 等等...
- 当我按下 F12 时,我希望监听器停止。
这是我的一段代码,当我按 F11 时,我很难找到正确的实现来避免常量循环 ("Activated... Deactivated... Activated... Deactivated...")。
此外,欢迎任何关于更好的实现、更先进、更 pythonic 的建议!
# -*- coding: utf-8 -*-
from pynput import keyboard
is_active = False
def on_press(key):
global is_active
# Activate/Deactivate when pressing F11
if key == keyboard.Key.f11:
if is_active:
is_active = False
print("Deactivate...")
else:
is_active = True
print("Activate...")
# Stop "on_press" listener
if key == keyboard.Key.f12:
return False
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
您将要使用
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
When you hold down on a key you will get multiple events just like if you hold down the aaaaaaaaaaaaaaaaaaaaaaaaaaa key without letting up.您需要设置一个锁定机制,在第一次按下时您不会让它再次发生,直到您释放键。
修改后的代码:
# -*- coding: utf-8 -*-
from pynput import keyboard
is_active = False
is_f11_pressed = False
def on_press(key):
global is_f11_pressed
global is_active
# Activate/Deactivate when pressing F11
if key == keyboard.Key.f11 and not is_f11_pressed:
is_f11_pressed = True
if is_active:
is_active = False
print("Deactivate...")
else:
is_active = True
print("Activate...")
# Stop "on_press" listener
if key == keyboard.Key.f12:
return False
def on_release(key):
global is_f11_pressed
if key == keyboard.Key.f11:
is_f11_pressed = False
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
我正在写一段非常简单的代码。
- 我想听键盘。
- 当我按 F11 时,消息 "Activated..." 必须显示一次。
- 当我再次按 F11 时,消息 "Deactivated..." 必须显示,而且只显示一次。
- 等等...
- 当我按下 F12 时,我希望监听器停止。
这是我的一段代码,当我按 F11 时,我很难找到正确的实现来避免常量循环 ("Activated... Deactivated... Activated... Deactivated...")。
此外,欢迎任何关于更好的实现、更先进、更 pythonic 的建议!
# -*- coding: utf-8 -*-
from pynput import keyboard
is_active = False
def on_press(key):
global is_active
# Activate/Deactivate when pressing F11
if key == keyboard.Key.f11:
if is_active:
is_active = False
print("Deactivate...")
else:
is_active = True
print("Activate...")
# Stop "on_press" listener
if key == keyboard.Key.f12:
return False
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
您将要使用
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
When you hold down on a key you will get multiple events just like if you hold down the aaaaaaaaaaaaaaaaaaaaaaaaaaa key without letting up.您需要设置一个锁定机制,在第一次按下时您不会让它再次发生,直到您释放键。
修改后的代码:
# -*- coding: utf-8 -*-
from pynput import keyboard
is_active = False
is_f11_pressed = False
def on_press(key):
global is_f11_pressed
global is_active
# Activate/Deactivate when pressing F11
if key == keyboard.Key.f11 and not is_f11_pressed:
is_f11_pressed = True
if is_active:
is_active = False
print("Deactivate...")
else:
is_active = True
print("Activate...")
# Stop "on_press" listener
if key == keyboard.Key.f12:
return False
def on_release(key):
global is_f11_pressed
if key == keyboard.Key.f11:
is_f11_pressed = False
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()