Get names / VKs + SCs of all keys

Get help with using AutoHotkey and its commands and hotkeys
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Get names / VKs + SCs of all keys

10 Mar 2016, 15:28

I have a "bindings" system that lets people pick which key they want to use for a hotkey by pressing it.
I do this by binding a hotkey to all keys on the keyboard.

It does this by getting the key name for every VK, however it appears that some keys cannot be described by VK alone, and need a VK + SC combination.
One I know of for sure is the arrow keys.

However, even then there is something I don't quite get - for example "Left" seems to be both VK0x1 / SC0x14b and VK0x25 / SC0x14b

When a user chooses a key, I currently store the VK, but it seems that I will probably have to expand this to VK + SC combination.

Basically I need code that can generate a complete list of key names, given VKs and / or SCs.

Can anyone shed any light on this issue?

Code: Select all

keys := {}

Gui, Add, ListView, w250 h300, Key|VK|SC
LV_ModifyCol(1, 100)
LV_ModifyCol(2, 60)
LV_ModifyCol(3, 60)
Loop 256 {
	; Get the key name
	i := A_Index
	vk := Format("{:x}", i)
	Loop 512 {
		sc := Format("{:x}", A_Index)
		n := GetKeyName("vk" vk " sc" sc)
		if (n = "" || ObjHasKey(keys, n))
			continue
		LV_Add(,n, vk, sc)
		keys[n] := 1
	}
}

Gui Show, x0 y0
return

GuiClose:
	ExitApp

wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Get names / VKs + SCs of all keys

10 Mar 2016, 20:26

I don't know about getting all the keys without a button press, but if you do have a button press you can do something like this:

Code: Select all

#InstallKeybdHook
^k::
KeyHistory
WinWaitActive, % title:="AutoHotkey v" A_AhkVersion
controlGetText, var, Edit1, % title
obj:=[] , pos:=1
while(pos:=RegExMatch(var, "`am)^(?<VK>[0-9A-Z]{2})\s\s(?<SC>[0-9A-Z]{3})\D+\d+\.?\d+\s+(?<KEY>\S+)", out, pos+1)) {
	obj.push({"Key":outKey, "SC":outSC, "VK":outVK})
}
for i in obj
	MsgBox, % "Key is " obj[i].Key " VK is " obj[i].VK " SC is " obj[i].SC
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Get names / VKs + SCs of all keys

11 Mar 2016, 01:17

... it appears that some keys cannot be described by VK alone, and need a VK + SC combination.
With the possible exception of single-character keys (which are defined by the keyboard layout), every key is identified either by VK alone or by SC alone, with priority on SC because it is more specific. The key names are listed in g_key_to_vk or g_key_to_sc.
However, even then there is something I don't quite get - for example "Left" seems to be both VK0x1 / SC0x14b and VK0x25 / SC0x14b
If you specify a virtual keycode and an unrelated scancode, you may get inconsistent results...

vk01 is LButton.

If you pass each scancode to GetKeyName, it will automatically convert any scancode that isn't in g_key_to_sc to a virtual keycode. However, this might miss some virtual keycodes which don't exist on the current keyboard layout.
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Re: Get names / VKs + SCs of all keys

11 Mar 2016, 07:07

Thanks Lex, but I am not quite getting it:

, {_T("Down"), SC_DOWN}

So what, I need to find out what the value of SC_DOWN is?
I did a bit of a google, the only thing I came across was this:
http://lampiweb.com/help/freebasic/GfxScancodes.html

Which says: SC_DOWN &h50

However:

MsgBox % GetKeyName("sc50") = "NumpadDown"

Never mind, used Visual Studio. SC_DOWN = 0x150, which works. Thanks!
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Re: Get names / VKs + SCs of all keys

11 Mar 2016, 07:20

So this code seems to pull a list of all keys and their scancodes.

Interestingly, some keys seem to appear more than once. Change unique_mode to 0 and you get 191 keys, unique_mode 1 and you get 116.

Does it matter which one you use?

Code: Select all

#SingleInstance force

keys := {}
unique_mode := 0
 
Gui, Add, ListView, w200 h300, Key|SC
LV_ModifyCol(1, 100)
LV_ModifyCol(2, 60)
LV_ModifyCol(3, 60)
count := 0
Loop 350 {
	; Get the key name
	sc := Format("{:x}", A_Index)
	n := GetKeyName("sc" sc)
	if (n = "" || (unique_mode ? ObjHasKey(keys, n) : 0))
		continue
	LV_Add(,n, sc)
	keys[n] := 1
	count++
}
Gui, Add, Text, xm, % "Total: " count
Gui Show, x0 y0
return
 
