Merge pull request #4403 from miere43/win-getch
Implemented terminal.getch() for Windows
This commit is contained in:
commit
f7f0cff8b3
2 changed files with 35 additions and 12 deletions
|
|
@ -493,17 +493,21 @@ template styledEcho*(args: varargs[expr]): expr =
|
||||||
## Echoes styles arguments to stdout using ``styledWriteLine``.
|
## Echoes styles arguments to stdout using ``styledWriteLine``.
|
||||||
callStyledEcho(args)
|
callStyledEcho(args)
|
||||||
|
|
||||||
when defined(nimdoc):
|
proc getch*(): char =
|
||||||
proc getch*(): char =
|
|
||||||
## Read a single character from the terminal, blocking until it is entered.
|
## 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
|
## The character is not printed to the terminal.
|
||||||
## Windows.
|
when defined(windows):
|
||||||
discard
|
let fd = getStdHandle(STD_INPUT_HANDLE)
|
||||||
elif not defined(windows):
|
var keyEvent = KEY_EVENT_RECORD()
|
||||||
proc getch*(): char =
|
var numRead: cint
|
||||||
## Read a single character from the terminal, blocking until it is entered.
|
while true:
|
||||||
## The character is not printed to the terminal. This is not available for
|
# Block until character is entered
|
||||||
## Windows.
|
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)
|
let fd = getFileHandle(stdin)
|
||||||
var oldMode: Termios
|
var oldMode: Termios
|
||||||
discard fd.tcgetattr(addr oldMode)
|
discard fd.tcgetattr(addr oldMode)
|
||||||
|
|
|
||||||
|
|
@ -1017,3 +1017,22 @@ proc wsaCloseEvent*(hEvent: Handle): bool
|
||||||
|
|
||||||
proc wsaResetEvent*(hEvent: Handle): bool
|
proc wsaResetEvent*(hEvent: Handle): bool
|
||||||
{.stdcall, importc: "WSAResetEvent", dynlib: "ws2_32.dll".}
|
{.stdcall, importc: "WSAResetEvent", dynlib: "ws2_32.dll".}
|
||||||
|
|
||||||
|
type
|
||||||
|
KEY_EVENT_RECORD* {.final, pure.} = object
|
||||||
|
eventType*: int16
|
||||||
|
bKeyDown*: WINBOOL
|
||||||
|
wRepeatCount*: int16
|
||||||
|
wVirtualKeyCode*: int16
|
||||||
|
wVirtualScanCode*: int16
|
||||||
|
uChar*: int16
|
||||||
|
dwControlKeyState*: DWORD
|
||||||
|
|
||||||
|
when defined(useWinAnsi):
|
||||||
|
proc readConsoleInput*(hConsoleInput: Handle, lpBuffer: pointer, nLength: cint,
|
||||||
|
lpNumberOfEventsRead: ptr cint): cint
|
||||||
|
{.stdcall, dynlib: "kernel32", importc: "ReadConsoleInputA".}
|
||||||
|
else:
|
||||||
|
proc readConsoleInput*(hConsoleInput: Handle, lpBuffer: pointer, nLength: cint,
|
||||||
|
lpNumberOfEventsRead: ptr cint): cint
|
||||||
|
{.stdcall, dynlib: "kernel32", importc: "ReadConsoleInputW".}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue