From 79fc61359a8f9a9563627e9ad8e5938c3ffee4ca Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Tue, 14 Sep 2010 15:37:12 +1000 Subject: win32: read_key: reset console state after reading --- win32/termios.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/win32/termios.c b/win32/termios.c index 6d85ff98e..1bd3cbd5b 100644 --- a/win32/termios.c +++ b/win32/termios.c @@ -12,25 +12,29 @@ int tcgetattr(int fd UNUSED_PARAM, struct termios *t UNUSED_PARAM) int64_t FAST_FUNC read_key(int fd, char *buf, int timeout UNUSED_PARAM) { - static int initialized = 0; HANDLE cin = GetStdHandle(STD_INPUT_HANDLE); INPUT_RECORD record; - DWORD nevent_out; + DWORD nevent_out, mode; + int ret = -1; if (fd != 0) bb_error_msg_and_die("read_key only works on stdin"); if (cin == INVALID_HANDLE_VALUE) return -1; - if (!initialized) { - SetConsoleMode(cin, ENABLE_ECHO_INPUT); - initialized = 1; - } + GetConsoleMode(cin, &mode); + SetConsoleMode(cin, 0); while (1) { if (!ReadConsoleInput(cin, &record, 1, &nevent_out)) - return -1; + goto done; if (record.EventType != KEY_EVENT || !record.Event.KeyEvent.bKeyDown) continue; - return record.Event.KeyEvent.uChar.AsciiChar; + if (!record.Event.KeyEvent.uChar.AsciiChar) + continue; + ret = record.Event.KeyEvent.uChar.AsciiChar; + break; } + done: + SetConsoleMode(cin, mode); + return ret; } -- cgit v1.2.3-55-g6feb