GuiClose:
	ExitApp
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Get names / VKs + SCs of all keys

11 Mar 2016, 17:50

The "duplicates" are the same scancode with and without the extended-key flag, such as sc01e and sc11e. The system might simply ignore this flag when it isn't needed to distinguish between two keys. All of the keys in the g_key_to_sc array are there because they have counterparts with the same virtual keycode and same scancode minus the extended flag, like Enter and NumpadEnter.
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Re: Get names / VKs + SCs of all keys

08 May 2016, 06:39

Well, I think I may as well just accept that GetKeyName is returning undesirable (for me, at least) results for certain values and patch in my own tweaks to it's table (decimal values): {{33: "PgUp", 34: "PgDn", 35: "End", 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 45: "Insert", 46: "Delete"}.

each key that I replace also has an alternate code, and all the replacement numbers I use are basically what MSDN says that the VK of that key is, so it doesn't seem like too much of a bodge to me.

Can anyone find any keys on their keyboard not detected by this code?

Only Numpad Enter for me. Enter (VK_RETURN) is 0xD, and 0xE-0xF are "Undefined" according to MSDN, so I could maybe just use 0xE for NumpadEnter.

Code: Select all

keys := {}
unique_mode := 0
 
Gui, Add, ListView, w200 h300, Key|SC
LV_ModifyCol(1, 100)
LV_ModifyCol(2, 60)
LV_ModifyCol(3, 60)
replacements := {33: "PgUp", 34: "PgDn", 35: "End", 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 45: "Insert", 46: "Delete"}
;replacements := {}
count := 0
Loop 350 {
	; Get the key name
	code := Format("{:x}", A_Index)
	if(ObjHasKey(replacements, A_Index)){
		n := replacements[A_Index]
	} else {
		n := GetKeyName("vk" code)
	}
	if (n = "" || (unique_mode ? ObjHasKey(keys, n) : 0))
		continue
	LV_Add(,n, code)
	keys[n] := 1
	fn := Func("action").Bind(A_Index, code, n, 1)
	hotkey, % "~" n, % fn
	fn := Func("action").Bind(A_Index, code, n, 0)
	hotkey, % "~" n " up", % fn
	count++
}
Gui, Add, Text, xm, % "Total: " count
Gui Show, x0 y0
return

action(dec, hex, n, event){
	if (event){
		ToolTip, % n " (" dec "/ Ox" hex ")"
	} else {
		ToolTip
	}
}

GuiClose:
	ExitApp
 
nismo
Posts: 19
Joined: 22 Dec 2016, 13:57

Re: Get names / VKs + SCs of all keys

02 May 2017, 18:38

I know it's an bit old thread but it's a very nice code, it detects all my keys except for Ralt and NumpadEnter, it can be fixed?

I would like an edit box with a button, then click the button a spam message ask for key, and update the edit box with the key, it's possible?, I don't know how to do it, script running all time and doesn't stop.

I found some examples out there but doesn't work well, doesn't work, doesn't detect L/R keys or doesn't detect some keys or mouse (with wheel up and down)...

This is the more simple code and works very well.

Thanks.
User avatar
evilC
Posts: 4765
Joined: 27 Feb 2014, 12:30

Re: Get names / VKs + SCs of all keys

03 May 2017, 08:51

Changing the line replacements := {14: "NumpadEnter", 33: "PgUp", 34: "PgDn", 35: "End", 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 45: "Insert", 46: "Delete"} to replacements := {33: "PgUp", 34: "PgDn", 35: "End", 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 45: "Insert", 46: "Delete"} makes it recognize NumpadEnter.
I think RALT is CTRL+ALT - ie it is not a single key combo, and so I am not sure it is supported by this technique
nismo
Posts: 19
Joined: 22 Dec 2016, 13:57

Re: Get names / VKs + SCs of all keys

03 May 2017, 09:00

Thank you very much, what about the edit box and button? I tried by myself but script working all time and doesn't stop when key detected, also it's capturing keys since I run the script, doesn't wait for the Gui... I'm new with ahk...

Anyway thanks for taking your time to help me.

Regards.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], BushMange, CEA6597, Google [Bot], VACO BenQ, w0z and 182 guests