16INVALID_HANDLE_VALUE = -1
18ENABLE_LINE_INPUT = 0x0002
20ENABLE_ECHO_INPUT = 0x0004
34 kernel32 = ctypes.windll.kernel32
44 print(
" Error! No termios or msvcrt. Cannot check for keys or get cursor position.")
61 self.
_hStdIn = kernel32.GetStdHandle(STD_INPUT_HANDLE)
62 if self.
_hStdIn != INVALID_HANDLE_VALUE:
63 oldMode = ctypes.c_uint32(0)
64 if kernel32.GetConsoleMode(self.
_hStdIn, ctypes.byref(oldMode)):
67 lastError = kernel32.GetLastError()
68 print(
"GetStdHandle(STD_INPUT_HANDLE) failed: code = {:x}".format(lastError))
72 if self.
_hStdIn != INVALID_HANDLE_VALUE:
73 newMode = self.
_inputMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)
74 if not kernel32.SetConsoleMode(self.
_hStdIn, newMode):
75 lastError = kernel32.GetLastError()
76 print(
"SetConsoleMode(hStdIn, newMode) failed: code = {:x}".format(lastError))
82 lastError = kernel32.GetLastError()
83 print(
"SetConsoleMode(hStdIn, inputMode) failed: code = {:x}".format(lastError))
94 DisableInputEcho = DisableInputEcho_Windows
117 stdin_fd = sys.stdin.fileno()
119 new_settings = termios.tcgetattr(stdin_fd);
120 new_settings[3] = new_settings[3] & ~termios.ICANON & ~termios.ECHO
121 termios.tcsetattr(stdin_fd, termios.TCSANOW, new_settings)
125 stdin_fd = sys.stdin.fileno()
127 termios.tcsetattr(stdin_fd, termios.TCSANOW, self.
old_settings)
139 DisableInputEcho = DisableInputEcho_Linux
161 sys.stdout.write(
"{}[{};{}H".format(ASCII_ESC, row, column))
175 sys.stdout.write(
"{}[6n".format(ASCII_ESC))
191 c = sys.stdin.read(1)
192 inputbuffer.append(c)
193 if c ==
'R' or c == ASCII_CTRL_Z:
195 buffer =
''.join(inputbuffer)
196 if len(buffer) > 2
and buffer[0] == ASCII_ESC
and buffer[1] ==
'[':
197 elements = re.split(
".\[([0-9]+);([0-9]+)R", buffer)
198 if len(elements) >= 3:
199 row = int(elements[1])
200 column = int(elements[2])
205 def sleep(self, milliseconds : int) ->
None:
206 time.sleep(milliseconds/1000.0)
214 return msvcrt.kbhit()
216 dr,dw,de = select.select([sys.stdin], [], [], 0)
230 return sys.stdin.read(1)
bool checkforkey(void)
Determine if a key has been pressed.
Class containing a number of helper methods for use in the Flyweight Pattern example.
tuple getcursorposition(self)
Retrieve the current cursor position in the console window.
None setcursorposition(self, int column, int row)
Move the text cursor to the specified screen coordinates.
None sleep(self, int milliseconds)
Sleep for the specified number of milliseconds.
int readkey(void)
Read a key from the keyboard, blocking if no key is pressed. Use the checkforkey() function to see if...