Jump to content
Sign in to follow this  
Darkbeo

Dynamic HotKeySet

Recommended Posts

Darkbeo

Hello all,

is possible to create a script that sets the key to be pressed from information taken from an ini file?

AutoIt.au3

HotKeySet( IniRead("file.ini", "Section", "Key", "Hotkey"), "Function" )

file.ini

[Section]
Key=F5

So the result would be:

HotKeySet( "F5", "Function" )

Share this post


Link to post
Share on other sites
FireFox

Hi,

Yes, but you need to use valid key combinations according to the function HotKeySet (explained for the function Send in the helpfile), so it would be {F5} here.

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
FireFox

I've tried some ways and not getting expected result.

You gave the code...just need to write into the ini.

IniWrite("file.ini", "Section", "Key", "{F5}")

HotKeySet(IniRead("file.ini", "Section", "Key", ""), "Function")

While 1
Sleep(1000)
WEnd

Func Function()
ConsoleWrite("toto" & @crlf)
EndFunc
  • Like 1

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Darkbeo

Thanks, worked perfectly as I wanted.

File.ini

[Section1]
Key1=Alt+F1
Key2=Ctrl+F1

Func KeySets($pSection, $pKey, $pFunc)
    #comments-start
        ! = Alt
        + = Shift
        ^ = Ctrl
    #comments-end
    If StringInStr( IniRead($arq, $pSection, $pKey, ""), "+" ) > 0 Then
        $cKey = StringLeft( IniRead($arq, $pSection, $pKey, ""), StringInStr( IniRead($arq, $pSection, $pKey, ""), "+" )-1 )
        Switch $cKey
            Case "Ctrl"
                $cReturn = "^{"& StringRight ( IniRead($arq, $pSection, $pKey, ""), StringInStr( IniRead($arq, $pSection, $pKey, ""), "+" )-2 ) &"}"
            Case "Alt"
                $cReturn = "!{"& StringRight ( IniRead($arq, $pSection, $pKey, ""), StringInStr( IniRead($arq, $pSection, $pKey, ""), "+" )-2 ) &"}"

            Case "Shift"
                $cReturn = "+{"& StringRight ( IniRead($arq, $pSection, $pKey, ""), StringInStr( IniRead($arq, $pSection, $pKey, ""), "+" )-2 ) &"}"
        EndSwitch
    Else
        $cReturn = "{"&IniRead($arq, $pSection, $pKey, "")&"}"
    EndIf
    HotKeySet( $cReturn, $pFunc)
EndFunc

