assigning a function to a hotkey

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

assigning a function to a hotkey

Post by helming » 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


p::Msgbox, contents is %Keydown2%

aw(b1)	{
	Sleep 1500
	Send {%b1% down}
	Keydown2 := b1
	}

7::aw(g)

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


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: 2873
Joined: 17 Oct 2015, 20:28

Re: assigning a function to a hotkey

Post by Exaskryz » 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: 13
Joined: 07 Dec 2016, 15:01

Re: assigning a function to a hotkey

Post by helming » 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: 2873
Joined: 17 Oct 2015, 20:28

Re: assigning a function to a hotkey

Post by Exaskryz » 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 [url=https://autohotkey.com/docs/Functions.htm#Locals]global[/url], that should make all of its variables accessible outside of the function.
helming
Posts: 13
Joined: 07 Dec 2016, 15:01

Re: assigning a function to a hotkey

Post by helming » 21 Nov 2017, 10:16

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

Code: Select all

dir12 := "g"
6::

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

Code: Select all

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

Code: Select all

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: 294
Joined: 18 Nov 2017, 05:50

Re: assigning a function to a hotkey

Post by Osprey » 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

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

Re: assigning a function to a hotkey

Post by helming » 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.
Post Reply

Return to “Ask For Help”