linux下使用python捕获键盘输入_在Linux机器上检测python 3中按键的最简单方法是什么?...

本文介绍如何在Linux环境下使用Python实时检测并响应键盘输入,特别是通过makey makey板。通过将stdin设置为原始模式,可以实现无缓冲的单字符读取,从而在按下或释放键时获取反馈。示例代码展示了如何监听特定按键如"W"和"S",并且讨论了在程序结束时恢复终端正常模式的重要性。
摘要由CSDN通过智能技术生成

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")

and so on. Thanks.

解决方案

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)

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

ETA some more explanation:

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.

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.

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 is the stream that input comes to you from the user. Wikipedia can tell you more about standard streams.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值