Python 中的关键监听器?

2025-03-04 08:27:00
admin
原创
69
摘要:问题描述:有没有办法在 python 中执行按键监听器而不需要诸如此类的庞大臃肿的模块pygame?例如,当我按下a键时,它会打印到控制台按下了a键!它还应该监听箭头键/空格键/Shift 键。解决方案 1:我正在寻找一种没有窗口焦点的简单解决方案。Jayk 的答案pynput对我来说非常完美。下面是我使用它...

问题描述:

有没有办法在 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:

键盘

使用这个小型 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()
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3983  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2747  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   82  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   90  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   79  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用