From 5a5d254ecb352ebd9743696794a98725c8bf3a36 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Sat, 11 Sep 2010 17:51:58 +1000 Subject: win32: reimplement read_key() to read Windows console --- win32/termios.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'win32') diff --git a/win32/termios.c b/win32/termios.c index 0aba48546..6d85ff98e 100644 --- a/win32/termios.c +++ b/win32/termios.c @@ -1,4 +1,4 @@ -#include "busybox.h" +#include "libbb.h" int tcsetattr(int fd UNUSED_PARAM, int mode UNUSED_PARAM, const struct termios *t UNUSED_PARAM) { @@ -9,3 +9,28 @@ int tcgetattr(int fd UNUSED_PARAM, struct termios *t UNUSED_PARAM) { return -1; } + +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; + + 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; + } + + while (1) { + if (!ReadConsoleInput(cin, &record, 1, &nevent_out)) + return -1; + if (record.EventType != KEY_EVENT || !record.Event.KeyEvent.bKeyDown) + continue; + return record.Event.KeyEvent.uChar.AsciiChar; + } +} -- cgit v1.2.3-55-g6feb