Jump to content
Sign in to follow this  
CodeMaster Rapture

[Need help] Creating Hotkeys via an INI file.

Recommended Posts

CodeMaster Rapture

Hey all,

Been a long time since I've been here, muchless used autoit. Anyways, I found a need for it and I am having a problem. I'm trying to create hotkeys via an ini file and I'm not too sure how to go about it. I mean, I could write a few hundred lines of code to make sure the value in the INI is a useable key, but I'm sure there is an easier way.

I've tried searching the forums, but I haven't come across anyhting useful for this. The psuedo code I have is:

HotKeySet( IniRead("MyIni.ini", "Hotkeys", Exit_Key, "{Esc}") , "Exit_Me")
; or
$Key = IniRead("MyIni.ini", "Hotkeys", Exit_Key, "{Esc}")
If ( _ValidKey($Key) )Then;A UDF I'd write that has all the possible keys
     HotKeySet($Key,"Exit_Me")
EndIf

I know that will work, but, I'd like to keep it simple for the End-User so they can modify the ini file without knowing the syntax for AutoIt Hotkeys (brackets, actual key names, etc).

Thanx,

CMR

Share this post


Link to post
Share on other sites
dabus

What about keychains?

I created an app for myself with a few hard-coded keys.

When I press this combination, it creates an transparent window with a focused imputbox. It waits for an imput or ESC to close the window.

Here's my crappy code :dance:

AutoItSetOption("TrayIconDebug", 1)
AutoItSetOption("WinTitleMatchMode", 2)

#include <GUIConstants.au3>

HotKeySet ("^#r", "GetCommandKeys")

Global $Title
Global $FileIniFile = @ScriptDir & "\AutoDo.ini"

If Not FileExists ( $FileIniFile ) Then
    $IniFile=FileOpen ( $FileIniFile , 2)
     FileWriteLine ( $IniFile, "[Command]" )
    FileWriteLine ( $IniFile, "; Value can be either: *.exe if it is in PATH, C:\*.exe if not " )
    FileClose ( $IniFile )
EndIf

While 1
    Sleep(100)
WEnd

Func GetCommandKeys()
    GetKey("Command")
EndFunc ;==>GetCommandKeys 

Func GetKey ($a)
    Local $Input
; Create Input to get "HotKey"
    $TransGui = GUICreate("GetText", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP)
    $Input = GUICtrlCreateInput("", -50, -50)
    GUISetBkColor(000000)
    GUICtrlSetState($Input, $GUI_FOCUS)
    WinSetTrans($TransGui, "", 25)
    GUISetState(@SW_SHOW)

    While 1
        $Antwort = GUIGetMsg()
        If $Antwort = $GUI_EVENT_CLOSE Then ExitLoop
        $InputText = GUICtrlRead($Input)
        If $InputText <> "" Then ExitLoop
    WEnd
    GUIDelete($TransGui)
; Parse IniFile and excute
    If $InputText <> "" Then
        $Found = 0
        $Items = IniReadSection($FileIniFile, $a)
        If @error Then
            PopUp ( 1 , "HotKey: Info" , "Error reading section " & $a & ". Have a look at " & $FileIniFile & "." )
        Else    
            For $i = 1 To $Items[0][0]
                If StringInStr ( $InputText , $Items[$i][0], 1 ) Then
                    If $a="Command" Then 
                        $Found=1
                        Run($Items[$i][1])
                    EndIf   
                    ExitLoop
                Else
                    ContinueLoop
                EndIf
            Next
            If $Found = 0 Then PopUp ( 1 , "HotKey: Info" , "No Hotkey for " & $InputText & " within section " & $a & _ 
                " . Have a look at AutoDo.ini." )
        EndIf
    EndIf       
EndFunc ;==>GetKey 

