News:

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

Simulate Mouse Clicks

Started by Seraph787, December 30, 2006, 06:52:25 PM

Previous topic - Next topic

Seraph787

I want to learn some ASM so I figured a good way to learn is to make a program. I have a problem with my laptop. My touchpad does not have a middle click but has 2 odd scroll up and down buttons that don't work well. I can map the buttons to launch a program and I was wondering if I could somehow make a program that would simulate a mouse click on execution for where the mouse is. Any ideas on how I would do this or even willing to show me an example code?
Thanks alot!

Darrel

Here is some code to simulate a Mouse Click

.data?

MousePoint POINT <>
Rect RECT <>
hWnd DWORD ?
dwMousePos DWORD ?

.code

INVOKE GetCursorPos,ADDR MousePoint

INVOKE WindowFromPoint,MousePoint.x,MousePoint.y ;user32.dll

mov hWnd,eax

INVOKE GetWindowRect,hWnd,ADDR Rect ;user32.dll

mov eax,MousePoint.x
sub eax,Rect.left
and eax,0FFFFh
mov edx,MousePoint.y
sub edx,Rect.top
shl edx,16
or eax,edx
mov dwMousePos,eax

INVOKE SendMessage,hWnd,WM_LBUTTONDOWN,NULL,dwMousePos ;user32.dll

INVOKE SendMessage,hWnd,WM_LBUTTONUP,NULL,dwMousePos ;user32.dll

INVOKE ExitProcess,NULL ;kernel32.dll


HTH,

Darrel

Tedd

The shorter version :bg


.data?
pt  POINT <?>

.code
    invoke GetCursorPos, ADDR pt
    invoke mouse_event, MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN,pt.x,pt.y,NULL,NULL
    invoke mouse_event, MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP,pt.x,pt.y,NULL,NULL
No snowflake in an avalanche feels responsible.

Darrel


Seraph787

sorry for the late reply. been really busy lately
anyway.
I got the code working completley.

Thanks alot.

I was able to simulate the middle click using "MOUSEEVENTF_MIDDLEDOWN/UP"
I was wondering if there is a way to simulate a 5 button mouse. I tried using MOUSEEVENTF_XUP
    invoke mouse_event, MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_XDOWN,pt.x,pt.y,XBUTTON1,NULL
    invoke mouse_event, MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_XUP,pt.x,pt.y,XBUTTON1,NULL


However I get the error code
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: C:\Documents.asm
C:\Documents.asm(18) : error A2006: undefined symbol : MOUSEEVENTF_XDOWN
C:\Documents.asm(18) : error A2114: INVOKE argument type mismatch : argument : 1

C:\Documents.asm(19) : error A2006: undefined symbol : MOUSEEVENTF_XUP
C:\Documents.asm(19) : error A2114: INVOKE argument type mismatch : argument : 1

_
Assembly Error
Press any key to continue . . .


I then looked into using SendInput but I got lost looking at it.

Any suggestions?

Thanks again

Tedd

#5
MOUSEEVENTF_X... are not defined values, so that's why you're getting those errors. No idea what values to use either :(

Something I forgot/missed:
The x and y co-ordinates given should actually be normalized mapping to a (65536,65536) square onto the screen surface. That means for a 1024,768 screen mode: (0,0) will be at (0,0), but (1023,767) will actually be at (65535,65535).
So, to do the mapping correctly, you'll need to get the current screen size {scWidth = GetSystemMetrics(SM_CXSCREEN); scHeight = GetSystemMetrics(SM_CYSCREEN)} and then map your pixel co-ordinate into a normalized one {nx = pt.x*65535/(scWidth-1); ny = pt.y*65535/(scHeight-1)}
No snowflake in an avalanche feels responsible.

Seraph787

Hrm so all I have to do is define both
MOUSEEVENTF_XDOWN and MOUSEEVENTF_XUP?
I think I saw it while I was googling around so that is ok.
And why would I have to map correctly? It works perfectly normally without it.

Tedd

Yes, if you define those values then you can use them.
I found these:

MOUSEEVENTF_XDOWN equ 100h
MOUSEEVENTF_XUP equ 200h
XBUTTON1 equ 1h
XBUTTON2 equ 2h


I was just repeating what the docs say - the co-ordinates ARE mapped. Doing the following..
    invoke mouse_event, MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE,32767,32767,NULL,NULL
moves the pointer to the center of the screen (rather than clipped off the bottom-right corner.)
It seems the (x,y) co-ordinate given is conveniently ignored in the case of button-press events, in which case the call to GetCursorPos is unnecessary.

Proper info here: http://msdn2.microsoft.com/en-us/library/ms646260.aspx
(Accordingly, you're supposed to use SendInput now, but that seems too much like hard work :bdg)
No snowflake in an avalanche feels responsible.

Seraph787

Ok great thanks guys! I got the program working and learned quite a bit!