Page 1 of 1

Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sat Oct 15, 2011 7:47 pm
by RASHAD

Code: Select all


Procedure LeftClick ()
  In.INPUT
  ;// left down 
  In\type        = #INPUT_MOUSE
  In\mi\dwFlags  = #MOUSEEVENTF_LEFTDOWN
  SendInput_(1,@In,SizeOf(INPUT))

  ;// left up
  ;ZeroMemory_(@In,SizeOf(INPUT))
  ;In\type        = #INPUT_MOUSE
  In\mi\dwFlags  = #MOUSEEVENTF_LEFTUP
  
  SendInput_(1,@In,SizeOf(INPUT))
EndProcedure


SetCursorPos_(10,50)
LeftClick()

; ;***************************************************************
; 
Procedure RightClick ()
  In.INPUT
  ;// right down 
  In\type        = #INPUT_MOUSE
  In\mi\dwFlags  = #MOUSEEVENTF_RIGHTDOWN
  SendInput_(1,@In,SizeOf(INPUT))

  ;// right up
  ;ZeroMemory_(@In,SizeOf(INPUT))
  ;In\type        = #INPUT_MOUSE
  In\mi\dwFlags  = #MOUSEEVENTF_RIGHTUP

  SendInput_(1,@In,SizeOf(INPUT))
EndProcedure
; 
; RightClick()
; 
; ;**************************************************************
; 
Procedure MouseMove (x, y)
  In.INPUT
  fScreenWidth   = GetSystemMetrics_( #SM_CXSCREEN )-1 
  fScreenHeight  = GetSystemMetrics_( #SM_CYSCREEN )-1 
  fx = x*(65535/fScreenWidth)
  fy = y*(65535/fScreenHeight)
  In\type        = #INPUT_MOUSE;
  In\mi\dwFlags  = #MOUSEEVENTF_MOVE|#MOUSEEVENTF_ABSOLUTE
  In\mi\dx       = fx
  In\mi\dy       = fy

  SendInput_(1,@In,SizeOf(INPUT))
EndProcedure
; 
; MouseMove(100,100)

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 12:47 am
by MachineCode
In case you didn't know, you can do it simpler, faster and in far less code like so:

Code: Select all

; Setting the mouse position.
SetCursorPos_(x,y)

Code: Select all

; Simulating a left mouse click.
mouse_event_(#MOUSEEVENTF_LEFTDOWN,0,0,0,0)
mouse_event_(#MOUSEEVENTF_LEFTUP,0,0,0,0)

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 1:42 am
by netmaestro
you can do it simpler, faster and in far less code like so:
You make a good point and I don't really disagree. However, for a couple of reasons I will always use RASHAD's approach. These are:

1) Code with SetCursorPos in it can be very disturbing to the users. They like to set the cursorpos themselves and tend to get nervous when the little arrow goes shooting off on its own somewhere, even if it comes right back.

2) mouse_event and keybd_event are superseded by MS in favor of the newer and more comprehensive SendInput. They still work of course, and will for some time to come for backward compatibility, but not recommended by Microsoft.

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 1:54 am
by MachineCode
netmaestro wrote:1) Code with SetCursorPos in it can be very disturbing to the users. They like to set the cursorpos themselves and tend to get nervous when the little arrow goes shooting off on its own somewhere, even if it comes right back.
Huh? Functionally-wise, this is no different to Rashad's MouseMove() procedure. So, you can't say SetCursorPos_() is any more disturbing than MouseMove(). Both do exactly the same thing.
netmaestro wrote:2) mouse_event and keybd_event are superseded by MS in favor of the newer and more comprehensive SendInput. They still work of course, and will for some time to come for backward compatibility, but not recommended by Microsoft.
I know what you're getting at, but in reality, Microsoft have been threatening to end compatibility for certain API calls for literally decades now, and they've never done it. I don't believe they ever will. And even if they do, it just means our apps just need to be updated to deal with it. Any good app will always be updated anyway, so when the time comes, we just fix the missing API calls at the same time. Not a biggie.

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 1:58 am
by netmaestro
I was referring specifically to what your code accomplished, which is a move followed by a press/release. With SendInput you can set the x and y for where the press/release is to be sent without disturbing the current position of the cursor. Of course mousemoves are mousemoves, no argument there.

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 1:59 am
by MachineCode
netmaestro wrote:I was referring specifically to what your code accomplished, which is a move followed by a press/release.
I see... but I didn't intend for my code to be a running example of move and click. It was meant to show how to do both separately, but I didn't wrap them in separate

Code: Select all

 tags. I should have to avoid confusion, and have edited my post to clarify.

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 2:03 am
by netmaestro
Ok, I get it now. Nevermind what I said about the SetCursorPos :mrgreen:

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 2:04 am
by MachineCode
That's okay. I fully understand how I caused the confusion. My bad! :wink:

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 2:48 am
by Ramihyn_
MachineCode wrote:I know what you're getting at, but in reality, Microsoft have been threatening to end compatibility for certain API calls for literally decades now, and they've never done it. I don't believe they ever will.
16-bit emulation was dropped in Win7, 32-bit driver emulation dropped for 64-bit Win7, DirectSound/DirectMusic/Windows Multimedia all "deprecated" in Win7, the default UAE settings create all kind of "weird" problems for applications who still just store data or configuration files in program directories or worse in the root path of drives - all thanks to developers who ignore Microsoft guidelines for a full decade. Often API's still "partially" work, but only with old codecs or reduced functionality (network mapped or FS filter drives dont work etc.) so the users and support team often notice problems before the (bad) software developers do.

Microsoft sometimes tries to drop API support quicker, but during the beta testing will get a lot pressure from big companies who want to use their old software without changes. So Microsoft gets influenced to keep old APIs in, even if they are only partially functional.

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 6:22 am
by RASHAD
We have problems in this forum
No body read carefully
SetCursorPos_(10,50)
I think everybody in this forum knows well SetCursorPos_()
This snippet is to complete the use of SendInput_() after the fantastic use by infratec

And guys this is a Tricks 'n' Tips forum if you have somthing New to share it, do it
Do not rant please :evil:

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 11:09 am
by infratec
@RASHAD

I demonstrated SendInput_() with mouse events already in 2009:
http://www.purebasic.fr/english/viewtop ... ur&start=0

Ok, no clicks :wink:

Bernd

Re: Simulate Mouse Click-Move Using SendInput_() [ Windows]

Posted: Sun Oct 16, 2011 1:07 pm
by RASHAD
Thanks Bernd :)
At least you pointed me to another great piece of code I did not know anything about it before