Get Hotkey Control text using ControlGetText

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Get Hotkey Control text using ControlGetText

25 Nov 2015, 16:47

I need to get the text of an Hotkey Control.

This returns a blank value.

Code: Select all

ControlGetText,var, msctls_hotkey324, ahk_id %CHID%
msgbox % var
Any suggestions?

thanks
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Get Hotkey Control text using ControlGetText

25 Nov 2015, 17:24

Try removing "ahk_id" and adding a colon after the GUI name:

Code: Select all

Gui, msctls_hotkey324:Add, Hotkey, +HwndCHID
Gui, msctls_hotkey324:Add, Button, gB1, Test
Gui, msctls_hotkey324:Show
return

B1:
GuiControlGet, MyVar, msctls_hotkey324:, %CHID%
MsgBox, % MyVar
return

Esc::
msctls_hotkey324GuiClose:
ExitApp
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get Hotkey Control text using ControlGetText

25 Nov 2015, 17:36

thanks for the quick response.
Your example returns the hotkey not the text.

Using your example I selected Ctrl N as the hotkey. The hotkey control shows Control + N

Your example returns ^n
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: Get Hotkey Control text using ControlGetText

25 Nov 2015, 18:22

Try this:

Code: Select all

GuiControlGet Hotkey,, %hHotkey%
StringReplace Hotkey, Hotkey, +, % "Shift + "
StringReplace Hotkey, Hotkey, ^, % "Ctrl + "
StringReplace Hotkey, Hotkey, !, % "Alt + "
StringUpper Hotkey, Hotkey, T
MsgBox % Hotkey
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Get Hotkey Control text using ControlGetText

25 Nov 2015, 18:59

I suggest trying Alguimist's suggestion. Although, it has at least one drawback: the order of the modifiers is different. Ex: the hotkey control shows "Ctrl + Shift + A" but the MsgBox shows "Shift + Ctrl + A". It would be easy to rearrange the order though.

The following is just something I was playing with based on the info found at About Hot Key Controls. This seems to explain why it is not straightforward to get text from a hotkey control. The control doesn't seem to provide a mechanism to directly retrieve the text it displays.
This will report different results for some keys. Ex: the hotkey control shows "Home" but this shows "NumpadHome" because it gets the key name from the VK code.

Code: Select all

Gui, msctls_hotkey324:Add, Hotkey, +HwndCHID
Gui, msctls_hotkey324:Add, Button, gB1, Test
Gui, msctls_hotkey324:Show
return

B1:
SendMessage, 0x0402, 0, 0,, ahk_id %CHID% ; WM_USER | HKM_GETHOTKEY
Reply := ErrorLevel
KeyName := GetKeyName("VK" Format("{:x}", Reply & 0xFF))
if (StrLen(KeyName) = 1) { ; Convert to uppercase
    KeyChar := Asc(KeyName)
    if (KeyChar > 96 && KeyChar < 123)
        KeyName := Chr(KeyChar ^ 0x20)
}
KM := (Reply >> 8) & 0xFF
HotkeyText := (KM & 0x02 ? "Ctrl + "  : "") ; HOTKEYF_CONTROL
            . (KM & 0x01 ? "Shift + " : "") ; HOTKEYF_SHIFT
            . (KM & 0x04 ? "Alt + "   : "") ; HOTKEYF_ALT
            .  KeyName
MsgBox, % HotkeyText
return

Esc::
msctls_hotkey324GuiClose:
ExitApp
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get Hotkey Control text using ControlGetText

26 Nov 2015, 02:12

Here is an example using Alguimist's suggestion.

Hopefully what ever is in the hotkey control will show up in the edit control when the test button is pressed.

It appears that NumpadHome and Num 7 will always show up as Num 7 in the Hotkey Control even though they are 2 seperate hotkeys. And all the rest of the Numpad keys, etc...NumpadUp is displayed as Num 8. This could be very confusing for someone that wanted to use several Numpad hotkeys.

Have I missed something?

Code: Select all

Gui, msctls_hotkey324:Add, Hotkey, gClearEditControl +HwndCHID
Gui, msctls_hotkey324:Add, Edit, 
Gui, msctls_hotkey324:Add, Button, gB1, Test
Gui, msctls_hotkey324:Show
return
 
