Jump to content

Recommended Posts

I'm trying to make something that will accept 2 inputs from a user (hotkey, text). I've gotten everything to work except for the part of assigning the hotkeys and making all of them work simultaneously. Currently it accepts and creates the hotkeys, however will only use the values from the last saved input. I understand (at least I think) what is going wrong, I'm just unsure of how I can fix it. I'm self taught so apologies if I am just looking right over a simple solution

The issue as I see it, is my $hotkey and $text variables are simply holding the last value assigned to them. What I need it to do is send the text associated with the key pressed.  in this example pressing a, s, or d all result in msgbox with hello, rather then pressing a and returning fuu.

$hotkey = ""
;user input here, but initializing a few random .ini info
IniWrite("file.ini", "1", "Key", "a")
IniWrite("file.ini", "1", "text", "fuu")

IniWrite("file.ini", "2", "Key", "s")
IniWrite("file.ini", "2", "text", "bar")

IniWrite("file.ini", "3", "Key", "d")
IniWrite("file.ini", "3", "text", "hello")


global $aSecNames = IniReadSectionNames("file.ini")

For $i = 1 to $aSecNames[0]
    $hotkey = IniRead("file.ini", $aSecNames[$i], "Key", "")
    HotKeySet($hotkey, "_test")
    Global $text = IniRead("file.ini", $aSecNames[$i], "text", "")
Next

While 1

WEnd

Func _test()
    ;send($text)
    MsgBox(0, "",$text)
EndFunc

Thanks for any assistance

Link to post
Share on other sites

See the Autoit Help for HotKeySet, the  "Example 2, @HotKeyPressed usage"

Quote

 $text variables are simply holding the last value assigned to them.

Yes, a variable which is assigned in a loop, gets overwritten by each loop. You can use an array or  the above suggestion instead.

Edited by Dan_555
Link to post
Share on other sites

That was it, thank you Dan. Quite a simple solution after all this time ahah. Here is my hackjob solution for any who might stumble across this.

$hotkey = ""
;user input here, but initializing random .ini info
IniWrite("file.ini", "1", "Key", "a")
IniWrite("file.ini", "1", "text", "fuu")

IniWrite("file.ini", "2", "Key", "s")
IniWrite("file.ini", "2", "text", "bar")

IniWrite("file.ini", "3", "Key", "d")
IniWrite("file.ini", "3", "text", "hello")

global $aSecNames = IniReadSectionNames("file.ini")

For $i = 1 to $aSecNames[0]
    $hotkey = IniRead("file.ini", $aSecNames[$i], "Key", "")
    HotKeySet($hotkey, "_test")
Next

While 1

WEnd

Func _test()
    ;loop through all sections while and compare key value to the @hotkeypressed value
    For $i = 1 to $aSecNames[0]
        if IniRead("file.ini", $aSecNames[$i], "Key", "") = @HotKeyPressed Then
            $text = IniRead("file.ini", $aSecNames[$i], "text", "")
        EndIf
    Next
    MsgBox(0, "",$text)
EndFunc

 

Link to post
Share on other sites

Since all hotkeys must be unique, you could streamline your ini file and (likewise) your code this way :

#include <Constants.au3>
#include <Array.au3>

;user input here, but initializing random .ini info
IniWrite("file.ini", "HotKeys", "a", "fuu")
IniWrite("file.ini", "HotKeys", "s", "bar")
IniWrite("file.ini", "HotKeys", "d", "hello")

Global $aHotKeys = IniReadSection("file.ini", "HotKeys")

For $i = 1 to $aHotKeys[0][0]
  HotKeySet($aHotKeys[$i][0], _Test)
Next

While Sleep (100)
WEnd

Func _Test()
  Local $iInd = _ArraySearch($aHotKeys, @HotKeyPressed, 1, Default, 1, 2, Default, 0)
  If @error Then Exit MsgBox ($MB_SYSTEMMODAL, "", "Error on hotkey search")
  ConsoleWrite ($aHotKeys[$iInd][1] & @CRLF)
EndFunc

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...