Error: Hotkeys/hotstrings are not allowed inside functions or classes. Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 17:41

Error: Hotkeys/hotstrings are not allowed inside functions or classes.

How do I add this functionality in the proper way?

Basically I want to bind left, up, right, and down arrows to modify the function of a 5th button. The reason is using the remote only one of the buttons allows itself to be held down and in doing so looping the pressed button.

Code: Select all

#HotIf WinActive("ahk_exe HD-Player.exe")
{
$up:: {
$del:: {
    While GetKeyState('del', 'P') {
        MouseMove 0, 10, 99999999, "R"
        Sleep 1
         }
          }
}
}

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]
User avatar
boiler
Posts: 17153
Joined: 21 Dec 2014, 02:44

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 17:45

@zed6250jb -- Once again, please use [code][/code] tags when posting code on the forum! Thank you.
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 18:06

I'm spoiled, used to quickly edit my post, post submit. Will have to refrain from clicking submit before doing a visual inspection. Thanks again.
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 18:37

Code: Select all

#Requires AutoHotkey v2.0
winTitle := 'ahk_exe HD-Player.exe'
; winTitle := 'ahk_exe Notepad.exe'

#HotIf WinActive(winTitle)
Up::
Del:: {
 While GetKeyState(ThisHotkey, 'P')
  MouseMove 0, 10, 1, 'R'
}
#HotIf
User avatar
RaptorX
Posts: 388
Joined: 06 Dec 2014, 14:27
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 18:45

Basically you just need to remove the brackets from the #HotIf directive.

The directive itself acts as the container for the code:

Code: Select all

#HotIf WinActive("ahk_exe HD-Player.exe")

f1::msgbox 'single line'
f2::
{
	msgbox 'multiline'
}

; more hotkeys

#HotIf
Projects:
AHK-ToolKit
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 18:49

That is not the bug. The hotkeys would be stacked by removing the second brace.
Multiple hotkeys can be stacked vertically to have them perform the same action. For example:

^Numpad0::
^Numpad1::
{
MsgBox "Pressing either Ctrl+Numpad0 or Ctrl+Numpad1 will display this."
}

Source: Hotkeys - Definition & Usage | AutoHotkey v2
User avatar
RaptorX
Posts: 388
Joined: 06 Dec 2014, 14:27
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 19:19

mikeyww wrote:
22 Apr 2024, 18:49
That is not the bug. The hotkeys would be stacked by removing the second brace.
Multiple hotkeys can be stacked vertically to have them perform the same action. For example:

^Numpad0::
^Numpad1::
{
MsgBox "Pressing either Ctrl+Numpad0 or Ctrl+Numpad1 will display this."
}

Source: Hotkeys - Definition & Usage | AutoHotkey v2
You are correct I didnt see the additional brackets on the first hotkey.

I also didnt know that the brackets could be used to enclose code inside a hotif statement.
Projects:
AHK-ToolKit
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 20:27

The outer braces are not necessary and, I would say, are also not recommended. In this case, they do not create a specific problem, but in other situations, they might.
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 20:47

Alright, that progressed my script to a degree. I was wanting it to behave such that remapping would occur on the fly, because of the limited number of buttons on the ir remote. Here is what I have so far and the error it spits.

Code: Select all

#Requires AutoHotkey v2.0
winTitle := 'ahk_exe HD-Player.exe'
; winTitle := 'ahk_exe Notepad.exe'

#HotIf WinActive(winTitle)
Up::
Down:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
}
Down::
up:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
}
Right::
Left:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
}
Left::
Right:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
}
#HotIf
Attachments
image.png
(11.45 KiB) Downloaded 78 times
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 20:55

Here is an alternative display of the script but executes with-OUT error, as mentioned prior, undesirable due to unruly amount of button maps.

Code: Select all

#Requires AutoHotkey v2.0
winTitle := 'ahk_exe HD-Player.exe'
; winTitle := 'ahk_exe Notepad.exe'

#HotIf WinActive(winTitle)
Up::
del:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
}
Down::
pgdn:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
}
Right::
ins:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
}
Left::
/:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
#HotIf
}
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 21:00

If not for the duplicate hotkey error, ideally it would behave as intended and as follows.

Code: Select all

#Requires AutoHotkey v2.0
winTitle := 'ahk_exe HD-Player.exe'
; winTitle := 'ahk_exe Notepad.exe'

#HotIf WinActive(winTitle)
Up::
end:: {
 While GetKeyState('end', 'P')
  MouseMove 0, -10, 1, 'R'
  sleep 100
}
Down::
end:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
  sleep 100
}
Right::
end:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
  sleep 100
}
Left::
end:: {
 While GetKeyState('end', 'P')
  MouseMove 0, 10, 1, 'R'
  sleep 100
}
#HotIf
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 21:09

Although you posted many scripts, nowhere in this thread have you described what your script should do. In addition, no examples are provided. A script is not a statement or description of your intent. It is merely a product of your coding. Without any statement of your goal, I cannot comment on how you should achieve it (though others might be able to do so!).

As the error message has directly indicated to you, a script cannot contain duplicated hotkeys within the same context. Adding hotkeys to a broken script will yield a broken script. Therefore, I suggest that you shorten your script to a single hotkey so that you can get it working. After it works, expand.

