Jump to content

Hotkeys referencing functions with arguments


Recommended Posts

So I thought I had this coded right... but AutoIt fails saying that the variable isn't defined...

C:\Documents and Settings\user\Desktop\test.au3 (8) : ==> Variable used without being declared.:

msgbox(0,"",$value)

msgbox(0,"",^ ERROR

Here's a reproduction of the problem.

HotkeySet("{PGUP}", "_Test")

While 1
    Sleep(10)
WEnd

Func _Test(Const $value = 1) ; Testing without Const gives the same error as well. But for the sake of my use, I specify Const
    msgbox(0,"",$value)
EndFunc

Is this normal behavior and I'm not coding this correctly?

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

mechaflash213,

From the Help file page for HotKeySet:

"The called function can not be given parameters. They will be ignored."

So it is hardly surprising that AutoIt considers $value to be undefined. ;)

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

According to the help file: "The called function can not be given parameters. They will be ignored."

Too late ;)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

:doh: missed that bit...

I suppose a workaround would be:

HotkeySet("{PGUP}", "_Test")

While 1
    Sleep(10)
WEnd

Func _Test(Const $value = 1)
    If @HotKeyPressed Then $value = 1 ; <<<<<<<<<<
    msgbox(0,"",$value)
EndFunc
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

mechaflash213,

That will still give you an error because $value is undeclared. :(

But this works: ;)

HotKeySet("{PGUP}", "_Test")
HotKeySet("{PGDN}", "_Test")

While 1
    Sleep(10)
WEnd

Func _Test()
    Local $value = 0
    If @HotKeyPressed = "{PGUP}" Then
        $value = 1
    EndIf
    MsgBox(0, "", $value)
EndFunc   ;==>_Test

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

heh? I tested my code and it works :huh:

All it's doing is looking for @HotKeyPressed to have an assignment then set $value = 1.

If a hotkey isn't pressed, the whole function gets ignored anyways.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

I get an error because $value is defined as a constant. I changed the script a bit. This works:

HotKeySet("{PGUP}", "_Test")
HotKeySet("{PGDN}", "_Test")
HotKeySet("{Exit}", "_Exit")

While 1
    Sleep(10)
WEnd

Func _Test($value = 1)
    If @HotKeyPressed = "{PGUP}" Then $value = 2
    If @HotKeyPressed = "{PGDN}" Then $value = 3
    MsgBox(0, "", $value)
EndFunc   ;==>_Test

Func _Exit()
    Exit
EndFunc

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I get an error because $value is defined as a constant. I changed the script a bit. This works:

HotKeySet("{PGUP}", "_Test")
HotKeySet("{PGDN}", "_Test")
HotKeySet("{Exit}", "_Exit")

While 1
Sleep(10)
WEnd

Func _Test($value = 1)
If @HotKeyPressed = "{PGUP}" Then $value = 2
If @HotKeyPressed = "{PGDN}" Then $value = 3
MsgBox(0, "", $value)
EndFunc ;==>_Test

Func _Exit()
Exit
EndFunc

Yeah I get that at the au3 check. But that's because the au3 check only knows that a Const variable is defined and we are changing that variable. If you force it to run, it works fine (and will work fine in any circumstance because of the nature of how HotKeySet() treats functions)

This is one of the cases where the human mind overrides programming logic ;)

If the function is ever called outside of a hotkey assignment, that line is bypassed since a hotkey wasn't pressed.

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • Moderators

mechaflash213,

This is one of the cases where the human mind overrides programming logic

Whereas if you do as I suggested above and declare the variable within the functions, both human and AutoIt are quite content. ;)

M23

P.S. I know your version with a function parameter works, but it is an offence to good practise to code it that way. :x

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

bah... I ended up creating a wrapper for it since I had to utilize other functions on hotkey press, so I was able to avoid the whole ordeal.

On a side note, for the sake of the topic, the function serves as a tool to cycle through images (personal/business check processing). In the program, there's 3 ways to do this: hotkey, pressing a GUI button, and typing an input value to skip to an image #. If it's a single image skip (hotkey or GUI button) no problem, but I needed the ability to still pass an argument with the value to the function if the user decides to use the 'skip to image' so it knows how many to skip.

Let's pretend I didn't make a wrapper for it already... my only options would be to create a wrapper ( :mellow: ), store the value in a file and retrieve it in the function ( <_< ), or duplicate the function for this purpose only ( :puke: )

Is it still bad practice in this sense? The only problem that I see is that au3 check isn't programmed to look for a function-hotkey relation and to omit the Const variable rule from its check... which it shouldn't since it could miss a real problem with the Const variable being effected in other ways.

It's like wearing a seatbelt... it will save you 99.9% of the time from horrific injuries. But there's that .1% where the seat belt jams and locks on you while your car is on fire... or a head-on collision at 90 mph and you die as opposed to being ejected from the vehicle and thrown into a tree and live ;) . You know those extreme cases lol.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

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