Wildcard turned to toggle with expression syntax, bug?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
raron
Posts: 37
Joined: 11 Aug 2014, 00:50

Wildcard turned to toggle with expression syntax, bug?

Post by raron » 08 Sep 2021, 02:23

Hi all.

Not sure if this is a bug or not, just thought I'd post this. Maybe I'm using the expression syntax wrong in this case? Consider the following two hotkey down/up definitions:

Code: Select all

hkprefix := "*"

SetHotkeys:
	Hotkey, % hkprefix . key_forward,     Forward_axis_on,  On
	Hotkey, % hkprefix . key_forward up,  Forward_axis_off, On

	Hotkey, %hkprefix%%key_back%,        Back_axis_on,     On
	Hotkey, %hkprefix%%key_back% up,     Back_axis_off,    On
(hkprefix is in the scripts auto-execute part)

This turned key_forward into a toggle.
The second hotkey, key_back, kind of worked, but at least not as a toggle. I say kind of, because both versions apparently turned key repeat on again :(

To prevent that I used the trick from "lexikos", last post (so far, of Mar 05, 2018) here: viewtopic.php?t=45020
Which worked great until now. The trick was to turn off and then on again the hotkey, to avoid key repeats.
(I'm a bit surprised it worked, I feared the key repeats would sneak in between turning the hotkey off and then on again some time later, but it didn't. Until now that is)

Reposting the trick here:

Code: Select all

1::
   Hotkey 1, Off
   SendEvent {Blind}{LButton down}
return
   
1 up::
   Hotkey 1, On
   SendEvent {Blind}{LButton up}
return
But the above wildcard made it not work as intended.
(I've not been successful in using KeyWait).


Sidenote:

This is because I suddenly realised I needed hotkeys to ignore the SHIFT and CTRL keys (for a game). I might look into making separate hotkeys including combinations of those triggering the same label, not sure how that will work yet. Probably at best I just have to repeat the hotkeys 3 more times (no modifier, shift alone, ctrl alone, and combined.. maybe times 2 if including Alt. And times 2 for the up and down events...).

Actually writing this out now it seems like the simplest solution atm. And a short test just now seems to indicate it works :)

This will then be the initialization for just one hotkey:

Code: Select all

SetHotkeys:
	Hotkey, %key_forward%,     Forward_axis_on,  On
	Hotkey, +%key_forward%,     Forward_axis_on,  On
	Hotkey, ^%key_forward%,     Forward_axis_on,  On
	Hotkey, +^%key_forward%,     Forward_axis_on,  On
	Hotkey, %key_forward% up,  Forward_axis_off, On
	Hotkey, +%key_forward% up,  Forward_axis_off, On
	Hotkey, ^%key_forward% up,  Forward_axis_off, On
	Hotkey, +^%key_forward% up,  Forward_axis_off, On
(Guess it can put it in a loop to make it look nicer

EDIT: Typo
Last edited by raron on 08 Sep 2021, 15:10, edited 1 time in total.

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Wildcard turned to toggle with expression syntax, bug?

Post by mikeyww » 08 Sep 2021, 05:52

You can use * to represent all modifiers at once.

Explained: https://www.autohotkey.com/docs/Hotkeys.htm#wildcard

User avatar
raron
Posts: 37
Joined: 11 Aug 2014, 00:50

Re: Wildcard turned to toggle with expression syntax, bug?

Post by raron » 08 Sep 2021, 12:21

I tried this:

Code: Select all

Hotkey, *%key_forward%,     Forward_axis_on,  On
Hotkey, *%key_forward% up,  Forward_axis_off, On
(Not sure why I put the "*" in a variable before).

But then key repeat turns back on. My cumbersome version at the end of my post above works (but it doesn't take all modifiers into account. Haven't tried that yet).

I figured out my error with the expression syntax. It should have been:

Code: Select all

Hotkey, % "*" . key_forward,     Forward_axis_on,  On
Hotkey, % "*" . key_forward . " up",  Forward_axis_off, On
I missed the . " up" part.
But it works the same as the above, IE key repeats turns on.

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Wildcard turned to toggle with expression syntax, bug?

Post by mikeyww » 08 Sep 2021, 12:27

Debugging a snippet can be difficult at times. Perhaps you can post your script.

User avatar
raron
Posts: 37
Joined: 11 Aug 2014, 00:50

Re: Wildcard turned to toggle with expression syntax, bug?

Post by raron » 08 Sep 2021, 15:24

mikeyww wrote:
08 Sep 2021, 12:27
Debugging a snippet can be difficult at times. Perhaps you can post your script.
It's starting to get somewhat complex at ~20kb :P (plus a couple of libraries).

I might post a shorter excerpt if I can't solve the hotkey thing, but if the above works then I'll go with that (I haven't had the time to work more on it than the above posts atm).
I plan to post it anyway if I get it working. For now the functionality is just too awkward to really improve the game experience. I'm starting to get some doubts, but I'm not done with it yet.

Also, thanks for the replies!

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Wildcard turned to toggle with expression syntax, bug?

Post by mikeyww » 08 Sep 2021, 15:32

The following worked in Notepad.

Code: Select all

key_forward = F3
Hotkey, *%key_forward%   , Forward_axis_on ,  On
Hotkey, *%key_forward% Up, Forward_axis_off, On
Return

Forward_axis_on:
Send a
KeyWait, %key_forward%
Return

Forward_axis_off:
Send b
Return
Also:

Code: Select all

key_forward = 1
Hotkey, *%key_forward% Up, Forward_axis_off, On
On:
Hotkey, *%key_forward%   , Forward_axis_on , On
Return

Forward_axis_on:
Hotkey, *%key_forward%   , Forward_axis_on , Off
SoundBeep, 1500
Click, D
Return

Forward_axis_off:
Click, U
Gosub, On
SoundBeep, 1000
Return
If Shift is pressed, then you get the effect of Shift+Click. You might be able to counteract that by sending Shift up first.

Code: Select all

#SingleInstance Force
key_forward = 1
Hotkey, *%key_forward% Up, Forward_axis_off, On
On:
Hotkey, *%key_forward%   , Forward_axis_on , On
Return

Forward_axis_on:
Hotkey, *%key_forward%   , Forward_axis_on , Off
SoundBeep, 1500
Send {Blind}{Shift up}
Click, D
Return

Forward_axis_off:
Click, U
Gosub, On
SoundBeep, 1000
Return
The same may be true for other modifiers.

User avatar
raron
Posts: 37
Joined: 11 Aug 2014, 00:50

Re: Wildcard turned to toggle with expression syntax, bug?

Post by raron » 08 Sep 2021, 16:13

Great! Your second example works, thanks!

There are timers involved, so I guess that's why keywait can't be used (I didn't try that now). If the hotkey is still down when the timer runs out, it starts the timer again (one-shot timers).

Btw, this might be my fault (usually is lol), because this was kind of what I was already doing. But I forgot to change the hotkey wildcard where they were initially set (or removed if changed). So I ended up turning off a non-existant hotkey. (of course I used UseErrorLevel Off... probably not needed there).

User avatar
raron
Posts: 37
Joined: 11 Aug 2014, 00:50

Re: Wildcard turned to toggle with expression syntax, bug?

Post by raron » 11 Sep 2021, 03:11

I spoke too early!

It was still the same (not sure how I missed that). IE key repeats... Anyway, using this method currently, which works for now:

Code: Select all

SetHotkeys:
	loop, % modifierCombo.Length()
	{
		Hotkey, % modifierCombo[A_Index] . key_forward,         Forward_axis_on,  On
		Hotkey, % modifierCombo[A_Index] . key_forward . " up", Forward_axis_off, On

		... other hotkeys...
	}
Return
Same for turning them off.
Where modifierCombo[] is an array of all combinations of the modifiers I want to ignore. Basically just Shift and Ctrl (maybe Alt later).
Maybe a better way will present itself later. Been thinking that setting a boolean flag (to prevent key repeats) may be better / more elegant.

Post Reply

Return to “Ask for Help (v1)”