CarlD Posted June 10, 2015 Share Posted June 10, 2015 (edited) In my thread entitled Newbie template for assigning script to hotkey, I was rightly chastised for failing to include error-checking that would trap an attempt to launch a second instance of the script, which then would not be responsive to the hotkey for terminating the script. Forum member Exit set me straight with his _Hotkey function, which does just that (and which I've incorporated into my "template"):; Code by forum member Exit _HotKey("{ESC}") _HotKey("^b") Func _HotKey($hotkey = "") ; ! ALT + SHIFT ^ CONTROL # WinKey Switch @HotKeyPressed Case "{ESC}" Exit 0*MsgBox(64 + 262144, Default, "Exit", 1) Case "^b" Beep() ; delete ";" to temporarely disable hotkey ; HotKeySet(@HotKeyPressed) ; send("^b") ; HotKeySet(@HotKeyPressed,"_Hotkey") Case Else If Not IsDeclared("hotkey") Then Return MsgBox(16 + 262144, Default, "No CASE statement defined for hotkey " & @HotKeyPressed) If HotKeySet($hotkey, "_Hotkey") = 0 Then Return MsgBox(16 + 262144, Default, "Hotkey " & $hotkey & " invalid or set by another application.") EndSwitch EndFunc ;==>_HotKey While Sleep(100) ; here should be your application. WEnd ; meanwhile, here is a dummy loop.It occurred to me that an alternate way of doing this, involving less code, might be:#include <Misc.au3> If _Singleton(@ScriptName, 1) = 0 Then MsgBox(64, @ScriptName, @ScriptName & " is already running!", 2) Exit EndIfAre there advantages/disadvantages to this alternate method?Thanks in advance for your help.PS: Private message sent to Exit alerting him to this thread. Edited June 10, 2015 by CarlD Link to comment Share on other sites More sharing options...
CarlD Posted June 10, 2015 Author Share Posted June 10, 2015 (edited) [Duplicate post removed] Edited June 10, 2015 by CarlD Link to comment Share on other sites More sharing options...
argumentum Posted June 10, 2015 Share Posted June 10, 2015 Exit was right, his code will show a FAIL even if the key is taken by another script or any cause of failure. Exit 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
CarlD Posted June 10, 2015 Author Share Posted June 10, 2015 Exit was right, his code will show a FAIL even if the key is taken by another script or any cause of failure.Got it. Thanks! Link to comment Share on other sites More sharing options...
Exit Posted June 10, 2015 Share Posted June 10, 2015 It occurred to me that an alternate way of doing this, involving less code, might be:#include <Misc.au3>If _Singleton(@ScriptName, 1) = 0 Then MsgBox(64, @ScriptName, @ScriptName & " is already running!", 2) Exit EndIfAre there advantages/disadvantages to this alternate method?This way does not help when the hotkey is set by another application or by system.Just try to set F12. It is reserved to system. See also help file._HotKey("{ESC}") _HotKey("{F12}") Func _HotKey($hotkey = "") Switch @HotKeyPressed Case "{ESC}" Exit 0 * MsgBox(64 + 262144, Default, "Exit", 1) Case "{F12}" Beep() Case Else If Not IsDeclared("hotkey") Then Return MsgBox(16 + 262144, Default, "No CASE statement defined for hotkey " & @HotKeyPressed) If HotKeySet($hotkey, "_Hotkey") = 0 Then Return MsgBox(16 + 262144, Default, "Hotkey " & $hotkey & " invalid or set by another application.") EndSwitch EndFunc ;==>_HotKey While Sleep(100) ; here should be your application. WEnd ; meanwhile, here is a dummy loop. App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
CarlD Posted June 10, 2015 Author Share Posted June 10, 2015 Exit, thanks. I also noticed that your function actually prevents a second instance of the script from opening, whereas the _Singleton statement takes effect in the second instance (but quickly shuts it down). Your _Hotkey function is very instructive -- thanks again. Link to comment Share on other sites More sharing options...
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