Jump to content

Newbie template for assigning script to hotkey


CarlD
 Share

Recommended Posts

As a newcomer to AutoIt, I was struggling with how to use the built-in function HotKeySet() to assign a script to a hotkey within the script itself (equivalent of AutoHotkey's [keyname]:: statement). With help from argumentum and Melba23, I was finally able to wrap my head around it. (The documentation for HotKeySet() really ought to include an example that illustrates the endless While...WEnd loop needed to make the internal hotkey assignment work.) The demo code below summarizes what I learned, in the hope that it will help others trying to puzzle out how to replicate the AutoHotkey [keyname]:: functionality in AutoIt. The following script is meant to be both studied and run.

Edit: Tried to integrate Exit's error-checking function to trap second instance of script. Works here. Also took in argumentum's corrections.

; Hotkey.au3 -- AutoIt v3
; Template for assiging/unassigning script to hotkey(s)

#cs
This demo script assigns itself to hotkey <NumPadMinus> ("{NumPadSub}"),
and uses <Shift-NumPadMinus> ("+{NumPadSub}") to clear the hotkeys
and exit the script.
#ce

_HotKey("+{NumPadSub}") ; error-checking func -- thanks to member Exit
Func _HotKey($hotkey = "") ; (trap second instance of script)
;       ! ALT  + SHIFT  ^ CONTROL  # WinKey
 Switch @HotKeyPressed
  Case "+{NumPadSub}"
   HotKeyClear()
  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

; Set hotkey(s)
HotKeySet("{NumPadSub}", "Main")   ; edit key names as needed
While 1          ; wait for hotkey to be pressed
 Sleep(20)
WEnd

; Clear hotkey(s)
Func HotKeyClear()
 HotKeySet("{NumpadSub}")   ; empty second param clears
 HotKeySet("+{NumpadSub}")   ;
 MsgBox(64, @ScriptName, "Clearing hotkeys and Quitting " _
  & @ScriptName, 3)
 Exit ; Omit if script is to continue running after clearing hotkey(s)
EndFunc   ;==>HotKeyClear

FUNC Main()
; Put main routine here -- i.e, the code to be run when hotkey is pressed.
; If the script calls user-defined functions,
;  define them below Main(); e.g., see MyFunc, below.
 MsgBox(0, @ScriptName, "We are in Main()", 2)
 MyFunc()
 MsgBox(0, @ScriptName, "We are back in Main()" & @CRLF _
  & "Hit <Shift-NumpadMinus> to clear hotkeys and exit script", 5)
ENDFUNC   ;==>Main

Func MyFunc()
; Do stuff here if you like
 MsgBox(0, "MyFunc", "Now we are in MyFunc()", 2)
 Return
EndFunc   ;==>MyFunc

 

Edited by CarlD
Correction from argumentum; error-checking func from Exit
Link to comment
Share on other sites

please fix this

; Clear hotkey(s)
Func HotKeyClear()
    HotKeySet("{NumpadSub}") ; empty second param clears the specified hotkey
    HotKeySet("+{NumpadSub}") ;
    MsgBox(64, @ScriptName, "Clearing hotkeys and Quitting " & @ScriptName, 3)
    Exit ; Omit if script is to continue running after clearing hotkey(s)
EndFunc   ;==>HotKeyClear

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

This is just the regular use of HotKeySet, isn't it?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Error checking is missing.

If it is run twice, the second script will never recognice numpad+ and will never end.

Here my snippet for hotkey usage:

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

 

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

  • Moderators

CarlD, as is being pointed out here, as well as your other post in this forum, while it is great that you want to share your scripts with the forum, just repackaging a function already built into the language is not really helping anyone. Especially when you do not have proper error checking in place.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Point taken. Thanks for your comments. I have taken them in (or tried to) in the current version -- see OP.

Apologies if this is not useful to you or is not posted to the proper forum. It has nevertheless been a learning experience for me.

 

Edited by CarlD
Link to comment
Share on other sites

Had a spare minute, so I tweaked the error checking myself

 

; Hotkey.au3 -- AutoIt v3
; Template for assiging/unassigning script to hotkey(s)

#cs

    This demo script assigns itself to hotkey <NumPadMinus> ("{NumPadSub}"),
    and uses <Shift-NumPadMinus> ("+{NumPadSub}") to clear the hotkeys

    and exit the script.
#ce



_HotKey("+{NumPadSub}") ; error-checking func -- thanks to member Exit
_HotKey("{NumPadSub}")

Func _HotKey($hotkey = "")
    Switch @HotKeyPressed

        Case "{NumPadSub}"
            Main()
        Case "+{NumPadSub}"
            HotKeyClear()
        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.

; Clear hotkey(s)
Func HotKeyClear()
    HotKeySet("{NumpadSub}") ; empty second param clears

    HotKeySet("+{NumpadSub}") ;
    MsgBox(64, @ScriptName, "Clearing hotkeys and Quitting " _
             & @ScriptName, 3)
    Exit ; Omit if script is to continue running after clearing hotkey(s)
EndFunc   ;==>HotKeyClear



Func Main()
    ; Put main routine here -- i.e, the code to be run when hotkey is pressed.
    ; If the script calls user-defined functions,
    ;  define them below Main(); e.g., see MyFunc, below.
    MsgBox(0, @ScriptName, "We are in Main()", 2)
    MyFunc()
    MsgBox(0, @ScriptName, "We are back in Main()" & @CRLF _
             & "Hit <Shift-NumpadMinus> to clear hotkeys and exit script", 5)
EndFunc   ;==>Main

Func MyFunc()
    ; Do stuff here if you like

    MsgBox(0, "MyFunc", "Now we are in MyFunc()", 2)
    Return
EndFunc   ;==>MyFunc

 

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Had a spare minute, so I tweaked the error checking myself

 

; Hotkey.au3 -- AutoIt v3
; Template for assiging/unassigning script to hotkey(s)

#cs

    This demo script assigns itself to hotkey <NumPadMinus> ("{NumPadSub}"),
    and uses <Shift-NumPadMinus> ("+{NumPadSub}") to clear the hotkeys

    and exit the script.
#ce



_HotKey("+{NumPadSub}") ; error-checking func -- thanks to member Exit
_HotKey("{NumPadSub}")

Func _HotKey($hotkey = "")
    Switch @HotKeyPressed

        Case "{NumPadSub}"
            Main()
        Case "+{NumPadSub}"
            HotKeyClear()
        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.

; Clear hotkey(s)
Func HotKeyClear()
    HotKeySet("{NumpadSub}") ; empty second param clears

    HotKeySet("+{NumpadSub}") ;
    MsgBox(64, @ScriptName, "Clearing hotkeys and Quitting " _
             & @ScriptName, 3)
    Exit ; Omit if script is to continue running after clearing hotkey(s)
EndFunc   ;==>HotKeyClear



Func Main()
    ; Put main routine here -- i.e, the code to be run when hotkey is pressed.
    ; If the script calls user-defined functions,
    ;  define them below Main(); e.g., see MyFunc, below.
    MsgBox(0, @ScriptName, "We are in Main()", 2)
    MyFunc()
    MsgBox(0, @ScriptName, "We are back in Main()" & @CRLF _
             & "Hit <Shift-NumpadMinus> to clear hotkeys and exit script", 5)
EndFunc   ;==>Main

Func MyFunc()
    ; Do stuff here if you like

    MsgBox(0, "MyFunc", "Now we are in MyFunc()", 2)
    Return
EndFunc   ;==>MyFunc

 

​Hmmm, when I run your tweak, if I open a second instance of the script, I see the second message box under "Case Else", but then the second instance of the script stays running.

If I change your code as follows (changes are under "Case Else" -- omit "Return" before the MsgBox and add "Exit"), then the second instance of the script does exit, as expected:

; Hotkey.au3 -- AutoIt v3
; Template for assiging/unassigning script to hotkey(s)

#cs

    This demo script assigns itself to hotkey <NumPadMinus> ("{NumPadSub}"),
    and uses <Shift-NumPadMinus> ("+{NumPadSub}") to clear the hotkeys

    and exit the script.
#ce



_HotKey("+{NumPadSub}") ; error-checking func -- thanks to member Exit
_HotKey("{NumPadSub}")

Func _HotKey($hotkey = "")
    Switch @HotKeyPressed

        Case "{NumPadSub}"
            Main()
        Case "+{NumPadSub}"
            HotKeyClear()
        Case Else
            If Not IsDeclared("hotkey") Then
                MsgBox(16 + 262144, Default, "No CASE statement defined for hotkey " & @HotKeyPressed)
                Exit
            EndIf
            If HotKeySet($hotkey, "_Hotkey") = 0 Then
                MsgBox(16 + 262144, Default, "Hotkey " & $hotkey & " invalid or set by another application.")
                Exit
            EndIf
    EndSwitch

EndFunc   ;==>_HotKey



While Sleep(100) ; here should be your application.
WEnd ; meanwhile, here is a dummy loop.

; Clear hotkey(s)
Func HotKeyClear()
    HotKeySet("{NumpadSub}") ; empty second param clears

    HotKeySet("+{NumpadSub}") ;
    MsgBox(64, @ScriptName, "Clearing hotkeys and Quitting " _
             & @ScriptName, 3)
    Exit ; Omit if script is to continue running after clearing hotkey(s)
EndFunc   ;==>HotKeyClear



Func Main()
    ; Put main routine here -- i.e, the code to be run when hotkey is pressed.
    ; If the script calls user-defined functions,
    ;  define them below Main(); e.g., see MyFunc, below.
    MsgBox(0, @ScriptName, "We are in Main()", 2)
    MyFunc()
    MsgBox(0, @ScriptName, "We are back in Main()" & @CRLF _
             & "Hit <Shift-NumpadMinus> to clear hotkeys and exit script", 5)
EndFunc   ;==>Main

Func MyFunc()
    ; Do stuff here if you like

    MsgBox(0, "MyFunc", "Now we are in MyFunc()", 2)
    Return
EndFunc   ;==>MyFunc

Do you get the same results?

Link to comment
Share on other sites

That's just a point of view, if you exit the script unconditionally or give the user the chance to continue.
He can kill the script anyway. So your solutution is the "second way to Rome" :)

But there is no need to put a  new line with an exit statement.
Just replace Return by Exit before the msgbox()

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

That's just a point of view, if you exit the script unconditionally or give the user the chance to continue.
He can kill the script anyway. So your solutution is the "second way to Rome" :)

But there is no need to put a  new line with an exit statement.
Just replace Return by Exit before the msgbox()

​Glad I managed to get to Rome somehow. ;) And I did realize that I could simply replace Return with Exit after I looked your post from last year. Much obliged, Exit. :D

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