web analytics

AutoIt: script for hotkeys usage

This script allows You to define the global Windows hotkeys via the configuration file.

The syntax of the config file is simple:

Hotkey,Program,Parameters

Here “Hotkey” is the combination of the keyboard key with one of the identifiers:

^ Ctrl
! Alt
+ Shift
# Windows-Key

Example:

;Win+C – start calculator
#{c},calc.exe

;Win+Shift+N – start notepad
+#{n},notepad.exe

;Win+N – start notepad++ together with apploc.exe
#{n},%windir%\AppPatch\AppLoc.exe,”%ProgramFiles%\Notepad++\notepad++.exe” -nosession /L0419

#Include <File.au3>

; ^ Ctrl
; ! Alt
; + Shift
; # Windows-Key

Global  $MATRIX_SIZE = 1
Global $aMatrix[$MATRIX_SIZE][3]
Global  ENUM    $POS_HOTKEY = 0, $POS_PROGRAM, $POS_PARAM

Global $sPATH, $sProgramFiles, $sAppData
AutoItSetOption("TrayAutoPause",1)
AutoItSetOption("TrayMenuMode",1)

$mnuReadSettings = TrayCreateItem("Read settings")
$mnuHelp         = TrayCreateItem("Help")
TrayCreateItem("")
$mnuExit = TrayCreateItem("Exit")

Func read_settings()

    Local $aTMPArray
    FileChangeDir (@scriptdir )
    If Not _FileReadToArray("hotkeys.txt",$aTMPArray) Then
       MsgBox(4096,"Error", " Error reading hotkeys.txt@CRLF error:" & @error)
       Exit
    EndIf
    if ( $aTMPArray[0] = 0 ) Then
        MsgBox(4096,"Error", " No hotkeys defined")
        Exit
    EndIf

    $MATRIX_SIZE = $aTMPArray[0]
    ReDim $aMatrix[$MATRIX_SIZE][3]

    AutoItSetOption("ExpandEnvStrings",1)

    $i = 0
    for $i_tmp = 1 to $MATRIX_SIZE

        if ( StringLen($aTMPArray[ $i_tmp ]) = 0 or StringLeft( $aTMPArray[ $i_tmp ] , 1) =  ";"  ) Then
            ContinueLoop
        EndIf

        $aCurLine = StringSplit( $aTMPArray[ $i_tmp ], ",;", 2 )
        $lCurLine = UBound( $aCurLine )

        if ( $lCurLine<2 or $aCurLine[ $POS_HOTKEY ] =  "" or $aCurLine[ $POS_PROGRAM ] =  "" ) Then
           MsgBox(4096,"Error", " No program or hotkey defined: " & $aTMPArray[ $i_tmp ] )
       Exit

        EndIf

        ; Are the parameters for the command line?
        if ($lCurLine = 2 ) Then
            $sParam = ""
        Else
            $sParam = $aCurLine[$POS_PARAM]
        EndIf

        Switch  $aCurLine[$POS_PROGRAM]
                Case "SPECIAL"
                    HotKeySet($aCurLine[$POS_HOTKEY], "_run_special" )
                Case Else

                    $aMatrix[$i][$POS_HOTKEY]   = $aCurLine[$POS_HOTKEY]

                    $aMatrix[$i][$POS_PROGRAM]  = $aCurLine[$POS_PROGRAM]

                    $aMatrix[$i][$POS_PARAM] = $sParam

                    $aMatrix[$i][$POS_PROGRAM] = $aMatrix[$i][$POS_PROGRAM] & " " & $aMatrix[$i][$POS_PARAM] ;

                    HotKeySet($aMatrix[$i][$POS_HOTKEY],"_run_it" )

                    $i = $i + 1 ;

        EndSwitch


    Next

    AutoItSetOption("ExpandEnvStrings",0)

EndFunc

Func    _run_special()
    MsgBox(0 , "No action", "Reserved for future use" )
EndFunc


Func    _run_it()
    For $i = 0 to $MATRIX_SIZE - 1
        if( $aMatrix[$i][$POS_HOTKEY] = @HotKeyPressed ) Then
            Run( $aMatrix[$i][$POS_PROGRAM] )
            ExitLoop
        EndIf
    Next
EndFunc

Func    show_help()
    Local   $helpMessage = ""

    for $i = 0 to $MATRIX_SIZE - 1
        $helpMessage = $helpMessage & $aMatrix[$i][$POS_HOTKEY] & " " & $aMatrix[$i][$POS_PROGRAM] & @CRLF
    Next

    MsgBox(0,"Help", $helpMessage )
EndFunc

; ##### Main ######
read_settings()
While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = $mnuReadSettings
            read_settings()
        case $msg = $mnuHelp
            show_help()
        Case $msg = $mnuExit
            Exit
    EndSelect

    sleep(100)
WEnd

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Categories

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.