News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

reading from cosole

Started by Sevag.K, June 12, 2005, 08:00:27 PM

Previous topic - Next topic

Sevag.K

Hi all,

I'm trying to read input from the console, but I can only seem to get ReadFile to recognize standard keys, is there any way to receive input from ESC, F1-12, arrow keys and home keys?


Tedd

Under dos, I think they were prefixed with 0 and then the following byte indicated the key.
Under windows though, I don't think you get this.
You could try using GetAsyncKeyState at the time you need to check for those keys. There are also other functions similar to this that might suit your purposes eg. GetKeyState, GetKeyboardState, ...
No snowflake in an avalanche feels responsible.

MichaelW

See MSDN: ReadConsoleInput

I think I posted this here somewhere, but it's easier to post it again than to find it.

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; Under Windows 2000 and probably XP, to see the mouse events
; you must run the console full screen, or disable QuickEdit
; mode in the console window properties.
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                       ; create 32 bit code
    .model flat, stdcall       ; 32 bit memory model
    option casemap :none       ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\kernel32.lib
    include \masm32\macros\macros.asm

    MOUSE_EVENT EQU 2

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .data

        hStdIn    dd 0
        nRead     dd 0

        INPUT_RECORD STRUCT
          EventType WORD ?
          WORD ?                  ; For alignment
          UNION
            KeyEvent              KEY_EVENT_RECORD          <>
            MouseEvent            MOUSE_EVENT_RECORD        <>
            WindowBufferSizeEvent WINDOW_BUFFER_SIZE_RECORD <>
            MenuEvent             MENU_EVENT_RECORD         <>
            FocusEvent            FOCUS_EVENT_RECORD        <>
          ENDS
        INPUT_RECORD ENDS
        InputRecord INPUT_RECORD <>

    .code

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    invoke GetStdHandle,STD_INPUT_HANDLE
    mov   hStdIn,eax

    .WHILE InputRecord.KeyEvent.wVirtualKeyCode != VK_ESCAPE

        invoke ReadConsoleInput,hStdIn,ADDR InputRecord,1,ADDR nRead

        movzx  eax,InputRecord.EventType
        Switch eax

            Case KEY_EVENT

                print chr$("bKeyDown ")
                print uhex$(InputRecord.KeyEvent.bKeyDown)
                print chr$(13,10)

                print chr$("wRepeatCount ")
                movzx eax,InputRecord.KeyEvent.wRepeatCount
                print uhex$(eax)
                print chr$(13,10)

                print chr$("wVirtualKeyCode ")
                movzx eax,InputRecord.KeyEvent.wVirtualKeyCode
                print uhex$(eax)
                print chr$(13,10)

                print chr$("wVirtualScanCode ")
                movzx eax,InputRecord.KeyEvent.wVirtualScanCode
                print uhex$(eax)
                print chr$(13,10)

                print chr$("uChar ")
                movzx eax,InputRecord.KeyEvent.uChar
                print uhex$(eax)
                print chr$(13,10,13,10)

            Case MOUSE_EVENT

                print chr$("dwMousePosition.x ")
                movzx eax,InputRecord.MouseEvent.dwMousePosition.x
                print ustr$(eax)
                print chr$(13,10)
     
                print chr$("dwMousePosition.y ")
                movzx eax,InputRecord.MouseEvent.dwMousePosition.y
                print ustr$(eax)
                print chr$(13,10)
     
                print chr$("dwButtonState ")
                print uhex$(InputRecord.MouseEvent.dwButtonState)
                print chr$(13,10)
     
                print chr$("dwControlKeyState ")
                print uhex$(InputRecord.MouseEvent.dwControlKeyState)
                print chr$(13,10)
     
                print chr$("dwEventFlags ")
                print uhex$(InputRecord.MouseEvent.dwEventFlags)
                print chr$(13,10,13,10)

        EndSw
    .ENDW
    exit
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


The alignment pad in the structure is necessary to duplicate the C/C++ alignment for a union in a structure.
eschew obfuscation

RuiLoureiro

Hi Michael
              Yes you posted in my topic Console Input-Output (is in page 4) where i have the example file Cons29.zip.

Rgds

Sevag.K

Thanks, that helps.  I thought there was an error in the SDK since the 'union' in KEY_EVENT_RECORD appeared to be actually 2 words according to the memory dump I did.  So the VirtualKeyCode mapped onto the VirtualScanCode and the whole structure was out of alignment!