pynput 和 GlobalHotKeys 没有收到任何键盘事件
pynput and GlobalHotKeys not receiving any keyboard events
此代码适用于 Windows 10,但不适用于 Linux。 Linux 似乎根本没有收到任何键盘事件。当使用 Ctrl-C 在 Linux 上中断程序时,这是堆栈跟踪:
File "/home/andreas/src/magnetfeld-aux/keyboard_events.py", line 22, in <module>
key = kbd_q.get()
File "/usr/lib/python3.9/queue.py", line 171, in get
self.not_empty.wait()
File "/usr/lib/python3.9/threading.py", line 312, in wait
waiter.acquire()
KeyboardInterrupt
代码如下:
from pynput import keyboard
from queue import Queue
kbd_q = Queue(maxsize=1)
def on_activate_s():
kbd_q.put("Hotkey s")
listener = keyboard.GlobalHotKeys({
's': on_activate_s,
})
listener.start()
while True:
key = kbd_q.get()
if key:
print(key)
如何在 Linux 上将此发送到 运行?
启动侦听器后缺少的东西是 listener.wait()
。
这有效:
from pynput import keyboard
from queue import Queue
kbd_q = Queue(maxsize=1)
def on_activate_s():
kbd_q.put("Hotkey s")
listener = keyboard.GlobalHotKeys({
's': on_activate_s,
})
listener.start()
listener.wait()
while True:
key = kbd_q.get()
if key:
print(key)
此代码适用于 Windows 10,但不适用于 Linux。 Linux 似乎根本没有收到任何键盘事件。当使用 Ctrl-C 在 Linux 上中断程序时,这是堆栈跟踪:
File "/home/andreas/src/magnetfeld-aux/keyboard_events.py", line 22, in <module>
key = kbd_q.get()
File "/usr/lib/python3.9/queue.py", line 171, in get
self.not_empty.wait()
File "/usr/lib/python3.9/threading.py", line 312, in wait
waiter.acquire()
KeyboardInterrupt
代码如下:
from pynput import keyboard
from queue import Queue
kbd_q = Queue(maxsize=1)
def on_activate_s():
kbd_q.put("Hotkey s")
listener = keyboard.GlobalHotKeys({
's': on_activate_s,
})
listener.start()
while True:
key = kbd_q.get()
if key:
print(key)
如何在 Linux 上将此发送到 运行?
启动侦听器后缺少的东西是 listener.wait()
。
这有效:
from pynput import keyboard
from queue import Queue
kbd_q = Queue(maxsize=1)
def on_activate_s():
kbd_q.put("Hotkey s")
listener = keyboard.GlobalHotKeys({
's': on_activate_s,
})
listener.start()
listener.wait()
while True:
key = kbd_q.get()
if key:
print(key)