Jump to content

_Hotkey() --> Hotkeyset() abbreviation with error checking.


Exit
 Share

Recommended Posts

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

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Good job!

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

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