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

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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)
Egypt my love
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

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

Post 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)
Last edited by MachineCode on Sun Oct 16, 2011 2:00 am, edited 1 time in total.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

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

Post 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.
Last edited by MachineCode on Sun Oct 16, 2011 1:58 am, edited 1 time in total.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

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

Post 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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post by netmaestro »

Ok, I get it now. Nevermind what I said about the SetCursorPos :mrgreen:
BERESHEIT
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

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

Post by MachineCode »

That's okay. I fully understand how I caused the confusion. My bad! :wink:
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Ramihyn_
Enthusiast
Enthusiast
Posts: 314
Joined: Fri Feb 24, 2006 9:40 am

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

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post 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:
Egypt my love
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Thanks Bernd :)
At least you pointed me to another great piece of code I did not know anything about it before
Egypt my love
Post Reply