Search code examples
assembly32-bitmasmmasm32

Manage keypress


I'm using MASM syntax , and I want to get a keyboard key press then store it to use it later in code , I've tried using : int 21h interrupt but it seems that it doesn't work under 32-bit.

Is there any other way to achieve that ?

thanks.


Solution

  • If you want to switch to Windows, you could probably use the GetKeyboardState function to find out if one or more keys are being pressed. Even easier than using GetKeyboardState would be to use something like the following:

    .486
    .model flat,stdcall
    option casemap : none
    
    include \masm32\include\windows.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\msvcrt.inc
    include \masm32\macros\macros.asm
    
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\msvcrt.lib
    
    .data?
    key dd ?
    
    .code
    
    start:
    
    printf("Press a key..")
    call crt__getch
    mov key,eax
    printf("\nYou pressed key number %d", key)
    
    invoke ExitProcess,0
    
    END start