diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-06-28 18:55:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-28 18:55:59 +0200 |
commit | f7f0cff8b3544694593d36f880d24f7e9f87593f (patch) | |
tree | b0f5e1adad463c7732737a682053fd3b1e2c62e0 /lib/pure | |
parent | 38de553b86fd5f42c726da9bceb6828fb835eff5 (diff) | |
parent | 8314412b99fbb101d44cee62a091c0a8e5f84089 (diff) | |
download | Nim-f7f0cff8b3544694593d36f880d24f7e9f87593f.tar.gz |
Merge pull request #4403 from miere43/win-getch
Implemented terminal.getch() for Windows
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/terminal.nim | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim index 60f064e7c..c135f6a00 100644 --- a/lib/pure/terminal.nim +++ b/lib/pure/terminal.nim @@ -493,17 +493,21 @@ template styledEcho*(args: varargs[expr]): expr = ## Echoes styles arguments to stdout using ``styledWriteLine``. callStyledEcho(args) -when defined(nimdoc): - proc getch*(): char = - ## Read a single character from the terminal, blocking until it is entered. - ## The character is not printed to the terminal. This is not available for - ## Windows. - discard -elif not defined(windows): - proc getch*(): char = - ## Read a single character from the terminal, blocking until it is entered. - ## The character is not printed to the terminal. This is not available for - ## Windows. +proc getch*(): char = + ## Read a single character from the terminal, blocking until it is entered. + ## The character is not printed to the terminal. + when defined(windows): + let fd = getStdHandle(STD_INPUT_HANDLE) + var keyEvent = KEY_EVENT_RECORD() + var numRead: cint + while true: + # Block until character is entered + doAssert(waitForSingleObject(fd, INFINITE) == WAIT_OBJECT_0) + doAssert(readConsoleInput(fd, addr(keyEvent), 1, addr(numRead)) != 0) + if numRead == 0 or keyEvent.eventType != 1 or keyEvent.bKeyDown == 0: + continue + return char(keyEvent.uChar) + else: let fd = getFileHandle(stdin) var oldMode: Termios discard fd.tcgetattr(addr oldMode) |