Jump to content

Recommended Posts

Posted

Hi, Is there any way I can detect if a button on a piece of software has been pressed? I need to change what a button on some software does by running my own function when pressed.

For example. If I have the windows "calculator" program open. Whenever the = sign is pressed, I want a message box to pop up saying "the result has been calculated"

Thanks,

Matt.

  • Moderators
Posted

  mattschinkel said:

Hi, Is there any way I can detect if a button on a piece of software has been pressed? I need to change what a button on some software does by running my own function when pressed.

For example. If I have the windows "calculator" program open. Whenever the = sign is pressed, I want a message box to pop up saying "the result has been calculated"

Thanks,

Matt.

SetWindowsHookEx + CallNextHook

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.

Posted

Try this:

#include <Misc.au3>
#include <WinAPI.au3>
;

Global $hUser32_Dll = DllOpen("User32.dll")

HotKeySet("^e", "_Quit")

While 1
    If _IsPressed("01", $hUser32_Dll) Then
        ;Waiting untill the button is released
        While _IsPressed("01", $hUser32_Dll)
            Sleep(10)
        WEnd
        
        $hWindow = _WinGetHoveredHandle()
        $iCtrlID = _ControlGetHoveredID()
        
        $sWinClass = _WinAPI_GetClassName($hWindow)
        $sCtrlData = ControlGetText($hWindow, "", $iCtrlID)
        
        If $sWinClass = "SciCalc" And $iCtrlID <> 0 And $sCtrlData = "=" Then
            $sResult = Number(ControlGetText($hWindow, "", "Edit1"))
            ToolTip("The result has been calculated: " & $sResult, Default, Default, "Calc Info", 1, 5)
        Else
            ToolTip("")
        EndIf
    EndIf
    
    Sleep(10)
WEnd

Func _ControlGetHoveredID()
    Local $iOld_Opt_MCM = Opt("MouseCoordMode", 1)
    
    Local $hRet = DllCall("user32.dll", "int", "WindowFromPoint", "long", MouseGetPos(0), "long", MouseGetPos(1))
    
    $hRet = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hRet[0])
    If $hRet[0] < 0 Then $hRet[0] = 0
    
    Opt("MouseCoordMode", $iOld_Opt_MCM)
    
    Return $hRet[0]
EndFunc

Func _WinGetHoveredHandle()
    Local $iOld_Opt_MCM = Opt("MouseCoordMode", 1)
    Local $aRet = DllCall("user32.dll", "int", "WindowFromPoint", "long", MouseGetPos(0), "long", MouseGetPos(1))
    
    Opt("MouseCoordMode", $iOld_Opt_MCM)
    
    $aRet = DllCall("User32.dll", "hwnd", "GetAncestor", "hwnd", $aRet[0], "uint", 2) ;$GA_ROOT
    
    Return HWnd($aRet[0])
EndFunc

Func _Quit()
    DllClose($hUser32_Dll)
    Exit
EndFunc

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

u can do a GuiCtrlRead on the button. Use AutoIt Window Info tool to get the necessary info. Just check if the button is higghlighted with a black box when u drag the finder cursor on it. If it does, then that means that the ctrl is recognized as a button by autoit. N u can then easily read whether the button is pressed.

Cheers.

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com
Posted

  Manjish said:

u can do a GuiCtrlRead on the button. Use AutoIt Window Info tool to get the necessary info. Just check if the button is higghlighted with a black box when u drag the finder cursor on it. If it does, then that means that the ctrl is recognized as a button by autoit. N u can then easily read whether the button is pressed.

Cheers.

GUICtrlRead will not work on external applications :)

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • 6 years later...
Posted (edited)

I am trying to run your code on this application can you point me , why it's not working , I just need  when click "=" a message to pop

post-81487-0-67435900-1424709457_thumb.g

Edited by antonioj84
  • Moderators
Posted (edited)

Things have probably changed in 6 years! Please start your own help thread with a reference link to the code that isn't working.  In your support thread, be very specific on what you've done to debug what isn't working.

Edit:

A little hint, the "class" is different, so you'll have to change that.

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.

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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