pyhton 下 使用getch(), 输入字符无需回车
原代码来自
https://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/
同时支持windows和unix平台
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
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
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
使用的时候可以这么用,比如输入时不仅可以将输入的字符显示在屏幕上,就像是input(), 同时还可以对其每个字符都在输入时进行操作, 最后按下回车键退出输入
import sys
ss =''
while True:
s = Getch()
if s == "\r": # 注意回车键是 \r, 不是 \n
break
print(s,end='')
sys.stdout.flush() # 刷新缓冲区,不然无法显示到屏幕上
ss += s
print()
print(ss)
还可以在类中加上
def __str__(self):
return self.impl # 返回是其实是个字符串
然后使用:
str = str(_Getch()) # 自动调用__str__(self)方法
然后是我的精简魔改版
def Getch():
def _GetchUnix():
import sys, tty, termios
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 _GetchWindows():
import msvcrt
return msvcrt.getch()
try:
impl = _GetchWindows()
except ImportError:
impl = _GetchUnix()
return impl
使用:
str = Getch()
即可
魔改精简版2
上一个代码如果把Getch()放到循环里会有个问题,就是上下左右键可以控制光标的移动,这有些惊奇,但也是我不想要的
class Getch():
def __init__(self):
try:
self.impl = self._GetchWindows()
except ImportError:
self.impl = self._GetchUnix()
def __str__(self):
return self.impl
def _GetchUnix(self):
import sys, tty, termios
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 _GetchWindows(self):
import msvcrt
return msvcrt.getch()
使用
import sys
ss =''
while True:
s = Getch()
s = str(Getch())
if s == "\r":
break
print(s,end='')
sys.stdout.flush()
ss += s
方法3.使用getch库
这是一个仿照windows下conio.h中的getch()函数的库
https://pypi.org/project/getch/
产生这个问题的原因是使用tcp连接通信,在终端输入一句话时,如果只输入了一半,突然接收到了对方发来的消息,就会从光标处打印在屏幕上,那么我输入的话就被打断了.
查了很多网页,不知道getch这么个东西,学C语言时压根不知道,太差了.
pyhton 下 使用getch(), 输入字符无需回车的更多相关文章
- linux系统下解决getch()输入数值不回显示
在linux系统下开发C 程序却会遇到系统不支持conio.h头文件,无法使用getch()不回显函数.下面就演示如何构建函数实现数值输入不回显. #include <stdio.h> # ...
- 类似智能购票的demo--进入页面后默认焦点在第一个输入框,输入内容、回车、right时焦点自动跳到下一个,当跳到select时,下拉选项自动弹出,并且可以按上下键选择,选择完成后再跳到下一个。
要实现的效果:进入页面后默认焦点在第一个输入框,输入内容.回车.right时焦点自动跳到下一个,当跳到select时,下拉选项自动弹出,并且可以按上下键选择,选择完成后再跳到下一个. PS:自己模拟的 ...
- ubuntu中vi下删除键和上下左右键输入字符异常(ABCD)
刚安装的Ubuntu系统,使用vi编辑文本的时候, 出现以下现象: 点删除键输入了 D 回车无效 上下左右为字母 光标乱跳 原因: 自带的vi功能问题 解决: 卸载原有vi,重新安装完整版本vim 执 ...
- c 无回显读取字符/不按回车即获取字符
最近课程设计要使用各种有趣的函数,这是其中一个 #include <conio.h> 使用方法 char c; c=getch(); 这样按下输入一个字符不按回车就ok了
- 终端I/O之特殊输入字符
POSIX.1定义了11个在输入时作特殊处理的字符.实现定义了另外一些特殊字符.表18-6摘要列出了这些特殊字符. 表18-6 终端特殊输入字符 在POSIX.1的11个特殊字符中,可将其中9个更改为 ...
- c 语言连续输入字符型数据
#include<stdio.h> #include<stdlib.h> void Input1(char* &str){ // /* 这种情况下想要逐个输入字符串数组 ...
- JNI 在命令行窗口输入字符,不显所输入字符,显指定的掩饰符
//JNI-命令行窗口输入字符,显掩饰符.txt /* 目标:在命令行窗口输入字符,不显所输入字符,显指定的掩饰符 作者:tangshancheng@21cn.com*/ 1.KeyBoard.j ...
- java 图形化工具Swing 监听键盘输入字符触发动作getInputMap();getActionMap();
双缓冲技术的介绍: 所有的Swing组件默认启用双缓冲绘图技术.使用双缓冲技术能改进频繁重绘GUI组件的显示效果(避免闪烁现象)JComponent组件默认启用双缓冲,无须自己实现双缓冲.如果想关闭双 ...
- ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法
ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法 前几天卸掉了用了好多年的Windows,安装了Ubuntu12.04,就开始各种搭环境.今天装好了MySQ ...
随机推荐
- lua-excel助手
excel是我们工作及生活当中不可或缺的东西,好吧,我是一个游戏程序员,数值哥哥肯定会给我些表格的.回归正题,为什么需要做这个封装? 为什么需要这个项目,因为我们需要使用程序进行自动化操作 VBA我们 ...
- postgraSql支持View可以修改的两种方法。
http://www.postgresqltutorial.com/postgresql-views/ Creating PostgreSQL updatable views – gives you ...
- ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程
为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...
- LINUX基础了解
- Topic model的变种及其应用[1]
转: http://www.blogbus.com/krischow-logs/65749376.html LDA 着实 带领着 Topic model 火了一把. 但是其实我们华人世界内,也不乏 ...
- SAP R/3系统的R和3分别代表什么含义,负载均衡的实现原理
1972年,SAP诞生,推出了RF系统(实时财务会计系统), 后来命名为R1. R指Real time.3既指第三代系统,又代表3层架构. 三层架构分别为下图的Presentation server ...
- 跳跃表 SkipList【数据结构】原理及实现
为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. 想象一下,给你一张草稿纸,一只笔,一个编辑器,你能立即实现一颗红黑树,或者AVL树出来吗? ...
- PhoneGap Geolocation 获取地理位置 api
一. PhoneGap Geolocation 对象介绍 1.使应用程序可以访问地理位置信息.geolocation 对象提供了对设备 GPS 传感器的访问.Geolocation 提供设备的位置信息 ...
- Hibernate多对一关联关系
两个持久化类.Customer 和 OrderForm Customer 类. package com.zcd.hibernate.manyToOne; public class Customer { ...
- 软件架构的描述-Architecture Models
Software architecture involves the high level structure of software system abstraction, by using dec ...