Python跨平台监听按键?

Python跨平台监听按键?,python,keyboard-events,Python,Keyboard Events,我需要监听python终端程序中的某些按键,而不需要使用raw\u input暂停执行。我见过人们使用一些特定于windows的方式来监听击键,我见过人们使用tkinter和pygame等大型模块,我希望避免使用这些模块 有没有一个轻量级的模块可以跨平台(至少是ubuntu、windows、mac)运行?或者有没有一种方法只使用tkinter、pygame等的事件系统 如果没有,我应该如何处理这个问题?我的第一个想法是将stdin重定向到另一个进程,并不断检查它是否包含我的一个事件键 编辑 感

我需要监听python终端程序中的某些按键,而不需要使用
raw\u input
暂停执行。我见过人们使用一些特定于windows的方式来监听击键,我见过人们使用tkinter和pygame等大型模块,我希望避免使用这些模块

有没有一个轻量级的模块可以跨平台(至少是ubuntu、windows、mac)运行?或者有没有一种方法只使用tkinter、pygame等的事件系统

如果没有,我应该如何处理这个问题?我的第一个想法是将stdin重定向到另一个进程,并不断检查它是否包含我的一个事件键


编辑
感谢@unutbu花时间将这个3年前的问题标记为另一个问题的副本,该问题的答案不适用于这个问题,因为我专门询问了一个非阻塞解决方案。

我不知道有哪个跨平台的轻量级模块监听按键。但这里有一个建议,以防您想要实现一些简单的东西:

在Python常见问题解答中查看上的这个问题。您可以尝试一下从
sys.stdin
threading
阻塞读取。但这可能只适用于Unix。在Windows上,您可以使用

将Python常见问题解答中的按键配方与
msvcrt
模块相结合,生成的
kbhit
函数如下所示:

try:
    from msvcrt import kbhit
except ImportError:
    import termios, fcntl, sys, os
    def kbhit():
        fd = sys.stdin.fileno()
        oldterm = termios.tcgetattr(fd)
        newattr = termios.tcgetattr(fd)
        newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
        termios.tcsetattr(fd, termios.TCSANOW, newattr)
        oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
        fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
        try:
            while True:
                try:
                    c = sys.stdin.read(1)
                    return True
                except IOError:
                    return False
        finally:
            termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
            fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
简短回答:没有 按键取决于系统。他们是受干扰驱动的。它们是大多数现代操作系统的基本功能之一。它们有不同的理念,不能以通用的方式统一而不丧失功能

你可以试试- termios=unix,posix风格的文件描述符驱动

诅咒=门户终端样式处理(这是一种特定的基于控制台的范例,不是通用的)

Python封装了可能来自键盘的某些输入类:例如,控制台inut的sys.stdin


但是,试图获得通用键盘输入是一个非常普遍的问题,它本质上依赖于平台。

以下是如何在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)

def kbhit():。。。return sys.stdin.read(1)
+1根据您的回答,我能够完成本例中所需的内容。谢谢。-1这不包含与
msvcrt
模块的
kbhit()。这是因为它基于Python常见问题解答中的代码,用于一次按一个键。当一个角色可用时,你的改编会这样做,然后丢弃读取的角色,因此它会丢失。一旦你导入了kbhit,你会怎么做?如何使用它?EI capitan 10.11.3(15D21),我得到了这个错误:termios.error:(25,“设备的ioctl不合适”)+1感谢它的帮助解释。