python实时检测键盘输入函数的示例
作者:Complicated321 时间:2023-01-27 19:19:28
在嵌入式、尤其是机器人的python编程中,经常需要实时检测用户的键盘输入来随时控制机器人,这段代码可以帮助我们提取用户输入的字符,并在按下键盘的时候作出反应。
import sys
import tty
import termios
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def readkey(getchar_fn=None):
getchar = getchar_fn or readchar
c1 = getchar()
if ord(c1) != 0x1b:
return c1
c2 = getchar()
if ord(c2) != 0x5b:
return c1
c3 = getchar()
return chr(0x10 + ord(c3) - 65)
while True:
key=readkey()
if key=='w':
#go_forward()
if key=='a':
#go_back()
if key=='s':
#go_left()
if key=='d':
#go_right()
if key=='q':
break
key = readkey()即可使用
来源:https://blog.csdn.net/qq_40930675/article/details/84667762
标签:python,检测,键盘,输入
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
命令行传递参数argparse.ArgumentParser的使用解析
2023-09-09 12:28:17
延时重复执行函数 lLoopRun.js
2024-05-13 09:20:24
win10从零安装配置pytorch全过程图文详解
2022-07-01 20:54:55
![](https://img.aspxhome.com/file/2023/1/105401_0s.png)
深入了解vue-router原理并实现一个小demo
2024-04-30 10:25:31
![](https://img.aspxhome.com/file/2023/5/130215_0s.jpg)
JavaScript中layim之整合右键菜单的示例代码
2024-04-18 09:42:29
![](https://img.aspxhome.com/file/2023/2/136292_0s.png)
基于Python实现RLE格式分割标注文件的格式转换
2022-10-22 08:41:12
![](https://img.aspxhome.com/file/2023/5/87935_0s.png)
php析构函数的具体用法小结
2024-04-23 09:20:31
用js格式化金额可设置保留的小数位数
2024-05-21 10:20:44
python解析.pyd文件的详细代码
2022-08-04 22:16:45
PHPExcel笔记, mpdf导出
2024-06-05 09:44:57
Centos7下安装MySQL8.0.23的步骤(小白入门级别)
2024-01-22 02:10:34
![](https://img.aspxhome.com/file/2023/0/121560_0s.png)
Python Matplotlib绘图基础详细教程
2024-01-16 04:34:41
![](https://img.aspxhome.com/file/2023/0/119710_0s.jpg)
python抓取多种类型的页面方法实例
2023-11-20 12:56:11
JavaScript状态模式及适配器模式使用讲解
2024-04-19 10:14:05
MySQL数据库命令
2024-01-17 09:27:22
![](https://img.aspxhome.com/file/2023/2/102352_0s.png)
关于ASP中的类class
2007-08-19 15:37:00
一小时学会TensorFlow2之全连接层
2022-11-27 10:35:30
![](https://img.aspxhome.com/file/2023/0/93810_0s.jpg)
MySQL中XML数据的XPath支持
2009-12-15 21:57:00
教你用python从日期中获取年、月、日和星期等30种信息
2023-05-06 22:32:38
![](https://img.aspxhome.com/file/2023/1/102861_0s.png)
SQL Server 2000/2005/2008删除或压缩数据库日志的方法
2024-01-20 15:56:15