Sign in to follow this  
Followers 0
therks

Question about macros.

9 posts in this topic

So I initially thought that all macros were predefined, and filled with whatever values they needed, and if they weren't being used they were empty.

ie: If you tried to access @HotKeyPressed in a non-hotkey called function, it would just return empty, same for @GUI_CtrlID. But it appears I'm wrong.

For a GUI program I've been working on, I'd hoped to create a generic function that would handle a lot of the little bits and pieces I needed to accomplish. I had assigned HotKeys, and GuiEvents to this function, and was getting odd results at first, but now I think I've figured it out. I guess that until a macro would have to be created, it doesn't exist at all, is that right? And it's value will stay the same until it needs to be changed.

ie: If I try to access @HotKeyPressed before I call a HotKey function, the macro is undefined. If I try to access it after I call a HotKey function, the value is whatever it was last set as. It's basically a glorified global variable (is there any difference?).

Anyway, what I'm really looking for is code like the following to work (it's a silly example, but for the sake of brevity I'm not going to post a whole bunch of useful code).

#include <GuiConstants.au3>
Opt('GuiOnEventMode', 1)

HotKeySet('^y', 'GenericFunction')

GuiCreate('GUI')
GuiSetOnEvent($GUI_EVENT_CLOSE, 'GenericFunction')

$bt = GuiCtrlCreateButton('Click', 10, 10, 100, 100)
GuiCtrlSetOnEvent(-1, 'GenericFunction')

GuiSetState()

While 1
    Sleep(100)
WEnd

Func GenericFunction($param = '')
    If @GUI_CtrlID = $bt Then
        MsgBox(0, 'GenFunc', 'AWESOME!')
    ElseIf @HotKeyPressed = '^y' Then
        Exit
    ElseIf @GUI_CtrlID = $GUI_EVENT_CLOSE Then
        GuiSetState(@SW_MINIMIZE)
    ElseIf $param <> '' Then
        MsgBox(0, 'Param:', $param)
    EndIf
EndFunc

This script will error unless you do each of the following in order:

1. Click the button

2. Press Ctrl+Y (which will not activate the Hotkey part of the function, because @GUI_CtrlID is still = $bt)

3. Click the [x] on the GUI

Unless the function is called alone, $param is always undefined even though it's an optional parameter.

Think there's any chance that a function like the above could work without erroring? I thought the macros would work like @error. Always available, just empty if not specifically set, and the $param should be coming across as an empty string if it's not set when the function is called, shouldn't it?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I just moved a few things around and works fine, but nice ..um bug?

@GUI_CtrlID doesn't have a value unless something is pressed, clicked, or other type of event. So when you check the state of it before you do one of those, well... error.

I think @GUI_CtrlID should be set to null or 0 with the creation of the gui. That would solve it.

You can't set it to a value easily, so I normally only have a function that has that macro called on an event, like GuiCtrlSetOnEvent()

Anyway this seems to work...

#include <GuiConstants.au3>
Opt('GuiOnEventMode', 1)

HotKeySet('^y', 'GenericFunction')

GuiCreate('GUI')
GuiSetOnEvent($GUI_EVENT_CLOSE, 'GenericFunction')

$bt = GuiCtrlCreateButton('Click', 10, 10, 100, 100)
GuiCtrlSetOnEvent(-1, 'GenericFunction')

GuiSetState()

While 1
    Sleep(100)
WEnd

Func GenericFunction($param = '')
    If @HotKeyPressed = '^y' Then
        Exit
    elseIf $bt=@GUI_CtrlID Then
        MsgBox(0, 'GenFunc', 'AWESOME!')
    ElseIf @GUI_CtrlID = $GUI_EVENT_CLOSE Then
        GuiSetState(@SW_MINIMIZE)
    ElseIf $param <> '' Then
        MsgBox(0, 'Param:', $param)
    EndIf
EndFunc
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

Those macros are valid only in the function related. in your case you handle the 2 events in one unique function so you will not be able to know in which context your function has been called.

I think you need 2 differents functions using common functions if your logic is OK with. :)

Share this post


Link to post
Share on other sites

Yeah the logic is fine, I was just trying to be as efficient as possible for what I was doing.

Thanks for the replies.

Share this post


Link to post
Share on other sites

When I run your test I get the following error in both the production AutoIT and beta56:

C:\Program Files\AutoIt3\Examples\JeffTests\test of hotkeys+functions.au3(20,8) : ERROR: undefined macro.

    If @HotKeyPressed

~~~~~~~^

C:\Program Files\AutoIt3\Examples\JeffTests\test of hotkeys+functions.au3 - 1 error(s), 0 warning(s)

Is this expected, or am I missing something (again...)

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

When I run your test I get the following error in both the production AutoIT and beta56:

Is this expected, or am I missing something (again...)

<{POST_SNAPBACK}>

I am not sure where you get this error can you post a script which reproduce it? :)

Thanks

Share this post


Link to post
Share on other sites

JP, that's the same error I would get if I ran my script, and clicked the [x] before I pressed the hotkey. The @HotKeyPressed macro doesn't get set before that and as such doesn't exist. That was the core of my whole post.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I can't seem to get the error with my code:

My code just puts the test for hotkey first, and since you exit when finding it, you don't get to the @GUI_CtrlID check. If you got to the function by the event, then the event gave a value to @GUI_CtrlID. :)

I personally would have made seperate functions, but then again I am a minimalist at times and less code>more code, in many cases.

Used beta...3.1.1.38, guess I need to download again, new stuff to test. :">

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

I think he might have used the original code that I posted. That's the one where I get the error.

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