Jump to content

Check for custom hotkeys in a .ini


Recommended Posts

I have a .ini files which contains hotkeys among other things.

[HotKeys]

VNCNew=^!y

VNCOld=^+v

NotepadKey=#n

CMDKey=#`

PasteSig=^!s

PasteOPR=^!a

This .ini is generated the first time the script runs, and should allow the hotkeys to be changed by the user after the fact.

For some reason my code to check for .ini changes doesnt work.

; Hotkey vars
Global $HKVNCNew = "^!v"

; Set up hotkey assignments. ^ = Ctrl, ! = Alt, + = Shift
HotKeySet($HKVNCNew, "VNCNew")


Func ConfigCreate()
;There exists extra code HERE but i removed it to make it easier for you to read.
HotkeyCheck("HotKeys","VNCNew","^!v")
IniWrite($strINIFile, "Misc", "Version", $intVersion)
EndFunc


; Check if hotkey is set, and if it isn't, set it to the default.
Func HotkeyCheck($section,$key,$default)
If IniRead($strINIFile,$section,$key,"") == "" Then
IniWrite($strINIFile,$section,$key,$default)
EndIf
EndFunc


;Read values from SDUtil.ini for hotkeys and passwords, and call VersionCheck()
Func ConfigRead()
VersionCheck()
;Extra code removed again
$HKVNCNew = IniRead($strINIFile,"HotKeys","VNCNew","^!v")
EndFunc

ConfigRead is the important part, but i figured the rest might affect it.

Any ideas?

Link to comment
Share on other sites

As you wrote, you removed some of the code (so I needed to add some of this back to provide a working way...)

Maybe I'm wrong, or you removed this code part as well, but in your original post you read the INI but you do not apply the read values to the hotkey?

Is this the problem?

So in the func HotkeyCheck I kept your code to set the default value if empty , clear the key and set the key to the INI value.

Of course this opens the file twice. But Maybe this shows a way...

Does this somehow help you?

; Hotkey vars

global $strINIFile = "HOTKEY.INI"
global $intVersion= "1.0"
Global $HKVNCNew = "^!v"


; Set up hotkey assignments. ^ = Ctrl, ! = Alt, + = Shift
HotKeySet($HKVNCNew, "VNCNew")
ConfigCreate()
While 1
Sleep(100)
WEnd


Func ConfigCreate()
;There exists extra code HERE but i removed it to make it easier for you to read.
HotkeyCheck("HotKeys","VNCNew","^!v")
IniWrite($strINIFile, "Misc", "Version", $intVersion)
EndFunc


; Check if hotkey is set, and if it isn't, set it to the default.
Func HotkeyCheck($section,$key_ini,$default)
         ConsoleWrite("Set key:"&IniRead($strINIFile,$section,$key_ini,"")&@CRLF)

         If IniRead($strINIFile,$section,$key_ini,"") == "" Then
IniWrite($strINIFile,$section,$key_ini,$default)
EndIf
HotKeySet(IniRead($strINIFile,$section,$key_ini,""))
HotKeySet(IniRead($strINIFile,$section,$key_ini,""),$key_ini)
ConsoleWrite("Set key:"&IniRead($strINIFile,$section,$key_ini,"")&" to start "&$section&@CRLF)
EndFunc


;Read values from SDUtil.ini for hotkeys and passwords, and call VersionCheck()
Func ConfigRead()
     VersionCheck()
     ;Extra code removed again
     $HKVNCNew = IniRead($strINIFile,"HotKeys","VNCNew","^!v")
EndFunc

func VersionCheck()
;~ dummy code
EndFunc


func VNCNew()
     run("notepad.exe")
EndFunc

//edit: Sorry as I saw "I" didn' use the function ConfigRead.

On my approach the config is read in the beginning with ConfigCreate. So I guess I missunderstood the question. But you may find a solution still inthe function Hotkeycheck as mentioned.

Sorry for answering in a so confused way....shouldn't do this on a helping thread....

Edited by Tankbuster
Link to comment
Share on other sites

JustReidy,

Having some trouble following your code, for example:

- in ConfigCreate you run func HotKeyCheck then do an iniwrite for something (not sure if it is related to the problem)

- don't know what VersionCheck() is

- you are not checking anywhere for ini changes

- not sure what you mean by "; Check if hotkey is set, and if it isn't, set it to the default.". Exist in ini file?

I don't use ini files much but if I wanted to check for the existence of a value and add it if it does not exist I would do it like this.

; create a test ini file

local $ini_file = @scriptdir & '\test.ini'
local $ini_data = 'VNCNew=^!y' & @lf & 'VNCOld=^+v' & @lf & 'NotepadKey=#n' & @lf & 'CMDKey=#`' & @lf & 'PasteSig=^!s' & @lf & 'PasteOPR=^!a'
iniwritesection($ini_file,'Hotkeys',$ini_data)

; set key value to test for and new key/value pair to add if not found

local $newkey='new key', $newvalue = 'new value', $ret

; test 1 - value should not exist

if hotkeycheck($newkey) = 'Not Found' then
    $ret = iniwrite($ini_file,'Hotkeys',$newkey,$newvalue)
    if $ret <> 1 then msgbox(0,'ERROR','Ini Write Failed' & @lf & 'key = ' & $newkey & @lf & 'value = ' & $newvalue)
Else
    ConsoleWrite('Hotkey in use - INI Value = ' & iniread($ini_file,'Hotkeys',$newkey,'ERROR') & @LF)
endif

; test 2 - value should exist and issue a console message

$newkey = 'VNCNew'
if hotkeycheck($newkey) = 'Not Found' then
    $ret = iniwrite($ini_file,'Hotkeys',$newkey,$newvalue)
    if $ret <> 1 then msgbox(0,'ERROR','Ini Write Failed' & @lf & 'key = ' & $newkey & @lf & 'value = ' & $newvalue)
Else
    ConsoleWrite('Hotkey in use - INI Key = ' & $newkey & ' INI Value = ' & iniread($ini_file,'Hotkeys',$newkey,'ERROR') & @LF)
endif

; check for ini key and return value or default ('Not Found')

func hotkeycheck($key)
    return iniread($ini_file,'Hotkeys',$key,'Not Found')
endfunc

; display ini file

shellexecute($ini_file)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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