Func PopUp ( $a, $b, $c)
    #region --- GuiBuilder code Start ---
    $Gui = GUICreate($b, 280, 80, (@DesktopWidth - 280) / 2, (@DesktopHeight - 80) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
    If $a=1 Then 
        $Label1 = GUICtrlCreateLabel($c, 60, 10, 210, 60)
    EndIf   
    $Icon1 = GUICtrlCreateIcon("shell32.dll", 23, 10, 20, 32, 32)
    GUISetState(@SW_SHOW)
    If $a=1 Then
        While 1
            $Antwort = GUIGetMsg()
            If $Antwort = $GUI_EVENT_CLOSE Then ExitLoop
            Sleep(100)
        WEnd
        GUIDelete($Gui)
    EndIf
    #endregion --- GuiBuilder generated code End ---
EndFunc

[Command]
; HotKey=Ctrl+Win+r (Run)
1=explorer C:\

Not exactly what you wanted, but it fixed the problem for me. :whistle:

Edited by dabus

Share this post


Link to post
Share on other sites
SmOke_N

Nice dabus..

Dim $KEY = "{" & IniRead(@DesktopDir & "/MyIni.ini", "Hotkeys", "Exit_Key", "NOT FOUND") & "}"
HotKeySet($KEY, "Exit_Me")
While 1
    Sleep(100)
WEnd

Func Exit_Me()
    MsgBox(0, "", "MADE IT HERE")
EndFunc

User wouldn't be able to change while using the script... but I guess you could do a function call and return the value to change it while the user was using it..

Is this what you were looking for?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
CodeMaster Rapture

Well, both methods would work, but a bit too primitive for my needs. What I need is to be able to parse the value in the INI file and make sure it is a legal hotkey in AutoIt. It has to be done in the background (no GUI). I'm basically looking to see if anyone has written some code to check this already. If not, I'll write some and post it.

For example, if the End-User modifies there INI to something like so:

[Hotkeys]
Exit_Key = Shift+F5

Then I need to be able to parse that into:

HotKeySet("+{F5}","Exit_me")

Also, I need to be able to remove illegal characters. Such as @#$%*(), etc. I already wrote a UDF to strip characters (here), so that isn't too much of a problem.

So let's say someone get's creative with there INI and puts in:

[Hotkeys]
Exit_Key = Zero+CTRL+F14

I will need to be able to reject that completely. Therein lies my problem. I suppose I'll just have to spend a bit of time writting something a bit complex to deal with all this. I just don't want to leave my little app with a bug.

Thanx anyways,

-CMR

Share this post


Link to post
Share on other sites
SmOke_N

So why wouldn't you just write what is acceptable in the script?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Valuater

So why wouldn't you just write what is acceptable in the script?

<{POST_SNAPBACK}>

I agree... to parse all of the possibilities would be really tough

I did it like this

Hope that helps

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
CodeMaster Rapture

Alrighty, I'm making headway, now I got a problem with IniRead...

My Code:

;Declare with defaults
$InGame_Pause = "PAUSE"
$Auto_Pause = "F5"
$Auto_Click = "SPACE"

If (Not FileExists("AutoPause.ini")) Then
    IniWrite("AutoPause.ini","HotKeys","InGame_Pause","PAUSE")
    IniWrite("AutoPause.ini","HotKeys","Auto_Pause","F5")
    IniWrite("AutoPause.ini","HotKeys","Auto_Click","SPACE")
Else
    $InGame_Pause = IniRead("AutoPause.ini","HoyKeys","InGame_Pause", "NOTFOUND")
    $Auto_Pause = IniRead("AutoPause.ini","HoyKeys","Auto_Pause", "NOTFOUND")
    $Auto_Click = IniRead("AutoPause.ini","HoyKeys","Auto_Click", "NOTFOUND")
EndIf

MsgBox(0,$App_Full,"$InGame_Pause: " & $InGame_Pause)
MsgBox(0,$App_Full,"$Auto_Pause: " & $Auto_Pause)
MsgBox(0,$App_Full,"$Auto_Click: " & $Auto_Click)

$InGame_Pause = "{" & $InGame_Pause & "}"
$Auto_Pause = "{" & $Auto_Pause & "}"
$Auto_Click = "{" & $Auto_Click & "}"

MsgBox(0,$App_Full,"$InGame_Pause: " & $InGame_Pause)
MsgBox(0,$App_Full,"$Auto_Pause: " & $Auto_Pause)
MsgBox(0,$App_Full,"$Auto_Click: " & $Auto_Click)
Exit

And the output is this:

$InGame_Pause: NOTFOUND

$Auto_Pause: NOTFOUND

$Auto_Click: NOTFOUND

$InGame_Pause: {NOTFOUND}

$Auto_Pause: {NOTFOUND}

$Auto_Click: {NOTFOUND}

The INI:

[HotKeys]
InGame_Pause=PAUSE
Auto_Pause=F5
Auto_Click=SPACE

I've tried by deleting the INI and then running the script once (setup the INI) and then run it again and read it. I get the same results no matter what. I've also tried adding @ScriptDir & "\AutoPause.ini" with the same results.

Dunno what I'm doing wrong. Maybe one of you knows.

-CMR

Share this post


Link to post
Share on other sites
LxP

    $InGame_Pause = IniRead("AutoPause.ini","HoyKeys","InGame_Pause", "NOTFOUND")

    $Auto_Pause = IniRead("AutoPause.ini","HoyKeys","Auto_Pause", "NOTFOUND")

    $Auto_Click = IniRead("AutoPause.ini","HoyKeys","Auto_Click", "NOTFOUND")

Share this post


Link to post
Share on other sites
CodeMaster Rapture

LOL, god I'm the king of idiots. :whistle:

Ya know, sometimes it takes another person's perspective to find the most obvious flaw.

Thanx,

-CMR

Share this post


Link to post
Share on other sites
LxP

Here's a small suggestion that I follow almost religiously: any string within my work that I reference many times becomes a single constant which I then place at the top of the corresponding file. This is a great way to trim script sizes down while making your script easier to understand at points. In your case perhaps $INI_FILE and $INI_SECTION would be good choices.

Here's a good example -- compare the two code snippets below (where all of my message boxes would have a common title):

if (msgBox(0x24), "Alex's Company Registration Script", "Continue?" = 7) then exit

local const $TITLE = "Alex's Company Registration Script"
local const $MB_QUESTION = 32
local const $MB_YESNO = 4
local const $ID_NO = 7

if (msgBox($MB_QUESTION + $MB_YESNO, $TITLE, "Continue?") = $ID_NO) then exit

Actually, that's probably a counterexample simply because it turns one line into five but when you're working on large projects where you reference the values many times, you'd be surprised at how effective it is.

Share this post


Link to post
Share on other sites
CodeMaster Rapture

Here's a small suggestion that I follow almost religiously: any string within my work that I reference many times becomes a single constant which I then place at the top of the corresponding file. This is a great way to trim script sizes down while making your script easier to understand at points. In your case perhaps $INI_FILE and $INI_SECTION would be good choices.

Here's a good example -- compare the two code snippets below (where all of my message boxes would have a common title):

if (msgBox(0x24), "Alex's Company Registration Script", "Continue?" = 7) then exit

local const $TITLE = "Alex's Company Registration Script"
local const $MB_QUESTION = 32
local const $MB_YESNO = 4
local const $ID_NO = 7

if (msgBox($MB_QUESTION + $MB_YESNO, $TITLE, "Continue?") = $ID_NO) then exit

Actually, that's probably a counterexample simply because it turns one line into five but when you're working on large projects where you reference the values many times, you'd be surprised at how effective it is.

<{POST_SNAPBACK}>

Oh I do that already. I was just trying to get the hang of the INI functions. The actual script has constants. Good point for anyone else reading though.

-CMR

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.