在Linux机器上检测python 3中按键的最简单方法是什么? [英] What is the easiest way to detect key presses in python 3 on a linux machine?

查看:359
本文介绍了在Linux机器上检测python 3中按键的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在尝试用覆盆子pi和一个makey makey编写一个小代码.该makey makey是一个小板,当某些触点通电时,它充当usb键盘.我的问题是检测python脚本中的那些按键的最简单方法是什么.我知道使用GPIO引脚会更容易,但是现在我正在寻找它.我已经看到了一些示例,例如使用pygame.key和getKey从msvcrt使用getch()(据我所知仅是Windows).哪个论文最容易使用?是否有任何可以检测到按键被按下和按键被释放的信号?

Right now I'm trying to make a small code with a raspberry pi and and a makey makey. The makey makey is a small board that acts as a usb keyboard when certain contacts are powered. My question is what is the easiest way to detect those keypresses inside a python script. I understand using the GPIO pins would be easier, but right now I'm looking for this. I have seen examples such as using using getch() from msvcrt (which from what I understand is windows only,) using pygame.key, and using getKey. Which of theses is easiest to use? Are there any that can detect a key being pressed and a key being released?

伪代码(...就是所谓的吗?)

Pseudocode Code (... is that what it's called?)

import whatever needs importing    

if the "W" key is pressed:
   print ("You pressed W")

elif the "S" is pressed:
    print ("You pressed S")

,依此类推.谢谢.

推荐答案

这是一个简单的循环,会将stdin置于原始模式(禁用缓冲,因此您不必按Enter)即可获取单个字符.您应该做一些更聪明的事情(例如使用with语句将其禁用),但是您在这里有了主意:

This is a simple loop that will put stdin in raw mode (disabling buffering so you don't have to press enter) to get single characters. You should do something smarter (like a with statement to disable it) but you get the idea here:

import tty
import sys
import termios

orig_settings = termios.tcgetattr(sys.stdin)

tty.setcbreak(sys.stdin)
x = 0
while x != chr(27): # ESC
    x=sys.stdin.read(1)[0]
    print("You pressed", x)

termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings)    

我认为您必须循环以检测Python中的关键发布.

I think you'd have to loop to detect key releases in Python.

ETA的更多解释:

在Linux上,程序的输入将行缓冲.这意味着操作系统将缓冲输入直到它整行,因此您的程序甚至在用户也按"enter"之前都看不到用户键入的任何内容.换句话说,如果您的程序期望用户键入"w"并且用户执行此操作,则"w"将坐在操作系统的缓冲区中,直到用户单击"enter"为止.此时,整行都已传递到您的程序,因此您将获得字符串"w \ n"作为用户输入.

On Linux, input to your program will be line buffered. This means that the operating system will buffer up input until it has a whole line, so your program won't even see anything the user typed until the user also hits 'enter'. In other words, if your program is expecting the user to type 'w' and the user does this, 'w' will be sitting in the OS's buffer until the user hits 'enter'. At this point the entire line is delivered to your program so you will get the string "w\n" as the user's input.

您可以通过将tty置于原始模式来禁用此功能.您可以使用Python函数tty.setcbreak进行此操作,该函数将在Linux中调用tty驱动程序以告知其停止缓冲.我向它传递了sys.stdin参数,以告诉它要关闭 1 的缓冲的流.因此,在tty.setcbreak调用之后,上面的循环将为您提供用户按下的每个键的输出.

You can disable this by putting the tty in raw mode. You do this with the Python function tty.setcbreak which will make a call down the tty driver in linux to tell it to stop buffering. I passed it the sys.stdin argument to tell it which stream I wanted to turn buffering off for1. So after the tty.setcbreak call, the loop above will give you output for every key the user presses.

一个复杂的问题是,一旦程序退出,tty仍处于原始模式.通常您会感到不满意,因为您没有获得现代终端设置所提供的任何功能(例如,使用控制或转义序列时).例如,请注意您可能无法通过ctrl-C退出程序.因此,一旦您阅读完输入字符,就应该将终端重新设置为 cooked模式. termios.tcsetattr调用只是说将终端放回我找到它的方式".它知道如何执行此操作,方法是首先在程序的开头调用termios.tcgetattr,说告诉我终端的所有当前设置".

A complication, though, is that once your program exits, the tty is still in raw mode. You'll generally find this unsatisfying since you don't get any of the power that modern terminal settings offer (like when you use control or escape sequences). For example, notice that you might have trouble exiting the program with ctrl-C. Consequently you should put the terminal back into cooked mode once you are done reading input characters. The termios.tcsetattr call simply says "put the terminal back the way I found it". It knows how to do this by first calling termios.tcgetattr at the beginning of the program which is saying "tell me all the current settings for the terminal".

一旦了解了所有这些,就应该能够轻松地将功能封装在适合您程序的功能中.

Once you understand all that, you should easily be able to encapsulate the functionality in a function that suits your program.

1 stdin是输入来自用户的流. Wikipedia可以告诉您有关标准流的更多信息.

1 stdin is the stream that input comes to you from the user. Wikipedia can tell you more about standard streams.

这篇关于在Linux机器上检测python 3中按键的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