def get_input():
global lines
data = None
try:
if sys.platform in ['linux', 'darwin']:
import select
time.sleep(0.01)
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
data = sys.stdin.readline().rstrip()
elif sys.platform == 'win32':
import msvcrt
time.sleep(0.01)
if msvcrt.kbhit():
data = msvcrt.getch().decode('utf-8')
if data == '\r':
# Enter is pressed
data = lines
lines = ''
else:
lines += data
print(data)
data = None
else:
pass
except KeyboardInterrupt:
data = 'exit'
return data
python类kbhit()的实例源码
def getbyt(self):
res = None
if len(self.buf)>0:
res = self.buf[0]
self.buf = self.buf[1:]
elif self.posix:
nw = sys.stdin.read(32)
if len(nw)>0:
self.buf = nw[1:]
res = nw[0]
else:
if msvcrt.kbhit():
res = msvcrt.getch()
return res
def stdin_ready():
return msvcrt.kbhit()
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
def userKeyboardInput():
"""
Reads the input from the user
:return: Keycode or None if there was no hit
"""
x = msvcrt.kbhit()
if x:
ret = msvcrt.getch()
else:
ret = None
return ret
def check_key():
if msvcrt is None:
return False
else:
if msvcrt.kbhit()!=0:
q=msvcrt.getch()
return q in "q"
else:
return False
def check_key():
if msvcrt is None:
return False
else:
if msvcrt.kbhit():
q = ensure_unicode(msvcrt.getch())
return q
return ""
def check_key():
if msvcrt is None:
return False
else:
if msvcrt.kbhit():
q = ensure_unicode(msvcrt.getch())
return q
return ""
def getch(self):
''' Returns a keyboard character after kbhit() has been called.
Should not be called in the same program as getarrow().
'''
s = ''
if os.name == 'nt':
return msvcrt.getch().decode('utf-8')
else:
return sys.stdin.read(1)
def kbhit(self):
''' Returns True if keyboard character was hit, False otherwise.
'''
if os.name == 'nt':
return msvcrt.kbhit()
else:
dr,dw,de = select([sys.stdin], [], [], 0)
return dr != []
def flush(self):
"""
flush stdin... untested on Windows"
"""
if os.name == 'nt':
while self.kbhit():
self.getch()
else:
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
return
# Test
def keystop(delay=0):
if os.name == 'posix':
return len(select.select([sys.stdin],[],[],delay)[0])
else:
return msvcrt.kbhit()
def getch(self):
''' Returns a keyboard character after kbhit() has been called.
Should not be called in the same program as getarrow().
'''
s = ''
if os.name == 'nt':
return msvcrt.getch().decode('utf-8')
else:
return sys.stdin.read(1)
def kbhit(self):
''' Returns True if keyboard character was hit, False otherwise.
'''
if os.name == 'nt':
return msvcrt.kbhit()
else:
dr,dw,de = select([sys.stdin], [], [], 0)
return dr != []
# Test
def getch(self):
''' Returns a keyboard character after kbhit() has been called.
Should not be called in the same program as getarrow().
'''
s = ''
if os.name == 'nt':
return msvcrt.getch().decode('utf-8')
else:
return sys.stdin.read(1)
def kbhit(self):
''' Returns True if keyboard character was hit, False otherwise.
'''
if os.name == 'nt':
return msvcrt.kbhit()
else:
dr,dw,de = select([sys.stdin], [], [], 0)
return dr != []
# Test
def stdin_ready():
return msvcrt.kbhit()
# On linux only, window.flip() has a bug that causes an AttributeError on
# window close. For details, see:
# http://groups.google.com/group/pyglet-users/browse_thread/thread/47c1aab9aa4a3d23/c22f9e819826799e?#c22f9e819826799e
def stdin_ready():
return msvcrt.kbhit()
#-----------------------------------------------------------------------------
# Callback functions
#-----------------------------------------------------------------------------
def _stdin_ready_nt():
"""Return True if there's something to read on stdin (nt version)."""
return msvcrt.kbhit()
def _run_stdio(self):
"""Runs the process using the system standard I/O.
IMPORTANT: stdin needs to be asynchronous, so the Python
sys.stdin object is not used. Instead,
msvcrt.kbhit/getwch are used asynchronously.
"""
# Disable Line and Echo mode
#lpMode = DWORD()
#handle = msvcrt.get_osfhandle(sys.stdin.fileno())
#if GetConsoleMode(handle, ctypes.byref(lpMode)):
# set_console_mode = True
# if not SetConsoleMode(handle, lpMode.value &
# ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT)):
# raise ctypes.WinError()
if self.mergeout:
return self.run(stdout_func = self._stdout_raw,
stdin_func = self._stdin_raw_block)
else:
return self.run(stdout_func = self._stdout_raw,
stdin_func = self._stdin_raw_block,
stderr_func = self._stderr_raw)
# Restore the previous console mode
#if set_console_mode:
# if not SetConsoleMode(handle, lpMode.value):
# raise ctypes.WinError()
def check_key():
if msvcrt is None:
return False
else:
if msvcrt.kbhit():
q = ensure_unicode(msvcrt.getch())
return q
return ""