Search code examples
assemblymouseclick-eventmasm32irvine32

Mouse Selection in MASM32


Ultimately, I am trying to select a button when there is a left click on the mouse using assembly language but have not found any useful techniques. Any help would be grateful! The code for the program is below.

INCLUDE IRVINE32.inc
INCLUDELIB kernel32.lib
INCLUDELIB user32.lib
INCLUDELIB Winmm.lib
INCLUDELIB Windows.lib
INCLUDELIB kernel32.lib
INCLUDELIB gdi32.lib



WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
ExitProcess PROTO STDCALL :DWORD

WM_LBUTTONDOWN EQU 0x0201

.data
prompt BYTE "Mouse Not Pressed",0
message BYTE "Mouse pressed",0
MouseClick db 0
WM_LBUTTONDOWN bytE 0x0201



.code
main PROC 
mov edx, OFFSET message
.IF WM_LBUTTONDOWN == true
Call WriteString
.ENDIF
mov edx, OFFSET prompt
Call WriteString

invoke ExitProcess, 0
main ENDP
END main

Solution

  • Thanks to MichaelW I could build an example for your needs:

    include irvine32.inc
    
    .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 <>
    
        ConsoleMode dd 0
        Msg db "Click! ",0
        Msg2 db "Esc ",0
    
    .code
    
    main PROC
        invoke GetStdHandle,STD_INPUT_HANDLE
        mov   hStdIn,eax
    
        invoke GetConsoleMode, hStdIn, ADDR ConsoleMode
        mov eax, 0090h          ; ENABLE_MOUSE_INPUT | DISABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS
        invoke SetConsoleMode, hStdIn, eax
    
        .WHILE InputRecord.KeyEvent.wVirtualKeyCode != VK_ESCAPE
    
            invoke ReadConsoleInput,hStdIn,ADDR InputRecord,1,ADDR nRead
    
            movzx  eax,InputRecord.EventType
            cmp eax, MOUSE_EVENT
            jne no_mouse
            test InputRecord.MouseEvent.dwButtonState, 1
            jz no_mouse
    
            lea edx, Msg
            Call WriteString
            jmp done
    
            no_mouse:
        .ENDW
    
        lea edx, Msg2
        Call WriteString
    
        done:
        mov eax, ConsoleMode
        invoke SetConsoleMode, hStdIn, eax
        call ReadChar
        invoke ExitProcess, 0
    main ENDP
    
    end main