convert hotkey syntax to send input syntax?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wdmodlin
Posts: 150
Joined: 16 Dec 2015, 02:42

convert hotkey syntax to send input syntax?

22 Mar 2016, 22:01

My game-monitor app supports simple autofire hotkeys.
A hotkey to toggle the autofire on and off, and another key to send to the game repeatedly until toggled off.
(with specified interval between keys, and notification beeps as the keys are toggled on and off).

Not hard to do.

But I did not want my user to have to learn any special syntax for naming the keys.

So I use a hotkey control to enter the toggle hotkey, with an extra checkbox for the # modifier. (windows key)

That gives me a key in the right form for making a hotkey.

But if I do the same for the key to send, it is not in the right form for sending input to the game.

Instead of "!F3", I need "!{F3}", for example.

Is there some easy built-in way to convert from hotkey syntax to send syntax?

Or do I have to parse the key from the hotkey box and figure out how to recode it?
If I do need to do it myself, is there an easy way to do that?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: convert hotkey syntax to send input syntax?

22 Mar 2016, 22:17

You could use A_ThisHotkey to identify the key name. The trick will be with incorporating modifiers. If it's possible someone uses !^::, you have to consider that the right most character which would normally be a modifier in this case is not, and that it's someone pressing Alt+Shift+6 -- though that may not even be possible in the Hotkey control so it shouldn't be a problem. What I would suggest is using RegExMatch/Replace to identify where the +!#^ symbols are, and put brackets around the remainder. I'm sure it can be done in RegExReplace, but I'd have to play around with it to know what.

Though, that may not be the full solution. I'm not sure you can do this and have it work as you expect:

Code: Select all

var:="^+s"
Send %var%
You can see if that works to bring up the typical Save As window in like a word document. If it does, I guess you can just set the value of var through the RegEx as I mentioned before.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: convert hotkey syntax to send input syntax?

22 Mar 2016, 22:43

The following will work with anything that has only one literal key, unless there is a delimiter (I don't know if the control adds delimiters between non-modifier keys).

Code: Select all

; Example
msgbox % hotkeyToSend("!F3")

; Function
hotkeyToSend(hotkey){
    return regExReplace(hotkey,"\w+","{$0}")
}
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: convert hotkey syntax to send input syntax?

23 Mar 2016, 04:18

The control (which is a standard Windows control) does not support custom combinations. It does not even support all standard combinations.

Here's another version which handles more hotkeys (that the control will never output):

Code: Select all

MsgBox % HotkeyToSend("$$")  ; The second "$" is not a modifier.
MsgBox % HotkeyToSend("~left & up")
MsgBox % HotkeyToSend("<!enter")
MsgBox % HotkeyToSend("*^lbutton up")

HotkeyToSend(hotkey, format := "{$0}") {
    if RegExMatch(hotkey, "(.*)[ `t]&[ `t](.*)", m)
        return HotkeyToSend(m1, "{$0 down}")
             . HotkeyToSend(m2)
             . HotkeyToSend(m1, "{$0 up}")
    return RegExReplace(RegExReplace(hotkey, "\w+(?i:[ `t]up)?|\W$", format), "(?<!{)[~$*<>]")
}
It doesn't distinguish between left and right ctrl/alt/shift/win; just strips out any modifiers that don't have meaning to Send.
wdmodlin
Posts: 150
Joined: 16 Dec 2015, 02:42

Re: convert hotkey syntax to send input syntax?

23 Mar 2016, 04:51

Thank you all.

I'm more interested in keeping it simple for the user than in supporting all key combinations... my user will just have to use something that does not produce "none" in the control.

The control only produces one key. Pressing a second one replaces what is there. So the question of delimiters between multiples does not arise.

I think that means that the simpler regex given by @Masonjar13 should suffice?

I'm still pretty fuzzy about a lot of details here :(
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: convert hotkey syntax to send input syntax?

23 Mar 2016, 05:22

lexikos wrote:The control (which is a standard Windows control) does not support custom combinations. It does not even support all standard combinations.
I figured as much. I've never actually used it myself, though, didn't know for sure.
wdmodlin wrote:I think that means that the simpler regex given by @Masonjar13 should suffice?
I believe so, yes. I'll explain it a bit.

In my code, I simply specify that it should find a word/an alphanumeric character (\w) of any length (+), and replace all of them with the found substring ($0) with added brackets around it; it's replacing the text with the same exact text, plus the brackets. And since it's a function, I have it return the new string, of course.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: convert hotkey syntax to send input syntax?

23 Mar 2016, 16:59

It will not necessarily be sufficient. For instance, on the French keyboard layout there is a key which produces ^ when typed into the hotkey control. You cannot send ^ without wrapping it to {^}. My code automatically handles this case by {wrapping} any single non-word character at the end of the hotkey.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: convert hotkey syntax to send input syntax?

23 Mar 2016, 20:12

Even on an English keyboard, what if someone wanted to do a hotkey like ^\ or !.? So lexikos's code would be good even if your intended audience is only expected to use English QWERTY keyboards.
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: convert hotkey syntax to send input syntax?

23 Mar 2016, 20:20

There's no need to modify ^\ or !. for sending. That is valid Send syntax.

The problem is only with characters which Send interprets specially, like ! # ^ { }. The hotkey control won't generate hotkeys with these characters on the standard US layout - they would be +2 +3 +6 etc. instead.

Of course, since you can just use the function I posted, there's not much reason to not support these exotic cases.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: convert hotkey syntax to send input syntax?

24 Mar 2016, 10:44

Oh, yeah, I totally forgot the whole purpose that it was the multiple-character-named keys like Space and Enter we needed to worry about. My apologies.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 101 guests