flat assembler
Message board for the users of flat assembler.

Index > Windows > I have unbuffered keyboard input!

Author
Thread Post new topic Reply to topic
chastitywhiterose



Joined: 13 Oct 2025
Posts: 48
chastitywhiterose 26 Jan 2026, 18:56
I found the right Windows API function to turn off the line buffering mode in Windows console programs. This is the foundation of being able to manage a terminal base program or game. This program will print the bytes in hexadecimal that are sent when you press the keyboard. Some keys such as arrow keys actually send more than one byte. It would take time, but I could write a program than handles these and does different things accordingly.

Code:
format PE console
include 'win32ax.inc'
include 'chastelibw32.asm'

main:

mov [radix],16         ;can choose radix for integer output!
mov [int_width],1
mov [int_newline],0

mov eax,main_string
call putstring

call fix_stdin         ;call function to disable line buffering in console(set the source below in this file) 

loop_read_keyboard:    ;this loop keeps reading from the keyboard

call getchar           ;call my function that reads a single byte from the keyboard
call putint            ;print the number of this key
call putline           ;print a line to make it easier to read

cmp al,'q'             ;test for q key. q stands for quit in this context
jnz loop_read_keyboard

;Exit the process with code 0
push 0
call [ExitProcess]

.end main

;A string to test if output works
main_string db 'This program reads from the keyboard until you press q.',0Ah,0

;this function disables line buffering in the terminal on windows
fix_stdin:
push 0x200          ; mode: ENABLE_VIRTUAL_TERMINAL_INPUT
push -10            ;STD_INPUT_HANDLE = Negative Ten
call [GetStdHandle] ;use the above handle
push eax            ;eax is return value of previous function
call [SetConsoleMode]
ret

keys_read dd 0
key db 0

;read only 1 byte using Win32 ReadFile system call.
getchar:
push 0              ;Optional Overlapped Structure 
push keys_read      ;Store Number of Bytes Read from this call
push 1              ;Number of bytes to read
push key            ;address to store bytes
push -10            ;STD_INPUT_HANDLE = Negative Ten
call [GetStdHandle] ;use the above handle
push eax            ;eax is return value of previous function
call [ReadFile]
xor eax,eax         ;set eax to 0
mov al,[key]        ;set lowest part of eax to key read
ret
    


Description: My chastelib library for windows to print things to the console
Download
Filename: chastelibw32.asm
Filesize: 5.88 KB
Downloaded: 8 Time(s)

Post 26 Jan 2026, 18:56
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20843
Location: In your JS exploiting you and your system
revolution 27 Jan 2026, 05:53
It is possible to use ReadConsoleInput to read the input events, including for the mouse etc. and the associated GetNumberOfConsoleInputEvents + PeekConsoleInput to monitor the state of the input without blocking.

Using WaitForSingleObject allows an efficient block until something is available.
Post 27 Jan 2026, 05:53
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.