Question on closure Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
live_opt
Posts: 1
Joined: 26 May 2023, 21:36
Contact:

Question on closure

26 May 2023, 21:39

Basically, I am trying to do that. And that doesn't work out very well... It tells me impl is not a valid argument of Hotkey function. I am using AHK v2.

Code: Select all

; Win + Ctrl + [0-9] = Move window to desktop [1-9] and switch to it
i := 1
while (i <= desktop_count) {
	impl() {
		MoveCurrentWindowToDesktop i
		GoToDesktopNumber i
	}
	Hotkey "#^" . i, impl
	i++
}
User avatar
boiler
Posts: 17302
Joined: 21 Dec 2014, 02:44

Re: Question on closure  Topic is solved

26 May 2023, 23:08

As the documentation says:
Hokey documentation wrote:If Action is a function, it is called with one parameter...
So your function is invalid because it doesn't accept a parameter. You can have it ignore the parameter with a * in its definition:

Code: Select all

	impl(*) {

I assume there is more to your script than what you've shown.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Question on closure

27 May 2023, 03:49

also the defined hotkeys wont do what u would expect them to: "i" will be set to whatever value it last had

u cant use closures inside a loop like that, they capture variables, not the variables' values. u have to use .Bind()

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: jooolius, st-webmas and 53 guests