News:

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

Console app + mouse input

Started by jspaul, April 14, 2006, 12:49:50 PM

Previous topic - Next topic

jspaul

Could someone give me any tips to get started on getting mouse input to work in a console app?
The texts I have read only have examples for DOS using interrupts.

All I'd like to do is have a message display on the screen when a mouse button is clicked. Eventually, I hope to figure out how to read the cursor coordinates.

Much appreciated,
Jeff

jspaul

P1, I appreciate you keeping these forums up to date. However, I meant I need help getting mouse input to work in a Win32 console application, not a DOS application.

Could you please move this thread back to the appropriate forum? :)

P1

Quote from: jspaul on April 14, 2006, 04:00:54 PMI meant I need help getting mouse input to work in a Win32 console application, not a DOS application.
Then why does M$ view it as such ?

A new console process has started. The idObject parameter contains the process identifier of the newly created process. If the application is a 16-bit application, the idChild parameter is CONSOLE_APPLICATION_16BIT and idObject is the process identifier of the NTVDM session associated with the console.

The 16bit solution will be the 32bit solution because you need a ntvdm solution.

I stuck it in DOS, because the DOS programmers for the console have solutions.  I will be very surprized, if you get any solution that does not get it's solution from doing it in a DOS mannor.

So as it is written, so as it will be done.   :wink

Welcome a Board !!!     

Forum 'Search' is your friend, along with Google.   

Please read the forum rules.


Regards,  P1  :8)

Mark Jones

Jeff I just saw something about this the other day. If you have the WIN32.HLP file try an index search for "Console Input Buffer." That talks about events being in an INPUT_RECORD struct. Namely KEY_EVENT_RECORD and MOUSE_EVENT_RECORD structures. So it looks like maybe a double-pointer arrangement of structures. MSDN should also have the relevant info. Let us know how it goes. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

jspaul

Ok, thanks! That sounds kind of like what I was hoping for. I'll look that up and hopefully my little knowledge of assembler won't prevent me from applying it.

jspaul

Great, I think I am well on my way to do this. But, I've come across one problem. I found out I need to use SetConsoleMode to enable mouse input, and that's where the problem lies.

BOOL WINAPI SetConsoleMode(
  HANDLE hConsoleHandle,
  DWORD dwMode
);

Can anyone point me in the right direction to a procedure that will give me the handle to the console?
I've found GetStdHandle, but it seems a little bit different than what I need.

P1

Be sure to check the MSDN website, that is where I got my console quote from.

Regards,  P1  :8)

GregL

jspaul,

GetStdHandle will do it.


.DATA
    hStdIn DWORD 0
.CODE
    invoke GetStdHandle, STD_INPUT_HANDLE
    mov hStdIn, eax


Tip: If QuickEdit Mode is on, mouse input is disabled, you must turn it off if you want mouse input. Use GetConsoleMode and SetConsoleMode with ENABLE_QUICK_EDIT_MODE ORed with
ENABLE_EXTENDED_FLAGS. Be sure to put QuickEdit Mode back the way it was when you're done. I don't have the code in MASM or I would post it.

MSDN SetConsoleMode


MichaelW

I'm sure I posted an earlier version of this somewhere, but I cannot find it here or on the old forum.

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

    include \masm32\include\masm32rt.inc

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

    .data

      hStdIn    dd 0
      nRead     dd 0

      ; ----------------------------------------------------------
      ; The INPUT_RECORD structure in the MASM32 windows.inc is
      ; a direct translation of the structure from the API header
      ; files and as such it lacks the alignment padding that the
      ; C/C++ compiler would add, and that Windows expects, ahead
      ; of the union.
      ; ----------------------------------------------------------

      _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 "bKeyDown "
            print uhex$(InputRecord.KeyEvent.bKeyDown)
            print chr$(13,10)

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

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

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

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

          Case MOUSE_EVENT

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

        EndSw
    .ENDW

    inkey "Press any key to continue..."
    mov   InputRecord.KeyEvent.wVirtualKeyCode, 0
    cls

    .WHILE InputRecord.KeyEvent.wVirtualKeyCode != VK_ESCAPE

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

        .IF InputRecord.EventType == MOUSE_EVENT

            loc 1,1
            print "dwMousePosition.x "
            movzx eax,InputRecord.MouseEvent.dwMousePosition.x
            print ustr$(eax)
            loc 1,2
            print "dwMousePosition.y "
            movzx eax,InputRecord.MouseEvent.dwMousePosition.y
            print ustr$(eax)
            loc 1,3
            print "dwButtonState "
            print uhex$(InputRecord.MouseEvent.dwButtonState)
            loc 1,4
            print "dwControlKeyState "
            print uhex$(InputRecord.MouseEvent.dwControlKeyState)
            loc 1,5
            print "dwEventFlags "
            print uhex$(InputRecord.MouseEvent.dwEventFlags)

        .ENDIF
    .ENDW

    print chr$(13,10,13,10)
    inkey "Press any key to exit..."
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start
eschew obfuscation

redskull

QuoteIf the input buffer contains input events other than keyboard events (such as mouse events or window-resizing events), they are discarded. Those events can only be read by using the ReadConsoleInput function.
Strange women, lying in ponds, distributing swords, is no basis for a system of government