Instead of having a sequence of hotkeys that are all the same, combine the code into a single hotkey (function). As mentioned, just get one hotkey working, and remove the rest. After it works, add a new hotkey separately. Test iteratively.

Although I posted a script, you have not indicated whether it worked. You have no obligation to do any such thing, but if you do not want anyone to post scripts, you might consider providing that information so that forum readers do not waste their time. It is fine to do so, and some writers just want advice or ideas rather than scripts handed to them.
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

22 Apr 2024, 22:15

fair enough mike, please let me explain...

"Instead of having a sequence of hotkeys that are all the same, combine the code into a single hotkey (function). As mentioned, just get one hotkey working, and remove the rest. After it works, add a new hotkey separately. Test iteratively."

When you have buttons that are being remapped and are interdependent, then it requires me to show all four hotkeys.

"nowhere in this thread have you described what your script should do. "

Basically I want to bind left, up, right, and down arrows to modify the function of a 5th button. The reason is using the remote only one of the buttons allows itself to be held down and in doing so looping the pressed button. I said this in the first post of this thread.

So to further explain in detail, if I press up (keypress), end (keypress) then has move mouse up. I press left (keypress) , end (keypress) then moves mouse left, I press right (keypress), end (keypress) then moves mouse right, and finally I press down (keypress), end (keypress) then moves mouse down.

The reason for me wanting the script to behave in this manner is due to the fact that only one button functions continuously as I press it down, and every single other button on the remote to which I have keyboard keys remapped to only functions for three or so seconds which is a horrible ux experience when you are trying to use keys or in the final case buttons on the remote, to move the mouse cursor around.

Thanks for your feedback, I always strive to improve my methodology.
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

23 Apr 2024, 00:08

I'm not sure what this means: "I press up (keypress), end (keypress)". Is this a sequence? A combination? Something else?

Hotkeys work individually unless you add code to handle sequencing. Naming two or more hotkeys on consecutive lines does not actually enact or detect a key sequence. Below is information about custom combinations. Perhaps this helps.

https://www.autohotkey.com/docs/v2/Hotkeys.htm#combo
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

23 Apr 2024, 18:01

Thanks for your reply Mike, Incidentally, the roku/hisense tv remote was solely at fault for their crappy implementation of an ir remote. I bought another ir remote made by direct-tv (black a white, and shapely, I guess it's called a genie or something) and seems to work flawlessly. Here is the last and likely final update to the repo. Thanks for your attention again, and sorry if I took your time.

https://github.com/zed6250JB/ahkFLIRC/blob/main/rebind.ahk
User avatar
RaptorX
Posts: 388
Joined: 06 Dec 2014, 14:27
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.  Topic is solved

25 Apr 2024, 18:44

zed6250jb wrote:
23 Apr 2024, 18:01
Thanks for your reply Mike, Incidentally, the roku/hisense tv remote was solely at fault for their crappy implementation of an ir remote. I bought another ir remote made by direct-tv (black a white, and shapely, I guess it's called a genie or something) and seems to work flawlessly. Here is the last and likely final update to the repo. Thanks for your attention again, and sorry if I took your time.

https://github.com/zed6250JB/ahkFLIRC/blob/main/rebind.ahk

I saw the code over there and noticed you can simplify a bit by using the switch/case statements or if statements if you prefer.
This is probably what you were attempting to achieve when you were stacking your hotkeys on your OP, not sure.

Code: Select all

#HotIf WinActive("ahk_exe HD-Player.exe")

$up::
$down::
$left:: 
$right:: {
	switch hk := StrReplace(A_ThisHotkey, "$")
	{
	case 'up':    x:= 0 , y:= -8
	case 'down':  x:= 0 , y:= 8
	case 'left':  x:= -8, y:= 0
	case 'right': x:= 8 , y:= 0
	}
	
	While GetKeyState(hk, 'P') {
		MouseMove x, y , 45, "R"
		Sleep 1
	}
}

$^m::
$\:: {
	switch hk := StrReplace(A_ThisHotkey, "$")
	{
	case '\': button := 'middle'
	case '^m': button := 'left', hk := StrReplace(hk, '^')
	}
	
	While GetKeyState(hk, 'P') {
		MouseClick button
		Sleep 1000
	}
}

$,:: 
$.:: {

	switch hk := StrReplace(A_ThisHotkey, "$")
	{
	case ',': direction := 'down'
	case '.': direction := 'up'
	}
	
	While GetKeyState(hk, 'P') {
		Send "{Wheel" direction "}"
		Sleep 1
	}
}

$f1:: 
$f2:: {
	switch hk := StrReplace(A_ThisHotkey, "$")
	{
	case 'f1': key := 'left'
	case 'f2': key := 'right'
	}
	
	Send "{" key "}"
	Sleep 100
}

$!z:: {
	{
		MouseMove 198,1080
		Sleep 100
	}
}

#HotIf
I noticed that on a few of them you didnt use the GetKeyState function, is there a reason for that?
Projects:
AHK-ToolKit
zed6250jb
Posts: 54
Joined: 20 Apr 2024, 18:25
Contact:

Re: Error: Hotkeys/hotstrings are not allowed inside functions or classes.

25 Apr 2024, 20:05

(...didnt use the GetKeyState function...)Not specifically other than it was only explicitly needed with the mouse cursor movement. The rest achieves the needed outcome without GetKeyState.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Daniel_San and 35 guests