Python 中的关键监听器?
- 2025-03-04 08:27:00
- admin 原创
- 69
问题描述:
有没有办法在 python 中执行按键监听器而不需要诸如此类的庞大臃肿的模块pygame
?
例如,当我按下a
键时,它会打印到控制台
按下了a键!
它还应该监听箭头键/空格键/Shift 键。
解决方案 1:
我正在寻找一种没有窗口焦点的简单解决方案。Jayk 的答案pynput
对我来说非常完美。下面是我使用它的示例。
from pynput import keyboard
def on_press(key):
if key == keyboard.Key.esc:
return False # stop listener
try:
k = key.char # single-char keys
except:
k = key.name # other keys
if k in ['1', '2', 'left', 'right']: # keys of interest
# self.keys.append(k) # store it in global-like variable
print('Key pressed: ' + k)
return False # stop listener; remove this if want more keys
listener = keyboard.Listener(on_press=on_press)
listener.start() # start to listen on a separate thread
listener.join() # remove if main thread is polling self.keys
解决方案 2:
不幸的是,这并不容易。如果您尝试制作某种文本用户界面,您可能需要研究一下curses
。如果您想像在终端中一样显示内容,但又想要这样的输入,那么您必须使用termios
,不幸的是,Python 中似乎没有太多文档记录。然而,不幸的是,这两个选项都不是那么简单。此外,它们在 Windows 下不起作用;如果您需要它们在 Windows 下工作,您必须使用PDCurses来替代curses
或pywin32而不是termios
。
我能够让它正常工作。它会打印出您键入的键的十六进制表示。正如我在问题评论中所说,箭头很棘手;我想你会同意的。
#!/usr/bin/env python
import sys
import termios
import contextlib
@contextlib.contextmanager
def raw_mode(file):
old_attrs = termios.tcgetattr(file.fileno())
new_attrs = old_attrs[:]
new_attrs[3] = new_attrs[3] & ~(termios.ECHO | termios.ICANON)
try:
termios.tcsetattr(file.fileno(), termios.TCSADRAIN, new_attrs)
yield
finally:
termios.tcsetattr(file.fileno(), termios.TCSADRAIN, old_attrs)
def main():
print 'exit with ^C or ^D'
with raw_mode(sys.stdin):
try:
while True:
ch = sys.stdin.read(1)
if not ch or ch == chr(4):
break
print '%02x' % ord(ch),
except (KeyboardInterrupt, EOFError):
pass
if __name__ == '__main__':
main()
解决方案 3:
有一种方法可以在 Python 中实现按键监听。此功能可通过pynput实现。
命令行:
$ pip install pynput
Python代码:
from pynput import keyboard
# your code here
解决方案 4:
在 Windows 上可以这样做:
"""
Display series of numbers in infinite loop
Listen to key "s" to stop
Only works on Windows because listening to keys
is platform dependent
"""
# msvcrt is a windows specific native module
import msvcrt
import time
# asks whether a key has been acquired
def kbfunc():
#this is boolean for whether the keyboard has bene hit
x = msvcrt.kbhit()
if x:
#getch acquires the character encoded in binary ASCII
ret = msvcrt.getch()
else:
ret = False
return ret
#begin the counter
number = 1
#infinite loop
while True:
#acquire the keyboard hit if exists
x = kbfunc()
#if we got a keyboard hit
if x != False and x.decode() == 's':
#we got the key!
#because x is a binary, we need to decode to string
#use the decode() which is part of the binary object
#by default, decodes via utf8
#concatenation auto adds a space in between
print ("STOPPING, KEY:", x.decode())
#break loop
break
else:
#prints the number
print (number)
#increment, there's no ++ in python
number += 1
#wait half a second
time.sleep(0.5)
解决方案 5:
键盘
sudo pip install keyboard
使用这个小型 Python 库完全控制您的键盘。挂钩全局事件、注册热键、模拟按键等等。
所有键盘上的全局事件挂钩(无论焦点如何都可捕获按键)。监听并发送键盘事件。适用于 Windows 和 Linux(需要 sudo),并具有实验性的 OS X 支持(感谢 @glitchassassin!)。纯 Python,无需编译 C 模块。零依赖。安装和部署很简单,只需复制文件即可。Python 2 和 3。复杂热键支持(例如 Ctrl+Shift+M、Ctrl+Space),超时可控。包括高级 API(例如录制和播放、add_abbreviation)。按按键在布局中的实际位置进行映射,并具有完整的国际化支持(例如 Ctrl+ç)。事件自动在单独的线程中捕获,不会阻塞主程序。经过测试和记录。不会破坏重音死键(我正在看你,pyHook)。可通过项目鼠标(pip install mouse)获得鼠标支持。
来自README.md:
import keyboard
keyboard.press_and_release('shift+s, space')
keyboard.write('The quick brown fox jumps over the lazy dog.')
# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))
# Blocks until you press esc.
keyboard.wait('esc')
# Record events until 'esc' is pressed.
recorded = keyboard.record(until='esc')
# Then replay back at three times the speed.
keyboard.play(recorded, speed_factor=3)
# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation('@@', 'my.long.email@example.com')
# Block forever.
keyboard.wait()
解决方案 6:
虽然我喜欢使用键盘模块来捕获键盘事件,但我不喜欢它的record()
功能,因为它返回一个数组[KeyboardEvent("A"), KeyboardEvent("~")]
,我觉得很难读懂。因此,为了记录键盘事件,我喜欢同时使用键盘模块和线程模块,如下所示:
import keyboard
import string
from threading import *
# I can't find a complete list of keyboard keys, so this will have to do:
keys = list(string.ascii_lowercase)
"""
Optional code(extra keys):
keys.append("space_bar")
keys.append("backspace")
keys.append("shift")
keys.append("esc")
"""
def listen(key):
while True:
keyboard.wait(key)
print("[+] Pressed",key)
threads = [Thread(target=listen, kwargs={"key":key}) for key in keys]
for thread in threads:
thread.start()
解决方案 7:
我喜欢使用Pynput。它有很多选项,可以提供更简单、更优雅的解决方案。
例子:
from pynput import keyboard
def on_activate_a():
print('A pressed')
def on_activate_b():
print('B pressed')
def on_activate_c():
print('C pressed')
def quit():
print('QUIT')
h.stop()
with keyboard.GlobalHotKeys({
'a': on_activate_a,
'b': on_activate_b,
'c': on_activate_c,
'<ctrl>+c': quit}) as h:
h.join()
扫码咨询,免费领取项目管理大礼包!