Dynamixel_SDK中python代码中的getch()和kbhit()代表什么
代码如下:
下面这一段表示在windows下使用时
if os.name == 'nt':
import msvcrt
def getch():
return msvcrt.getch().decode()
下面一段表示在其他包括Linux在内的系统使用时
else:
import termios, fcntl, sys, os
from select import select
fd = sys.stdin.fileno()
old_term = termios.tcgetattr(fd)
new_term = termios.tcgetattr(fd)
def getch():
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
termios.tcsetattr(fd, termios.TCSANOW, new_term)
try:
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_term)
return ch
def kbhit():
new_term[3] = (new_term[3] & ~(termios.ICANON | termios.ECHO))
termios.tcsetattr(fd, termios.TCSANOW, new_term)
try:
dr,dw,de = select([sys.stdin], [], [], 0)
if dr != []:
return 1
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_term)
sys.stdout.flush()
return 0
分析如下:
这里面的getch()这个函数是一个不回显函数,当用户按下某个字符时,函数自动读取,(如下例子)在这里面是用来判断是否按下了ESC键,若果按下ESC则会退出程序。其中ESC的键值是0x1b.
if getch() == chr(0x1b):
break
getch()这个函数则是检查当前是否有键盘输入,若有则返回一个非0值(真值),否则返回0(假)。通常用来检测是否继续执行某些需要外部按键触发的程序。