assigning a function to a hotkey

Get help with using AutoHotkey and its commands and hotkeys
helming
Posts: 12
Joined: 07 Dec 2016, 15:01

assigning a function to a hotkey

21 Nov 2017, 09:30

Hello, I have a basic question. I've assigned a function to a hotkey, But it seems functions isn't called at all.

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



Pressing 7 doesn't invoke anything. After pressing 7, I tried to press p which shows keydown2 is empty.

Up until now, I've used format like below

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus


7::
dir := "g"
Sleep 1500
Send {%dir% down}
Keydown2 := dir
Return


Then, I thought using a function would make the script simpler for similar hotkeys.

Could anyone tell me what's wrong with this script?
User avatar
Exaskryz
Posts: 2690
Joined: 17 Oct 2015, 20:28

Re: assigning a function to a hotkey

21 Nov 2017, 09:38

Try 7::aw("g"). Functions expect variables, not strings, so you need to explicitly define a string with the double quotation marks. Otherwise, it's doing Send { down} which is non-sensical to ANK.
helming
Posts: 12
Joined: 07 Dec 2016, 15:01

Re: assigning a function to a hotkey

21 Nov 2017, 10:02

Thanks so much Exaskryz. I just checked it out, it sends g down properly. But when I press p, It still say Keydown2 is empty. Why is that?
User avatar
Exaskryz
Posts: 2690
Joined: 17 Oct 2015, 20:28

Re: assigning a function to a hotkey

21 Nov 2017, 10:05

Hmm, I don't use functions too often, but I believe it's because the "Keydown2" variable is local only to the function. If you make the first line of the function global, that should make all of its variables accessible outside of the function.
helming
Posts: 12
Joined: 07 Dec 2016, 15:01

Re: assigning a function to a hotkey

21 Nov 2017, 10:16

Really Really thank you! Speaking of global, i have another question.

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

dir12 := "g"
6::

global dir12
Sleep 1500
Send {%dir12% down}
Keydown2 := dir12
Return


Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

global dir12 := "g"
6::
Sleep 1500
Send {%dir12% down}
Keydown2 := dir12
Return


Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

global dir12
dir12 := "g"
6::

global dir12
Sleep 1500
Send {%dir12% down}
Keydown2 := dir12
Return


None of them doesn't work. Sorry for bothering you with such a rudimentary question, could you tell me what's the problem?
Osprey
Posts: 225
Joined: 18 Nov 2017, 05:50

Re: assigning a function to a hotkey

21 Nov 2017, 11:24

Global should be in the function, not the hotkey subroutine. aw() is your function, according to your first post, so you'd do this:

Code: [Select all] [Download] GeSHi © Codebox Plus

aw(b1)	{
global
Sleep 1500
Send {%b1% down}
Keydown2 := b1
}
helming
Posts: 12
Joined: 07 Dec 2016, 15:01

Re: assigning a function to a hotkey

21 Nov 2017, 20:11

That's not my point. First question was about function, above question was about using a global variable in a hotkey. In other words, Is it impossible to use a global variable in a hotkey command?

Ah, Osprey maybe you're answering my question about Keydown2 being empty? Well, that was solved thanks to Exaskryz.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], blad4 and 38 guests