KeySets("Section1", "Key1", "FuncA") -=> HotKeySet("!{F1}", "FuncA")
KeySets("Section1", "Key2", "FuncB") -=> HotKeySet("^{F1}", "FuncB")
Edited by Darkbeo

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • mannworks00
      By mannworks00
      Hey all,
      I know this code has been attempted before as a GUI app, and it would not copy text properly. Here is an updated version of the Google Search Shortcut  Script from: Google Search Shortcut Script
      #Region #AutoIt3Wrapper_Outfile=shortcuts.exe #EndRegion #include <Clipboard.au3> Opt("TrayMenuMode", 3) Opt("TrayOnEventMode", 1) HotKeySet("{F1}", "_googleit") TraySetToolTip("Right click to exit") TrayCreateItem("Exit") TrayItemSetOnEvent(-1, "_exit") While 1 Sleep(20) WEnd #cs _googleit() Source User: ViciousXUSMC https://www.autoitscript.com/forum/topic/177446-google-search-shortcut-script/?do=findComment&comment=1273519 #ce Source Func _googleit() Opt("WinTitleMatchMode", 2) ;Set Title Match To "Any Part of String" $sOldClip = ClipGet() ;Save Current Clipboard Send("^c") ;Copy Selected Text to Clipboard *before losing focus of current window WinActivate("Google Chrome", "") ; activate chrome ShellExecute("https://www.google.at/search?q=" & URLEncode(_ClipBoard_GetData())) ;Navigate to search ClipPut($sOldClip) ;Restore Old Clipboard Opt("WinTitleMatchMode", 1) ; Sets back to default EndFunc ;==>_googleit ;URL encoding is critical when doing string search queries via URL. #cs URLEncode Source User: Dhilip89 - UnicodeURL UDF https://www.autoitscript.com/forum/topic/46894-unicodeurl-udf/ #ce Func URLEncode($UnicodeURL) $UnicodeBinary = StringToBinary($UnicodeURL, 4) $UnicodeBinary2 = StringReplace($UnicodeBinary, '0x', '', 1) $UnicodeBinaryLength = StringLen($UnicodeBinary2) Local $EncodedString For $i = 1 To $UnicodeBinaryLength Step 2 $UnicodeBinaryChar = StringMid($UnicodeBinary2, $i, 2) If StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", BinaryToString('0x' & $UnicodeBinaryChar, 4)) Then $EncodedString &= BinaryToString('0x' & $UnicodeBinaryChar) Else $EncodedString &= '%' & $UnicodeBinaryChar EndIf Next Return $EncodedString EndFunc ;==>URLEncode Func _exit() $result = MsgBox(1, "Shortcuts", "Do you wish to exit Shortcuts?", 0) If $result == 1 Then Exit EndFunc ;==>_exit  
       
      shortcuts.au3
      shortcuts.exe
    • argumentum
      By argumentum
      There is a _ArrayToString() but no _ArrayFromString().  ( searched in the forum and google )
      The example is based on the _ArrayToString() help file, to show the reconstruction of the array.
      #include <Array.au3> #include <MsgBoxConstants.au3> Local $aArray[20] For $i = 0 To 19 $aArray[$i] = $i Next _ArrayDisplay($aArray, "1D Array") MsgBox($MB_SYSTEMMODAL, "Items 1-7", _ArrayToString($aArray, @TAB, 1, 7)) ConsoleWrite('>' & _ArrayToString($aArray, @TAB, 1, 7) & '<' & @CRLF) _ArrayDisplay(_ArrayFromString(_ArrayToString($aArray, @TAB, 1, 7), @TAB), "1D ArrayFromString") Local $aArray[10][10] For $i = 0 To 9 For $j = 0 To 9 $aArray[$i][$j] = $i & "-" & $j Next Next _ArrayDisplay($aArray, "2D Array") MsgBox($MB_SYSTEMMODAL, "Rows 4-7, cols 2-5", _ArrayToString($aArray, " :: ", 4, 7, @CRLF, 2, 5)) ConsoleWrite('>' & _ArrayToString($aArray, " :: ", 4, 7, @CRLF, 2, 5) & '<' & @CRLF) _ArrayDisplay(_ArrayFromString(_ArrayToString($aArray, " :: ", 4, 7, @CRLF, 2, 5), " :: ", @CRLF), "2D ArrayFromString") Func _ArrayFromString($s, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0) ; https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/ Local $m, $n, $b, $c = StringSplit($s, $sDelim_Row, 3) $b = StringSplit($c[0], $sDelim_Col, 3) If UBound($c) = 1 And Not $iForce2D Then For $m = 0 To UBound($b) - 1 $b[$m] = StringStripWS($b[$m], 3) If $b[$m] == Int($b[$m]) Then $b[$m] = Int($b[$m]) Next Return $b EndIf Local $a[UBound($c)][UBound($b)] For $n = 0 To UBound($c) - 1 $b = StringSplit($c[$n], $sDelim_Col, 3) If UBound($b) > UBound($a, 2) Then Return SetError(1) For $m = 0 To UBound($b) - 1 $b[$m] = StringStripWS($b[$m], 3) If $b[$m] == Int($b[$m]) Then $b[$m] = Int($b[$m]) $a[$n][$m] = $b[$m] Next Next Return $a EndFunc ;==>_ArrayFromString
      PS: so, how to save an array to an ini file ? ( small array, the limitations of an ini file still applies )
      #include <Array.au3>; For _ArrayDisplay() ; if you declare it, it will use it, else, use default ;Global $g_iniFile = @ScriptDir & "\ThisTest.ini" Example() Func Example() Local $n, $aTest, $aArray[3] = ["00", "one", "2"] ; if is not in the INI file, it will save it $aTest = IniGet("Test", $aArray) _ArrayDisplay($aTest, "1st") ; since is saved, it'll recall it $aTest = IniGet("Test") For $n = 0 To UBound($aTest) - 1 ; ..just to show the elements found as integer If IsInt($aTest[$n]) Then $aTest[$n] &= " = IsInt() = " & (IsInt($aTest[$n]) = 1) Next _ArrayDisplay($aTest, "2nd") EndFunc ;==>Example Func IniGet($sKey, $vDefault = Default, $sSection = "Settings") Local Static $ini = IsDeclared("g_iniFile") ? Eval("g_iniFile") : StringTrimRight(@ScriptFullPath, 4) & ".ini" Local $v, $s = IniRead($ini, $sSection, $sKey, Chr(1)) If $s = Chr(1) Then If $vDefault == Default Then Return SetError(1, 0, "") Else IniSet($sKey, $vDefault, $sSection) Return $vDefault EndIf EndIf $v = StringLeft($s, 1) Switch $v Case "i" Return Int(StringTrimLeft($s, 2)) Case "a" Return _ArrayFromString(BinaryToString(StringTrimLeft($s, 2)), Chr(1), Chr(2)) Case "d" Return Binary(StringTrimLeft($s, 2)) Case Else Return String(StringTrimLeft($s, 2)) EndSwitch EndFunc ;==>IniGet Func IniSet($sKey, $vValue, $sSection = "Settings") Local Static $ini = IsDeclared("g_iniFile") ? Eval("g_iniFile") : StringTrimRight(@ScriptFullPath, 4) & ".ini" If IsInt($vValue) Then $vValue = "i:" & $vValue ElseIf IsArray($vValue) Then $vValue = "a:" & StringToBinary(_ArrayToString($vValue, Chr(1), -1, -1, Chr(2))) ElseIf IsBinary($vValue) Then $vValue = "d:" & $vValue Else $vValue = "s:" & $vValue EndIf $vValue = IniWrite($ini, $sSection, $sKey, $vValue) Return SetError(@error, @extended, $vValue) EndFunc ;==>IniSet Func _ArrayFromString($s, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0) ; https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/ Local $m, $n, $b, $c = StringSplit($s, $sDelim_Row, 3) $b = StringSplit($c[0], $sDelim_Col, 3) If UBound($c) = 1 And Not $iForce2D Then For $m = 0 To UBound($b) - 1 $b[$m] = StringStripWS($b[$m], 3) If $b[$m] == Int($b[$m]) Then $b[$m] = Int($b[$m]) Next Return $b EndIf Local $a[UBound($c)][UBound($b)] For $n = 0 To UBound($c) - 1 $b = StringSplit($c[$n], $sDelim_Col, 3) If UBound($b) > UBound($a, 2) Then Return SetError(1) For $m = 0 To UBound($b) - 1 $b[$m] = StringStripWS($b[$m], 3) If $b[$m] == Int($b[$m]) Then $b[$m] = Int($b[$m]) $a[$n][$m] = $b[$m] Next Next Return $a EndFunc ;==>_ArrayFromString
      PS2: https://www.autoitscript.com/trac/autoit/ticket/3696#ticket
    • AndreasNWWWWW
      By AndreasNWWWWW
      Hi, i'm curious if this is even possible, i want to do an action if the ini file contains current values under a section.
      for my test i'm looking for 100,200,300,400,500
      and if any of those excits i want to pop a msgbox with the number in the section.
      i can in my example find one, but it does not check everyone. why?  what am i missing?
      Local $iscore810[5] = [100,200,300,400,500] Local $iMax800 = 5 While 1 ;~     Send("{pause}") ;;func les ini fil     $var = IniReadSection("Area.ini", "modus")     If @error Then         MsgBox(4096, "Error", "Unable to read section.")     Else          For $number = 1 To $var[0][0] If $var[$number][1] == $iscore810[3] Then       MsgBox($MB_SYSTEMMODAL, "FAnt den på", $var[$number][0], 5) EndIf                       Next     EndIf     exit WEnd  
    • guinness
      By guinness
      Right now the script will exit immediately if I don't pause it somehow (obvious of course).
      I was  curious on people's opinions of how they do it or what option they prefer below? Thanks
      Option 1 (I prefer)
      #include <MsgBoxConstants.au3> HotKeySet('^!p', SomeFunc) ProcessWaitClose(@AutoItPID) Func SomeFunc() MsgBox($MB_SYSTEMMODAL, "Title", "This message box will timeout after 10 seconds or select the OK button.", 10) EndFunc Option 2 (in the help file)
      #include <MsgBoxConstants.au3> HotKeySet('^!p', SomeFunc) While Sleep(100) WEnd Func SomeFunc() MsgBox($MB_SYSTEMMODAL, "Title", "This message box will timeout after 10 seconds or select the OK button.", 10) EndFunc Option 3
      #include <MsgBoxConstants.au3> HotKeySet('^!p', SomeFunc) While 1 GUIGetMsg() ; Use the internal sleep WEnd Func SomeFunc() MsgBox($MB_SYSTEMMODAL, "Title", "This message box will timeout after 10 seconds or select the OK button.", 10) EndFunc  
    • Jibsbrown
      By Jibsbrown
      Need some help understanding why the ConsoleWrite works inside 2nd For loop but not out side. Between Audit Wiki, Help file , Forum searching (lots of code reading), and YouTube ( shout out to TutsTeach), I have not been able to find the reason why. 
      $sIniPath = "installLog.ini" ; - Get section name $iniSctionNames = IniReadSectionNames($sIniPath) ; - Get Keys and Vaules For $a = 1 to UBound($iniSctionNames) - 1 $keys = IniReadSection($sIniPath , $iniSctionNames[$a]) For $b = 1 to UBound($keys) - 1 $oldSysInfo = IniRead($sIniPath , $iniSctionNames[1], $keys[$b][0], "") $PntIPInfo = IniRead($sIniPath , $iniSctionNames[2], $keys[$b][0], "") $NewPCInfor = IniRead($sIniPath , $iniSctionNames[3], $keys[$b][0], "") ;ConsoleWrite($oldSysInfo & @LF) Next ;ConsoleWrite($oldSysInfo & @LF) Next ConsoleWrite($oldSysInfo) My intention is to use the variables later for Listboxes. Any explanation, forum post links or whatever would help. Sorry also very very new to Autoit.
      Also here's the ini file.
      [OldSysInfo] 4=192.168.0.4|DESKTOP-RDIU2SN|R90M05Q8 5=192.168.0.5|SD0123456789101|R9WGP9P 6=192.168.0.6|SD0123456789102|R9WGP9PT 3=192.168.0.3|DESKTOP-3RS4LKL|R9WGP9P 23=192.168.0.23|SD0123456789102|MXL1234P5I [PrinterIp] 50=192.168.0.50 48=192.168.0.48 47=192.168.0.47 [NewSysInfo] newPC = SD0123456789adfs|192.168.0.185|2UA1234FTR Thank you for your time.
×