%A_ThisMenuItem% prints &

Get help with using AutoHotkey and its commands and hotkeys
nicrox
Posts: 14
Joined: 14 Mar 2016, 19:05

%A_ThisMenuItem% prints &

06 Sep 2017, 19:25

I've put together a Hotstring file to assist me with typing. in some instances such as below example, the returned string contains the ampersand:

Code: Select all

::ad#::
Menu, MyMenu, Add, &additional, MenuAction
Menu, MyMenu, Add, a&ddress, MenuAction
Menu, MyMenu, Show
return

MenuAction:
SendInput %A_ThisMenuItem%
 Menu, MyMenu, DeleteAll
Return
I am using "&" as per 'Menu' instructions, the & suppose to underline a character and turn it into a shortcut.
This is the output:
ad# -> press 'a' -> &additional
ad# -> press 'd' -> a&ddress

I also tried to use RegExReplace(%A_ThisMenuItem%, "\W") to take out the ampersand but RegExReplace throws an error complaining about %A_ThisMenuItem% containing an illegal character.
Any idea how I can get around this?
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: %A_ThisMenuItem% prints &

06 Sep 2017, 19:38

Function parameters are evaluated as expression components, yet variable names in an expression are not enclosed in percent signs (while litteral strings must be enclosed in double quotes)
documentation: expressions

Besides I see no reason of using RegExReplace: simply use StrReplace for simple substring substitutions; RegEx... is dedicaded for patterns (and unless you intend use and hence not match the litteral ampersand (i.e. &&))

Code: Select all

::ad#::
Menu, MyMenu, Add, &additional, MenuAction
Menu, MyMenu, Add, a&ddress, MenuAction
Menu, MyMenu, Show
return

MenuAction:
SendInput % StrReplace(A_ThisMenuItem, "&", "")
Menu, MyMenu, DeleteAll
Return
nicrox
Posts: 14
Joined: 14 Mar 2016, 19:05

Re: %A_ThisMenuItem% prints &

06 Sep 2017, 20:06

thanks for your prompt response A_AhkUser
The purpose of using RegExReplace is to ensure that & in a mid word situation is not removed. Such as "Ahk & auto hot key"

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Bort P, Google [Bot], guest3456 and 204 guests