Jump to content

Detect which button the user selects


Barfly
 Share

Recommended Posts

I may be missing something really obvious in the Autoit help file, but it's been a long day...

Is there a way to tell which button in a window a user clicked on? I can see the button (am using 'ControlGetHandle') but then how do I know; after the user has clicked on one of them; which one he clicked on? This is basically what I have at the moment...

Func test()
If WinExists("WinName") Then
    $yes = ControlGetHandle("WinName", "", "TButton2")
    $no = ControlGetHandle("WinName", "", "TButton1")
    WinWaitClose("WinName")
               ; if $yes was clicked then
               ;YesFunc() 
               ;Elseif $no was clicked then
               ;NoFunc()
               ;EndIf
    Exit
    EndiF
EndFunc

Any ideas?

Link to comment
Share on other sites

ok, stupid question... is this a GUI you're making with ur script?

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

  • Moderators

You can use... ControlGetPos() / MouseGetPos() / and _Ispressed() a combination of the 3 if it is a 3rd Party application.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You can use... ControlGetPos() / MouseGetPos() / and _Ispressed() a combination of the 3 if it is a 3rd Party application.

Would that go something like this:

If WinExists("WinName") Then
    $yes = ControlGetHandle("WinName", "", "TButton2")
    $no = ControlGetHandle("WinName", "", "TButton1")
    WinWaitClose("WinName")
                if _IfPressed($yes) then
               YesFunc() 
               Elseif _IfPressed($no) then
               NoFunc()
               EndIf
    Exit

:lmao: How would knowing the position of the control help?

Link to comment
Share on other sites

  • Moderators

I was thinking something like this:

Opt('MouseCoordMode', 2)

While 1
    If WinExists("WinName") Then
        $CPosYes = ControlGetPos('WinName', '', 'TButton2')
        $CPosNo = ControlGetPos('WinName', '', 'TButton1')
        $MPos = MouseGetPos()
        If $MPos[0] >= $CPosYes[0] And $MPos[0] <= $CPosYes[0] + $CPosYes[3] _ 
            And $MPos[1] >= $CPosYes[1] And $MPos[1] <= $CPosYes[1] + $CPosYes[4] And _IsPressed('01') Then YesFun()
        
        If $MPos[0] >= $CPosNo[0] And $MPos[0] <= $CPosNo[0] + $CPosNo[3] _ 
            And $MPos[1] >= $CPosNo[1] And $MPos[1] <= $CPosNo[1] + $CPosNo[4] And _IsPressed('01') Then NoFun() 
    EndIf
    Sleep(10)
WEnd

Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc

I think that's right, I didn't double check... But at least you'll get the concept.

Edit: Had TButton2 on both contorl names :lmao:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

While 1
    If WinExists("WinName") Then
        $CPosYes = ControlGetPos('WinName', '', 'TButton2')
        $CPosNo = ControlGetPos('WinName', '', 'TButton1')
        $MPos = MouseGetPos()
        If $MPos[0] >= $CPosYes[0] And $MPos[0] <= $CPosYes[0] + $CPosYes[3] _ 
            And $MPos[1] >= $CPosYes[1] And $MPos[1] <= $CPosYes[1] + $CPosYes[4] And _IsPressed('01') Then YesFun()
        
        If $MPos[0] >= $CPosNo[0] And $MPos[0] <= $CPosNo[0] + $CPosNo[3] _ 
            And $MPos[1] >= $CPosNo[1] And $MPos[1] <= $CPosNo[1] + $CPosNo[4] And _IsPressed('01') Then NoFun() 
    EndIf
    Sleep(10)
WEnd

Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc

I see what you are trying to do, and I have tried to get this going. But for some reason my 'Yes' button control always equals 69x64 but my mouse postion equals something like 256x234 (depending...). I have tried all the Opt(MouseCoordMode) settings to no avail.

What I need is to know, when MouseDown (or control is clicked etc.), whick one is selected. So if I should be using the position of the mouse in relation to the control, how do I get these values to relate to each other? AND how do I read the values ONLY when the control is clicked?

:lmao:

...The frustration is going to send me over the edge...

Edited by Barfly
Link to comment
Share on other sites

  • Moderators

I made a mistake, I put [4] when it should have been [3] and [3] when it should have been [2], I coded it blind, and never tested it, but I have tested it since and the below code does work... you can take out the ToolTip() debug I put in there... But you must Use Opt('MouseCoordMode', 2).

Opt('MouseCoordMode', 2)
While 1
    If WinExists("Test GUI") Then
        $CPosYes = ControlGetPos('Test GUI', 'Clear', 'Button1')
        $CPosNo = ControlGetPos('Test GUI', 'Submit', 'Button2')
        $MPos = MouseGetPos()
        If IsArray($CPosNo) And IsArray($CPosYes) And IsArray($MPos) Then
            If $MPos[0] >= $CPosYes[0] And $MPos[0] <= $CPosYes[0] + $CPosYes[2] _
                And $MPos[1] >= $CPosYes[1] And $MPos[1] <= $CPosYes[1] + $CPosYes[3] And _IsPressed('01') Then YesFun()
            
            If $MPos[0] >= $CPosNo[0] And $MPos[0] <= $CPosNo[0] + $CPosNo[2] _
                And $MPos[1] >= $CPosNo[1] And $MPos[1] <= $CPosNo[1] + $CPosNo[3] And _IsPressed('01') Then NoFun()
            EndIf
            ToolTip('Mouse x: ' & $MPos[0] & @CRLF & 'Mouse y: ' & $MPos[1] & @CRLF & 'Control Yes x: ' & $CPosYes[0] & @CRLF & 'Control Yes y: ' & $CPosYes[1] & @CRLF & 'Control Yes width: ' & _
            $CPosYes[2] & @CRLF & 'Control Yes height: ' & $CPosYes[3] & @CRLF & 'Control No x: ' & $CPosNo[0] & @CRLF & 'Control No y: ' & $CPosNo[1] & @CRLF & _ 
            'Control No width: ' & $CPosNo[2] & @CRLF & 'Control No height: ' & $CPosNo[3], 0 , 0)
        EndIf
    Sleep(10)
WEnd

Func YesFun()
    MsgBox(0, '1', 'made it to yes')
EndFunc

Func NoFun()
    MsgBox(0, '2', 'made it to no')
EndFunc

Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

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...