Jump to content

Sending a mouse click to a window without focus


Recommended Posts

Hello all,

I know this topic has already been discussed multiple times but I can't figure out what is wrong with my code.
I would like to send a mouse click to a window without focus.

The following piece of code is creating a GUI with a "test" button. Clicking on the "test" button print a "ok" to the console.
When pressing the keys CTRL + P, the VirtualMouseClick() function is called.
This function calls the following :

_SendMessage($hwnd, $WM_LBUTTONDOWN, 1, _WinAPI_MakeLong($x1, $y1))
_SendMessage($hwnd, $WM_LBUTTONUP, 0, _WinAPI_MakeLong($x1, $y1))

Which should do what I want.. but it doesn't. Could you help me with it please ?
Thank you :)

Here is the code:

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)
;~ Opt("MustDeclareVars", 1)

HotKeySet("^p", VirtualMouseClick)

Global $gui
Global $size[2] = [300, 300]

$gui = GUICreate("", $size[0], $size[1])
GUISetOnEvent($GUI_EVENT_CLOSE, "quit", $gui)
GUICtrlCreateButton("Test", 10, 10, 100, 80)
GUICtrlSetOnEvent(-1, "TestOk")
GUISetState(@SW_SHOW, $gui)

ConsoleWrite($gui & @CRLF)


While(True)
    Sleep(200)
WEnd

Func _ArrayFind($array, $item)
    Local $retour = False
    For $i = 0 To UBound($array) -1
        If($array[$i] = $item) Then
            $retour = True
            ExitLoop
        EndIf
    Next
    Return $retour
EndFunc

Func quit()
    Exit
EndFunc

Func TestOk()
    ConsoleWrite("ok" & @CRLF)
EndFunc

Func VirtualMouseClick()
    Sleep(100)
    Local $hwnd = WinGetHandle("[ACTIVE]")
    Local $x1 = 30
    Local $y1 = 50
    Local $isDown = 1
    ConsoleWrite("$hwnd = " & $hwnd & @LF)
;~  _SendMessage($hwnd, $WM_MOUSEMOVE, 0, _WinAPI_MakeLong($x1, $y1))
;~  Sleep(50)
    _SendMessage($hwnd, $WM_LBUTTONDOWN, 1, _WinAPI_MakeLong($x1, $y1))
;~  Sleep(50)
;~  _SendMessage($hwnd, $WM_MOUSEMOVE, 1, _WinAPI_MakeLong($x1, $y1))
    Sleep(50)
    _SendMessage($hwnd, $WM_LBUTTONUP, 0, _WinAPI_MakeLong($x1, $y1))

EndFunc

 

Edited by Keikopute
inactive window -> window without focus
Link to comment
Share on other sites

The handle you're getting in the function is the ACTIVE window's handle, not an inactive window, doesn't sound right to me. :)

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

How are you determining that it's not clicking the window? Is it clicking somewhere that it would indicate that it was clicked? Also, have you looked at ControlClick?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

The (x,y) position I am sending are, accordingly to AutoIt Window Info, the position of the "test" button. That is why I think it should be working.

I tried ControlClick and it worked fine, but I specifically want to use SendMessage because I will need to send a click to a SDL window.

 

Link to comment
Share on other sites

Yes exactly, it's supposed to click it because of those 2 messages :

_SendMessage($hwnd, $WM_LBUTTONDOWN, 1, _WinAPI_MakeLong($x1, $y1))
    _SendMessage($hwnd, $WM_LBUTTONUP, 0, _WinAPI_MakeLong($x1, $y1))

But it doesn't and I don't understand why. Do you have any idea ?

Link to comment
Share on other sites

Just out of curiosity, have you tried using ControlClick instead of using _SendMessage on the application? Just do something like this in you VirtualMouseClick function to see if it works with the application you're trying to automate.

Func VirtualMouseClick()
    Sleep(100)
    Local $hwnd = WinGetHandle("[ACTIVE]")
    Local $x1 = 30
    Local $y1 = 50
    Local $isDown = 1
    ConsoleWrite("$hwnd = " & $hwnd & @LF)
;~  _SendMessage($hwnd, $WM_MOUSEMOVE, 0, _WinAPI_MakeLong($x1, $y1))
;~  Sleep(50)
;~  _SendMessage($hwnd, $WM_LBUTTONDOWN, 1, _WinAPI_MakeLong($x1, $y1))
;~     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') :  _WinAPI_MakeLong($x1, $y1) = ' &  _WinAPI_MakeLong($x1, $y1) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
;~  Sleep(50)
;~  _SendMessage($hwnd, $WM_MOUSEMOVE, 1, _WinAPI_MakeLong($x1, $y1))
;~  Sleep(50)
;~  _SendMessage($hwnd, $WM_LBUTTONUP, 1, _WinAPI_MakeLong($x1, $y1))
    ControlClick($hwnd, "", "", "left", 1, $x1, $y1) ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<
EndFunc   ;==>VirtualMouseClick

This should click in the correct location of the window, regardless of whether it can see the control or not. 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...