amin84 Posted October 27, 2017 Posted October 27, 2017 Hello, I'm trying to write a hotkey program and I know exactly how to do it but it will be hard coded and the user won't be able to edit it. HotKeySet("{ESC}", "HotKeyPressed") HotKeySet("^!d", "HotKeyPressed") HotKeySet("^+x", "HotKeyPressed") While 1 Sleep(500) WEnd Func HotKeyPressed() Switch @HotKeyPressed Case "{ESC}" Exit Case "^!d" ShellExecute("D:\Docs") Case "^+x" ShellExecute("D:\Docs\My Portables\Burner\AnyBurn\AnyBurn.exe") EndSwitch EndFunc I want the program to read a file that has hotkeys on odd lines and paths on even lines but I don't see how the program will load into itself. Is it even possible? Thanks.
gruntydatsun Posted October 27, 2017 Posted October 27, 2017 yes, it is possible. FileRead and use StringRegexp and the crlf to break the entries up. Using the INI library might be less hassle for you though.
amin84 Posted October 27, 2017 Author Posted October 27, 2017 I know about the reading ini but let's say we read all the hotkeys and the paths that it has to execute. Now how do we generate a function on runtime and execute it? In he example above, that would be HotkeyPressed() function.
gruntydatsun Posted October 27, 2017 Posted October 27, 2017 sorry leomoon, i missed the point there. looks like you can't pass arguments to a function you call from HotKeySet so i guess you'd need to be able to dynamically create the functions based on what you read off the file. does this thread help any?
amin84 Posted October 27, 2017 Author Posted October 27, 2017 The first way is to loop and create functions but hotkeyset needs the function created already: $ini = @ScriptDir&"\hotkeys.ini" Global $aHotkeys = IniReadSection($ini, "Hotkeys") Global $i For $i = 1 To $aHotkeys[0][0] HotKeySet($aHotkeys[$i][0], $i) Next While 1 Sleep(500) WEnd For $i = 1 To $aHotkeys[0][0] Func Eval($i)&"()" ShellExecute($aHotkeys[$i][1]) EndFunc Next The second way is to make one function with cases which is better cuz the hotkeyset already has the function ready. I just don't know how to create the cases with the array: $ini = @ScriptDir&"\hotkeys.ini" Global $aHotkeys = IniReadSection($ini, "Hotkeys") Global $i For $i = 1 To $aHotkeys[0][0] HotKeySet($aHotkeys[$i][0], "HotKeyPressed") Next While 1 Sleep(500) WEnd Func HotKeyPressed() Switch @HotKeyPressed ;Loop through array and create cases Case Else ;do nothing EndSwitch EndFunc
gruntydatsun Posted October 27, 2017 Posted October 27, 2017 check the return value on HotKeySet, i can't get it to work unless it's fed strings, not variables
gruntydatsun Posted October 27, 2017 Posted October 27, 2017 Its a dead quiet day here so I've been mucking around with it $aHotkeys = IniReadSection(@ScriptDir & "\test.ini", "Hotkeys") For $i = 1 To $aHotkeys[0][0] HotKeySet($aHotkeys[$i][0],"HotKeyPressed") Next While 1 Sleep(500) WEnd Func HotKeyPressed() for $x = 1 to UBound($aHotkeys)-1 if @HotKeyPressed = $aHotkeys[$x][0] Then Execute($aHotkeys[$x][1]) Next EndFunc and make a file called test.ini in the root of the program folder with contents: [Hotkeys] +!d=ShellExecute("D:\backup") +!x=ShellExecute("D:\repo") this works for me amin84 1
amin84 Posted October 27, 2017 Author Posted October 27, 2017 O wow. That's exactly what I was looking for. I didn't know about @HotKeyPressed. Thanks!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now