Sign in to follow this  
Followers 0
martin

functions with parameters in OnEvent and HotKeys

54 posts in this topic

#1 ·  Posted (edited)

The UDF function SetOnEventA can replace the inbuilt AutoIt functions GuiCtrlSetOnEvent, GuiSetOnEvent, HotKeySet and TraySetOnEvent providing an easy way for an event to call functions with parameters.

Example1 - no parameters

GuiCtrlCreateButton("Open",20,20,80,22)
SetOnEventA(-1,"OpenIt");same as using GuiCtrlSetOnEvent because no parameters listed for the function "OpenIt"

Example 2 - 3 parameters
Parameters are added to the SetOnEvent function in 2 parts. The first part tells SetOnEvent whether the parameter is a value or a variable, and the second part is the value or variable to be passed to the function.


GuiCtrlCreateButton("Open",20,20,80,22)
SetOnEventA(-1,"Function1",$paramByVal,$param1,$ParamByVal, $Param2,$ParamByRef, "$VarABC")

where $ParamByVal and $ParamByRef are constants defined in the UDF.
$ParamByVal means the following parameter for the function is a value. It could be 12 or $SomeVar but whatever it is the value will be used.
$ParamByRef means the following parameter is the variable to use. It must be enclosed in quotes as shown above (but with or without the $ prefix). Then, when the function for the event is called the parameter passed will be the value of that variable at the time of the event.
So Function1 will appear like this in the script.

Func Function1($a,$b)

If within the function you need to know the Control which created the event then instead of using @GUI_CtrlId use GetCtrlID() or GetCtrlHandle().
If within the function you need to know the drag ids then use the function EventGetDragIDs

Example 3 - Set a Hotkey for Alt k to call the function "StartKerning" with 2 parameters.

SetOnEventA("!k","StartKerning",$ParamByVal,26,$ParamByRef,"$margin")

Example 4 - Set a Gui event
For gui events the first parameter must be passed by Val and the value must either be 0 or the handle of the window the event is for.
If you only have one window then the gui handle is not relevant but it must still be the first parameter.


SetOnEventA($GUI_EVENT_CLOSE,"AllDone", $paramByVal, $gui,$paramByRef,"$rrdd")

Example 5 - Set a Tray menu item event (Version 12 of the UDF or later)


$iTrayIt1 =TrayCreateItem("BackGround")
TraySetOnEventA(-1,"SetGuiCol", $paramByVal, $iLastGuiCol)

Limitations
I have only allowed for the function to be called to have up to 5 parameters. (Instructions are in the UDF on how to increase this.) A better solution would be to write your function to have an array as a parameter.
If a parameter is a variable then it has to be a Global variable.
Although you can have variables passed as parameters, ByRef for a parameter in the function called will have no effect.




Here is the udf with an example
OnEventFunc downloader   But the latest udf is attached to post #49 untill I upload it and correct the link.

UPDATES:

12th December 2008
UDF changed to a version which doesn't need you to specify the number of parameters passed to the on event function when you call SetOnEventA. It makes things simpler but it isn't compatible with the earlier version. So I've kept the old function SetOnEvent which simply calls the new function without the unnecessary parameter. New scripts can call the new function which is SetOnEventA.

12th Jan 2009. Error in last version corrected. Thanks to FireFox for pointing out something was wrong.

4th Feruary 2009 -V10. Added setting Gui Events, thanks to GaRydelaMer

5th Feb 2009 -V11 corrected error in HotKey event setting in the udf and error in the example.

27th February 2010. Added TraySetOnEventA for Tray menu Items. (Thanks to FireFox)

14th Oct 2011. Corrected forgetting to declare $iParCount in 2 functions. (Thanks to ovideo.)

30th August 2014 Correct overlooking removing a function for an event and allowed for no parameters. (Thanks to PsiLink)

mgrefOnEvent

Edited by martin
2 people like this

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites



I am not at home, so I can't really test this out, however if this does what it's supposed to do, thank you very much! I've always wanted to pass parameters using GUICtrlSetOnEvent, I really hope this works.

Thanks for sharing :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

@Martin

Very nice, save us some time.

Suggestion : Maybe remame the Include to OnEventFunc , wich makes more sence (at least to me ?!)

And regarding the stored variables. Can be solved using "ByRef" in the Func.

SetOnEvent($par0, $Par1, $Par2 = '', $Par3 = '', $Par4 = '', $Par5 = '', $Par6 = '', $par7 = '')

Regards

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

VERY NICE

I will test this, this is a function I have searched for a long time.

NICE WORK !!!!!

Share this post


Link to post
Share on other sites

