Jump to content

Recommended Posts

Posted (edited)

This UDF allows to catch an event (pressed/released) from specific window's control.

Header:

; #FUNCTION# ====================================================================================================


; Name...........: _WinControlSetEvent
; Description ...: Sets an event for Window control.
; Syntax.........: Func(hWindow, nCtrlID, $sCallOnHoldFunc [, $sOnHoldParams [, $sCallOnReleaseFunc [, $sOnReleaseParams]]])
;
; Parameters ....: $hWindow         - Parent window handle (or class name) that have the control to set event for.
;                 $nCtrlID          - CtrlID (or control text) of the control to set the event for.
;                 $sCallOnHoldFunc  - Function name to call when event is fired (control is pressed *down*).
;                 $sOnHoldParams      - [Optional] Extra parameters to pass to the called function ($sCallOnHoldFunc).
;                 $sCallOnReleaseFunc - [Optional] Function to call when event is fired (control is released after pressing down).
;                 $sOnReleaseParams   - [Optional] Extra parameters to pass to the called function ($sCallOnReleaseFunc).
;
; Return values .: Always return 1.
;
; Author ........: G.Sandler (a.k.a MrCreatoR).
; Modified.......: 22.01.2009, 20:00
;
; Remarks .......: 1) This UDF includes OnAutoItExit function to release the callback/hooks resources.
;                 2) Blocking of $sCallOnHoldFunc/$sCallOnReleaseFunc function by window messages with commands
;                    such as "Msgbox()" can lead to unexpected behavior, the return to the system should be as fast as possible!
;
; Related .......: _WinCtrlEvents_Handler, _ControlGetHoveredID, _WinGetHoveredHandle, _OnAutoItExit
; Link ..........; 
; Example .......; Yes
; ====================================================================================================

Example:

#include <WinControlSetEvent.au3>

;============== Calc Demo ==============
;In this example we use the main loop to avoid blocking of mouse processing from inside CalcGetResult_Proc() function.

HotKeySet("^q", "_Quit")

$iGetCalcResults = False

Run("Calc.exe")

For $i = 1 To 9
    _WinControlSetEvent("[CLASS:SciCalc]", $i, "", "", "CalcGetResult_Proc", "'" & $i & "' Button has been pressed")
Next

_WinControlSetEvent("[CLASS:SciCalc]", "=", "", "", "CalcGetResult_Proc", "'=' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "/", "", "", "CalcGetResult_Proc", "'/' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "*", "", "", "CalcGetResult_Proc", "'*' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "-", "", "", "CalcGetResult_Proc", "'-' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "+", "", "", "CalcGetResult_Proc", "'+' Button has been pressed")

_WinControlSetEvent("[CLASS:SciCalc]", "Button28", "", "", "CalcGetResult_Proc", "'X' Button has been pressed")

While 1
    Sleep(10)
    
    If $iGetCalcResults <> False Then
        $iParam = $iGetCalcResults
        $iGetCalcResults = False
        
        If $iParam = 168 Then Exit ;Calc closed
        
        $sCtrl_Data = ControlGetText("[CLASS:SciCalc]", "", $iParam)
        
        If $sCtrl_Data = "=" Then
            Local $sResult = StringStripWS(ControlGetText("[CLASS:SciCalc]", "", "Edit1"), 3)
            If StringRight($sResult, 1) = "," Then $sResult = StringTrimRight($sResult, 1)
        
            ToolTip("The result has been calculated: " & $sResult, Default, Default, "Calc Info", 1, 5)
        ElseIf $sCtrl_Data <> "" Then
            ToolTip("Calc Button Pressed: " & $sCtrl_Data, Default, Default, "Calc Info", 1, 5)
        EndIf
    EndIf
WEnd

;Warning: blocking of this function by window messages with commands such as "Msgbox()" can lead to unexpected behavior,
;the return to the system should be as fast as possible !!!
Func CalcGetResult_Proc($sParams, $hWnd, $nCtrlID)
    ConsoleWrite("Passed params: " & $sParams & @CRLF)
    
    $iGetCalcResults = $nCtrlID
EndFunc

Func _Quit()
    Exit
EndFunc

WinControlSetEvent.zip

Edited by MrCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

Nice UDF.

I came up with another way to trigger actions while not blocking the message queue and not using the main loop. I used it for my MD5 gui script. The basic idea is to create a hidden GUI with a dummy control (GuiCtrlCreateDummy()), then when you want to do something, you send it a message with GuiCtrlSendToDummy() and pass it some identifying data. That data can be read in the dummy's OnEvent function with GuiCtrlRead() to determine what it needs to do. Sending the event is the last thing in your message loop function before returning to the main script. This approach worked great for me, since I obviously didn't want to be hashing huge files and have the script stuck in the message loop.

Posted

  wraithdu said:

Nice UDF.

Thanks.

  wraithdu said:

I came up with another way to trigger actions while not blocking the message queue and not using the main loop. I used it for my MD5 gui script. The basic idea is to create a hidden GUI with a dummy control (GuiCtrlCreateDummy()), then when you want to do something, you send it a message with GuiCtrlSendToDummy() and pass it some identifying data. That data can be read in the dummy's OnEvent function with GuiCtrlRead() to determine what it needs to do. Sending the event is the last thing in your message loop function before returning to the main script. This approach worked great for me, since I obviously didn't want to be hashing huge files and have the script stuck in the message loop.

But it's required OnEvent mode enabled, not all of us (and not for all our scripts) using this mode.

And in a matter of fact, this can be done seperately by you (as user), the event function just give you the needed trigger, what will happend next it's up to you.

P.S

To avoid the main loop inolving, we can use Adlib*:

Func _Event_Trigger_Proc($sParams, $hWnd, $nCtrlID)
    ConsoleWrite("Passed params: " & $sParams & @CRLF)
    
    $iGetCalcResults = $nCtrlID
    AdlibEnable("_Event_Main_Proc", 1)
EndFunc

Func _Event_Main_Proc()
    AdlibDisable()
    ;Here we can do whatever we need when event is triggered
EndFunc

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • 5 months later...
Posted (edited)

Thanks Mr.Creator,

I just downloaded it, didn't test yet, bu by sure it would solve many problems I had .

Jose

Edited by joseLB

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
  • Recently Browsing   0 members

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