Sign in to follow this  
Followers 0
greenmachine

hotkey function with optional parameters

9 posts in this topic

I'm making a script that does about a million things, and a few of them deal with loading files. For some reason, calling the load function doesn't work right. Here's a super shortened script that shows what I mean.

Global $fileloaded
HotKeySet ("1", "LoadAFile")
HotKeySet ("2", "ReloadTheFile")
HotKeySet ("3", "UseBlankFile")
HotKeySet ("9", "GODIE")

Func UseBlankFile()
    LoadAFile("BlankFile.txt")
EndFunc

Func ReloadTheFile()
    If $fileloaded <> "" Then
        LoadAFile($fileloaded)
    EndIf
EndFunc

Func LoadAFile($load = "")
    If $load = "" Then
        MsgBox (0, "load", "load is empty string")
        $load = InputBox ("loadfile", "type in a file name")
    EndIf
    If $load = "BlankFile.txt" Then
        MsgBox (0, "loadedblank", "blankfile.txt was loaded")
    EndIf
    $fileloaded = $load
EndFunc

Func GODIE()
    Exit
EndFunc


While 1
    Sleep (100)
WEnd

When I run this script, pressing 1 should let you input a file through the input box (because the $load is optional in the function and set to "" if nothing is called, right?). But... it doesn't. It gives me an error saying $load isn't declared.

It might be that I don't understand these optional parameters, but I thought I was doing it right. Any help would be appreciated.

Yes, my exit function is called GODIE - I'm a little frustrated by my script not working....

Share this post


Link to post
Share on other sites



Mabey:

Global $fileloaded
HotKeySet ("1", "LoadAFile")
HotKeySet ("2", "ReloadTheFile")
HotKeySet ("3", "UseBlankFile")
HotKeySet ("9", "GODIE")

Func UseBlankFile()
    LoadAFile("BlankFile.txt")
EndFunc

Func ReloadTheFile()
    If $fileloaded <> "" Then
        LoadAFile($fileloaded)
    EndIf
EndFunc

Func LoadAFile($load = "")
    If Not IsDeclared("load") Then
        MsgBox (0, "load", "load is empty string")
        $load = InputBox ("loadfile", "type in a file name")
    ElseIf $load = "BlankFile.txt" Then
        MsgBox (0, "loadedblank", "blankfile.txt was loaded")
    EndIf
    $fileloaded = $load
EndFunc

Func GODIE()
    Exit
EndFunc


While 1
    Sleep (100)
WEnd
I would recommend using FileOpenDialog().

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I see that you used: If Not IsDeclared("load"). While that should work, why does it have to be that way? In all sorts of other functions, there are optional strings that don't have to use that statement. It seems like my way should work...

And I am using FileOpenDialog. This was just an example to show my problem. It's not supposed to do anything. The real script does all the real stuff.

Example.. _TempFile does not need any parameters to work, and the first thing it does is use one of the optional parameters. I thought mine would do that too.

Func _TempFile($s_DirectoryName = @TempDir, $s_FilePrefix = "~", $s_FileExtension = ".tmp", $i_RandomLength = 7)
    Local $s_TempName
  ; Check parameters
    If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @TempDir ; First reset to default temp dir
    If Not FileExists($s_DirectoryName) Then $s_DirectoryName = @ScriptDir; Still wrong then set to Scriptdir
  ; add trailing \ for directory name
    If StringRight($s_DirectoryName, 1) <> "\" Then $s_DirectoryName = $s_DirectoryName & "\"
  ;
    Do
        $s_TempName = ""
        While StringLen($s_TempName) < $i_RandomLength
            $s_TempName = $s_TempName & Chr(Random(97, 122, 1))
        WEnd
        $s_TempName = $s_DirectoryName & $s_FilePrefix & $s_TempName & $s_FileExtension
    Until Not FileExists($s_TempName)
    
    Return ($s_TempName)
EndFunc ;==>_TempFile
Edited by greenmachine

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I guess hotkeys don't use any prams whatsoever...

[edit]

If 'If Not IsDeclared("load")' is bad you could check if it was started by a hotkey by using something like "@HotKeyPressed".

Edited by gamerman2360

Share this post


Link to post
Share on other sites

Update:

w0uter, your idea worked. Thanks.

gamerman2360: the thing is, I was only using hotkeys for the example. In my regular script, I'm using GUI Onevent functions to call... and I guess they do the same thing as hotkeys.

OK I figured it out. Watch this:

Add HotKeySet ("4", "Calltest") to the top, and add this function anywhere...

Func Calltest()

Call ("LoadAFile")

EndFunc

That gives the same error, which means I know what's going on. Hotkeys and GUIOnEvent functions use Call( "function" ) instead of just Function(). That makes a big difference.

Share this post


Link to post
Share on other sites

:\, why doesn't Call() use any of the variables? Not even the default ones?

Share this post


Link to post
Share on other sites

Are you planning to have the hotkeysets to be on all the time? If so, you may run into problems with other applications that use the same keys. You may wish to do a winactive to have the hotkeyset work only while the window you wish to use is active.

(Hope I'm explaining that right)

Share this post


Link to post
Share on other sites

Are you planning to have the hotkeysets to be on all the time? If so, you may run into problems with other applications that use the same keys. You may wish to do a winactive to have the hotkeyset work only while the window you wish to use is active.

(Hope I'm explaining that right)

You are definitely explaining it correctly, but like I said above, I'm not actually using HotKeys in my real script. That was just examples. In my real script, I use buttons and menuitems. But hotkeys were just simplifying things for the example.

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