B1:
GuiControlGet, MyHotkey, msctls_hotkey324:, %CHID%
StringReplace MyHotkey, MyHotkey, +, % "Shift + "
StringReplace MyHotkey, MyHotkey, ^, % "Ctrl + "
StringReplace MyHotkey, MyHotkey, !, % "Alt + "
StringReplace MyHotkey, MyHotkey, Numpad, % "Num "
StringReplace MyHotkey, MyHotkey, Mult, % "* "
StringReplace MyHotkey, MyHotkey, Div, % "/ "
StringReplace MyHotkey, MyHotkey, Add, % "+ "
StringReplace MyHotkey, MyHotkey, Sub, % "- "
StringReplace MyHotkey, MyHotkey, Sc145, % "Num Lock "
StringReplace MyHotkey, MyHotkey, Sc135, % "Num / "
StringReplace MyHotkey, MyHotkey, Dot, % "Del "
StringReplace MyHotkey, MyHotkey, Clear, % "5 "
StringReplace MyHotkey, MyHotkey, ~, % ""
StringReplace MyHotkey, MyHotkey, Num Ins, % "Num 0 "
StringReplace MyHotkey, MyHotkey, Num End, % "Num 1 "
StringReplace MyHotkey, MyHotkey, Num Down, % "Num 2 "
StringReplace MyHotkey, MyHotkey, Num PGDN, % "Num 3 "
StringReplace MyHotkey, MyHotkey, Num Left, % "Num 4 "
StringReplace MyHotkey, MyHotkey, Num Right, % "Num 6 "
StringReplace MyHotkey, MyHotkey, Num Home, % "Num 7 "
StringReplace MyHotkey, MyHotkey, Num Up, % "Num 8 "
StringReplace MyHotkey, MyHotkey, Num PGUP, % "Num 9 "
StringReplace MyHotkey, MyHotkey, PGUP, % "Page Up "
StringReplace MyHotkey, MyHotkey, PGDN, % "Page Down "
StringReplace MyHotkey, MyHotkey, CapsLock, % "Caps Lock "
StringReplace MyHotkey, MyHotkey, Shift + Ctrl, % "Ctrl + Shift "
StringUpper MyHotkey, MyHotkey, T
If MyHotkey =
 MyHotkey = None

GuiControl,,edit1,%MyHotkey%
return
ClearEditControl:
GuiControl,,edit1,
return
Esc::
msctls_hotkey324GuiClose:
ExitApp
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get Hotkey Control text using ControlGetText

26 Nov 2015, 13:17

I noticed something odd...
You can load the Gui Control with a non supported Gui Control hotkey and as expected it does not display it, but GuiControlGet will retrieve it.

Run this code and immediately press the test button and Mbutton1 will be retrieved as the hotkey even though it is not displayed.

Code: Select all

Gui, msctls_hotkey324:Add, Hotkey, +HwndCHID, XButton1
Gui, msctls_hotkey324:Add, Edit, 
Gui, msctls_hotkey324:Add, Button, gB1, Test
Gui, msctls_hotkey324:Show
return
 
B1:
GuiControlGet, MyHotkey, msctls_hotkey324:, %CHID%
GuiControl,,edit1,%MyHotkey%
return

Esc::
msctls_hotkey324GuiClose:
ExitApp
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get Hotkey Control text using ControlGetText

27 Nov 2015, 10:00

In addition to GuiControlGet retrieving the non Gui Hotkey Control supported hotkey the hotkey works.

Run this script. Click "Turn Hotkey On", then Right Click to test the Rbutton hotkey that is not displayed in the Hotkey control.

Code: Select all

Gui, msctls_hotkey324:Add, Hotkey, +HwndCHID, RButton
Gui, msctls_hotkey324:Add, Edit, 
Gui, msctls_hotkey324:Add, Button, gB1, Turn Hotkey On
Gui, msctls_hotkey324:Add, Text,, Press ESC to exit script
Gui, msctls_hotkey324:Show
return
 
B1:
GuiControlGet, MyHotkey, msctls_hotkey324:, %CHID%
GuiControl,,edit1,%MyHotkey%
Hotkey, %MyHotkey% ,B2,On, UseErrorLevel
return

B2:
MsgBox Hotkey was pressed
return

Esc::
msctls_hotkey324GuiClose:
ExitApp
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], haomingchen1998, mikeyww, mmflume, OrangeCat, roysubs, scriptor2016, ShatterCoder and 96 guests