KeyPress Variable Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Disguise_AU
Posts: 2
Joined: 25 May 2022, 05:50

KeyPress Variable

Post by Disguise_AU » 25 May 2022, 06:08

G'day, I haven't done anything with AHK for about 8 years, I've just recently come back to it and have found myself stuck on something. I'm trying to use a variable to define a hotkey, however I'm finding that it isn't working for me, I've tried several combinations of code to make it work and have spent some time looking at the documentation as well as some forums where people have asked questions about the KeyPress command but still can't work it out.

I have defined a key in an ini file:

Code: Select all

[Example]
Key=CapsLock
and have also tried:

Code: Select all

[Example]
Key={CapsLock}
Here is a simplified and cut down version of the relevant lines of code in my script to show basically what I'm trying to achieve with this part of my script:

Code: Select all

;... 
IniRead, Key, %A_ScriptDir%/example.ini, Example, Key
;... 
KeyWait, %Key%
Goto KeyPress
Return
;... 
KeyPress: 
MsgBox, Perform some action here.. 
return 
I am aware that there is a much better way to do this, but this example is an easy way to show what result I'm trying to achieve.

Can someone please give me some advice on how I can change this to get the script to do what I'm trying to do?

Thanks.

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: KeyPress Variable  Topic is solved

Post by boiler » 25 May 2022, 06:28

The problem has nothing to do with the variable or your ini file. You need to change your KeyWait line to include the D option to wait until the key is pressed down or else you’re waiting for the key to be released, which it already is, so it continues without waiting. Change it to this:

Code: Select all

KeyWait, %Key%, D

If you want to wait until the key is pressed down and then released before continuing, you would use two lines like this:

Code: Select all

KeyWait, %Key%, D
KeyWait, %Key%

Use the first version of your ini file.

Disguise_AU
Posts: 2
Joined: 25 May 2022, 05:50

Re: KeyPress Variable

Post by Disguise_AU » 30 Jun 2022, 03:59

Thank you! Your help is appreciated :)

Post Reply

Return to “Ask for Help (v1)”