Jump to content

Can't call userdefined function with Hotkeyset


Recommended Posts

Hey

I'm writing a script to start other progs with hotkeys.

for my script I need to set a hotkey for a function. Thats no big problem.

HotkeySet( "{F10}", "Hotkeyexecute")

Its like this code. But for my script I need to send a variable to the function. This variable depends to the pressed key.

I tested this code:

HotkeySet( $hotkey, "Hotkeyexecute($arraynumber)")

And this one:

HotkeySet( $hotkey, "Hotkeyexecute("&$arraynumber&")")

But when I start the script and press the comination, set in $hotkey nothing happens.

It only works with this code:

HotkeySet( $hotkey, "Hotkeyexecute")

There isn't a error set or something else.

Please help me.

Link to comment
Share on other sites

HotKeySet second parameter only takes a reference to a function name, which is used to find the function to call. You can not pass an entire line of script to the function. Furthermore, HotKeySet can only call functions that don't need any parameters, either because the function has no parameters or all the parameters have default values and are therefore optional.

Your problem however is a valid one and it needs to be solved in some way. AutoIt has chosen to do this by offering a @HotKeyPressed macro which can be used inside a function that handles a hotkey press. An example of this is:

For $i = 1 To 12
    HotKeySet("{F" & $i & "}", "MyHotkeyHandler")
Next

While 1
    Sleep(500)
WEnd

Func MyHotkeyHandler()
    $key = @HotKeyPressed
    MsgBox(0, "", "You pressed: " & $key)
EndFunc

Alternatively, you can define functions for each key. An example again:

For $i = 1 To 12
    HotKeySet("{F" & $i & "}", "HandlerForF" & $i)
Next

While 1
    Sleep(500)
WEnd

Func HandlerForF1()
EndFunc

Func HandlerForF2()
EndFunc

This last approach also fits with the approach that AutoIt takes, but yet it is not fully supported. For example, binding a HotKey to a non-existant function is not detected by Au3Check and it will not cause the script to hard-crash. Even though there is no way for the script to ever resolve that function during the course of the program (all functions must be known at compile-time, but functions via HotKey are bound at runtime). This is inconvenient for the programmer because it leads to (almost) silent errors, which truth be told, no one ever checks. Therefore, I recommend that you use the first option with @HotKeyPressed.

Edited by Manadar
Link to comment
Share on other sites

  • Moderators

LittleHuba,

AutoIt should change something in this function

Are you volunteering to write the code? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

LittleHuba,

Are you volunteering to write the code? :)

M23

Well there is no need because I did it some time ago :).

The UDF is in my signature and it can be used for passing parameters to functions for HotKeySet, GuiCtrlSetOnEvent and GuiSetOnEvent.

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.
Link to comment
Share on other sites

  • Moderators

martin,

Well, in truth I did I not really think the OP ever would, so it is just as well that you have! :)

I forgot your UDF dealt with HotKeys - I only ever use it for functions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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