Hotkeyset() without error checking is very common. Even help file examples omit it sometimes.
I've written code to set, check and process hotkeys in one function.
See this example:
_HotKey("{ESC}") ; hotkey to exit script.
_HotKey("{F1}") ; hotkey to show AutoIt help.
_HotKey("{F2}") ; hotkey with missing CASE code.
_HotKey("#") ; invalid hotkey
_HotKey("{F12}") ; F12 is reserved to the system.
While Sleep(100) ; here should be your application.
WEnd ; meanwhile, here is a dummy loop.
Func _HotKey($hotkey = "")
Switch @HotKeyPressed
Case "{ESC}"
Exit MsgBox(64 + 262144, Default, "Exit", 1)
Case "{F1}"
ShellExecute("http://www.autoitscript.com/autoit3/docs/functions/HotKeySet.htm")
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
For ease of use, I've written an abbreviation script:
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>
While Sleep(100)
If Not WinClose("[REGEXPTITLE:au3UserAbbrev.properties|au3.keywords.user.abbreviations.properties]") Then ExitLoop
WEnd
ShellExecute("NotePad.exe", @LocalAppDataDir & "\AutoIt v3\SciTE\au3UserAbbrev.properties", "", "", @SW_SHOW)
ShellExecute("NotePad.exe", @LocalAppDataDir & "\AutoIt v3\SciTE\au3.keywords.user.abbreviations.properties", "", "", @SW_SHOW)
$WinHandle1 = (WinWait("[REGEXPTITLE:au3UserAbbrev.properties]", "", 5) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : SetError(1, 0, 0))
$WinHandle2 = (WinWait("[REGEXPTITLE:au3.keywords.user.abbreviations.properties]", "", 5) ? (WinActivate("[LAST]") ? WinWaitActive("[LAST]") : 0) : SetError(1, 0, 0))
$Text1 = ControlGetText($WinHandle1, "", "Edit1")
$Text2 = ControlGetText($WinHandle2, "", "Edit1")
$Abbrev = @CRLF & 'hk=\n' & _
'_HotKey("{ESC}")\n' & _
'_HotKey("{|}")\n' & _
'Func _HotKey($hotkey = "")\n' & _
' Switch @HotKeyPressed\n' & _
' Case "{ESC}"\n' & _
' Exit MsgBox(64 + 262144, Default, "Exit", 1)\n' & _
' Case "{???}"\n' & _
' Beep()\n' & _
' Case Else\n' & _
' If Not IsDeclared("hotkey") Then Return MsgBox(16 + 262144, Default, "No CASE statement defined for hotkey " & @HotKeyPressed)\n' & _
' If HotKeySet($hotkey, "_Hotkey") = 0 Then Return MsgBox(16 + 262144, Default, "Hotkey " & $hotkey & " invalid or set by another application.")\n' & _
' EndSwitch\n' & _
'EndFunc ;==>_HotKey\n\n' & _
'While Sleep(100) ; here should be your application.\n' & _
'WEnd ; meanwhile, here is a dummy loop.\n' & _
'\n' & @CRLF & @CRLF
If Not (StringInStr($Text1, $Abbrev)) Then
$Text1 = StringStripWS($Text1, $STR_STRIPTRAILING)
ControlSetText($WinHandle1, "", "Edit1", $Text1 & @CRLF & $Abbrev & @CRLF)
ControlSend($WinHandle1, "", "", "^s")
EndIf
If Not (StringInStr($Text2, " hk ")) Then
$Text2 = StringStripWS($Text2, $STR_STRIPTRAILING)
ControlSetText($WinHandle2, "", "Edit1", $Text2 & " hk ")
ControlSend($WinHandle2, "", "", "^s")
EndIf
$aPos = WinGetPos($WinHandle1)
WinActivate($WinHandle1)
WinMove($WinHandle1, "", $aPos[0], $aPos[1] + 100)
MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, " ", "Abbreviation ""hk "" is installed. ", 0)
WinMove($WinHandle1, "", $aPos[0], $aPos[1])
WinClose($WinHandle1)
WinClose($WinHandle2)
Just run this script and restart the SciTE editor.
Enter "hk " (without quotes). Please note the blank after hk .
A skeleton code appears with the cursor at the convenient location.
Enter your hotkey code and place a corresponding case statement some lines below.
Enjoy.