@Martin

Very nice, save us some time.

Suggestion : Maybe remame the Include to OnEventFunc , wich makes more sence (at least to me ?!)

And regarding the stored variables. Can be solved using "ByRef" in the Func.

SetOnEvent($par0, $Par1, $Par2 = '', $Par3 = '', $Par4 = '', $Par5 = '', $Par6 = '', $par7 = '')

Regards

ptrex

Calling the udf OnEventFunc does sound better, I'll change it.

Using ByRef hadn't occurred to me, and I'll play with that idea. If it works then it means my understanding of ByRef was wrong.

Thanks for your input ptrex.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Using ByRef hadn't occurred to me, and I'll play with that idea.

Thanks for your input ptrex.

No, I'm sure ByRef doesn't help.

Presumably "ByRef $a" uses a pointer to $a, but when you save $a you don't save the pointer but the value.

I have often tried to find a way to get pointers to variables but have never succeeded.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@Martin

I realy look into the script deap enough to see if Byref is solving you issue.

If I see the time I will try to dig into this a bit deaper.

regards,

ptrex

I think I can solve it by copying the way that DllCall is done and specify the parameter types. I can specify whether the parametrs are passed by value or reference like this

Const $paramByVal = 0
Const $ParamByRef = 1

$temp = guiCtrlCreateButton(............
OnEvent($temp,"Functiontocall",$NumOfParams,$paramByRef,"$a",$paramByRef,"$b",$paramByVal,$c,$paramByVal,123)

so if you want to pass the parameter by value you specify $paramByVal and use the actual variable or value you want, but if you want the parameter to be a variable then you specify $paramByRef and enclose the variable name in quotes.

Then I only have to have a way to store and retrieve that information and I've done it.

I'll work on that idea until someone stops me.

EDIT: Nobody stopped me. Updated first post to have variables passed.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Correction made to the UDF because I assumed that all controls have handles. Some controls, such as menu items, don't have handles.

First post updated.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

@martin

Very good, i like the concept behind the idea! (and the idea itself of course) :)

Thanks for sharing.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

@martin

Very good, i like the concept behind the idea! (and the idea itself of course) :)

Thanks for sharing.

Than you MsCreatoR.

As you see, you helped me with an important detail.

I think the UDF can be useful in scripts like the one gcue is developing. He reads data from an ini file about groups. For each group he creates a button, and for each button he has a context menu. He can end up with several pages of buttons. When a button or context menu item is clicked the function which is called needs a few details about the group. With this udf he can set the information as parameters.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

@all

Still wondering why it is not supported natively in au3.

Anyone got a clue ?

regards

ptrex

Valik has started a topic about this issue: Callback functions need user-defined parameter


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Valik has started a topic about this issue: Callback functions need user-defined parameter

Ah yes, that made me realize I can use the same function for HotKeys with a small mod, so I have updated the first post so that the SetOnEvent function can be called with either a Control ID or a HotKey sequence.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Error in latest udf corrected. The third line of the function SetOnEvent had a typo.

The incorrect line is

$parcount = (@numparams - 2)/2

the correct line is

$iParCount = (@numparams - 2)/2

The correction has been uploaded.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

@martin

Very nice work! Thank you! :)

Share this post


Link to post
Share on other sites

@martin

Thanks for have fixed error ! Works great now :)

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

The concept looks great and I can find a place for it in a new script I'm working on.

BUT.........

I just tried to download the zip (twice) and it only contains a "short cut" no script.

Could you rezip and upload again.

Thanks

John Morrison

Edited by storme

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

The concept looks great and I can find a place for it in a new script I'm working on.

BUT.........

I just tried to download the zip (twice) and it only contains a "short cut" no script.

Could you rezip and upload again.

Thanks

John Morrison

That's correct, it contains a shortcut, but you should read the post where the link is :)

Just double click on the shortcut and you will get the udf.

EDIT:

I have changed the first post and now there is a link to my downloader which I hope will make getting my scripts more straightforward.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

That's correct, it contains a shortcut, but you should read the post where the link is :lmao:

I didn't see anything about clicking on a shortcut... :)

Just double click on the shortcut and you will get the udf.

Short cuts can do anything so I am VERY carefull about using them from unverified soruces.

EDIT:

I have changed the first post and now there is a link to my downloader which I hope will make getting my scripts more straightforward.

Yep looks good. I've downloaded it and I'm having a good play with it now.

Great work and VERY usefull. I was only thinking about how to do what you have done just before it came to my attention. :think:

Thank you

John Morrison

Share this post


Link to post
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
Sign in to follow this  
Followers 0