Search code examples
windowsassemblykeyboard64-bitstdin

Non-blocking keyboard input x64 windows assembly / c++


I need a code in windows x64 assembly that would either:

a) Read data from user keyboard(stdin) in a non blocking way.

b) The way to check if there is data so I know if i should invoke readFile already or skip this step

c) Fix to the implementation I made so far

So far i tried the function PeekNamedPipe but it just doesnt seem to work. Here is the code I have so far:

mov     rcx,        dword -10
sub     rsp,        28h
call    GetStdHandle
add     rsp,        28h
mov     [std_in_handle],        rax

sub     rsp,        30h
mov rcx, qword [std_in_handle]
mov rdx, qword 0
mov r8d, dword 0
mov r9, qword 0
mov qword [rsp+0x20],   bytes_available
mov qword [rsp+0x28],   0
call PeekNamedPipe
add     rsp,        30h

cmp dword [bytes_available],    dword 0
je  .skip_reading_input

sub     rsp,        28h
mov rcx, qword [std_in_handle]
mov rdx, qword input_char
mov r8d, dword 1
mov r9, qword BytesRead
mov qword [rsp+0x20], 0
call ReadFile
add     rsp,        28h

.skip_reading_input

The thing is that value in bytes_available is always 0 even if i type something on the keyboard, so readfile is always skipped.


Solution

  • I managed to somehow solve my particular problem using this function

    extern GetAsyncKeyState

    it has options to return given value when the key is pressed OR when it was pressed since last call to the function - so I call this function once at the beginning of the program and then check if it has been called since that time. However there is number of issues when using this approach so I will check the ones that others suggested soon and will maybe post better solution.