GetKeySC() incorrect values returned with "Escape" & "Esc" Topic is solved

Report problems with documented functionality
Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

GetKeySC() incorrect values returned with "Escape" & "Esc"

08 May 2017, 09:12

Seems to be a bug with the Scancode returned when passing the strings "Esc" and "Escape" as the key for GetKeySC() function. Should return 1 but different results are returned. When Esc is passed it returns 0 and when Escape is passed, it returns A (i.e. 10).

Code: Select all

key := ["Escape","Esc"] 
txt := ""
loop, % Round(key.MaxIndex())
{
	name := GetKeyName(key[A_Index])
	vk   := GetKeyVK(key[A_Index])
	sc   := GetKeySC(key[A_Index]) ;<- bugged, When "Escape", should be 1 not a, When "Esc" should be 1 not 0

	txt .= Format("Key: {}`nName: {},`tVK: {:X},`tSC: {:X}`n",key[A_Index], name, vk, sc)
	txt .= "`tvkName: " GetKeyName(Format("vk{:X}",vk)) ",`tscName: "GetKeyName(Format("sc{:X}",sc)) "`n`n"
}
txt .= "sc1: " GetKeyName("sc1")
MsgBox %txt%
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

13 May 2017, 01:53

It's due to a silly conflict with the "vkXXscYYY" syntax (due to the "sc" in "Esc"). I'll fix it in the next update.

ScrollLock and PrintScreen are also affected.
Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

13 May 2017, 04:09

Ok, makes sense, darn pesky keys with sc in their names. Thanks for the update.
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

01 Jul 2017, 03:28

1.1.26.00 - June 10, 2017
Fixed GetKeySC() with the key names Esc, Escape, ScrollLock and PrintScreen.
?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

01 Jul 2017, 04:00

Code: Select all

msgbox % Format("sc{:x}", GetKeySC("PrintScreen")) ; I get 154, but should be 137.
msgbox % GetKeyName("sc137")  ; I get NumpadMult , but should be PrintScreen.
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

01 Jul 2017, 05:00

Another error for all MM keys.

Code: Select all

msgbox % (sc := Format("sc{:x}", GetKeySC("Browser_Stop"))) "`n" GetKeyName(sc)
And why is it the same?

Code: Select all

msgbox % GetKeyName("sc37") "`n" GetKeyName("sc137")
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

01 Jul 2017, 05:28

GetKeySC for MM buttons do not have a leading "1".
For example, sc168 can be made a hotkey, but GetKeyName does not know this code.

Code: Select all

msgbox % (sc := Format("sc{:x}", GetKeySC("Browser_Stop"))) "`n" GetKeyName(sc)
msgbox % (sc := Format("sc{:x}", GetKeySC("Media_Prev"))) "`n" GetKeyName(sc)

sc168::MsgBox % GetKeyName(A_ThisHotkey)  ; Press - Browser_Stop
sc110::MsgBox % GetKeyName(A_ThisHotkey)  ; Press - Media_Prev
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

01 Jul 2017, 05:36

One more error found:

Code: Select all

msgbox % GetKeyName("sc45")  ; I get Numlock but should be Pause.
msgbox % Format("sc{:x}", GetKeySC("Pause")) ; Shows nothing
And one more thing that I do not understand.
I want to make hotkey NumpadHome using only VK and SC like this:

Code: Select all

NumpadHome::msgbox
1) If I make it like this I have got message when I push Home and NumpadHome buttons because they VK are the same:

Code: Select all

VK24::msgbox
2) If I make it like this I have got message when I push Numpad7 and NumpadHome buttons because they SC are the same:

Code: Select all

SC47::msgbox
3) I've thought that if I combine SC and VC then I will get message when I push NumpadHome only, but I was mistaken:

Code: Select all

VK24SC47::msgbox   ;   the same as vk24
SC47VK24::msgbox   ;   the same as sc47
I think it will be logical to make like this:

Code: Select all

VK24SC47::msgbox   ;   the same as NumpadHome
SC47VK24::msgbox   ;   the same as NumpadHome
And one more thing:
If I run this code:

Code: Select all

#Persistent
keyhistory
send {volume_up}{f5}
I will get wrong scancode - 030.
But if I physically press volume_up I will get right scancode = 130 in keyhistory.
Why key history shows wrong scancode when I emulate key?
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

02 Jul 2017, 01:56

VKnnSCnnn and SCnnnVKnn are not valid for hotkeys, hence not being on the key list page.

AutoHotkey's keyboard hook handles each key either by virtual keycode or by scancode, not both. Home (sc147) is always handled by scancode. NumpadHome/Numpad7 is handled by virtual keycode by default, but the presence of a hotkey which lacks a virtual keycode (i.e. sc47::) can override this. For example:

Code: Select all

$vk24::msgbox  ; Does not work unless sc47:: is removed.
#if false  ; The hotkey does not have to be active.
sc47::return  ; The presence of this hotkey changes the handling of key sc47.
NumpadHome:: is equivalent to $vk24::. Since the hook handles Home by scan code, it is never triggered by pressing Home.

vk24:: (without $) may be implemented with the "reg" method, in which case a hotkey is registered with the system by vk, and the system notifies us whenever any key with that vk is pressed.

This is by design.

msgbox % GetKeyName("sc45") ; I get Numlock but should be Pause.
This is because Pause is handled by vk - there is no entry for it in the name-to-scancode table - and when we ask MapVirtualKey which vk corresponds to sc45, it gives us the wrong answer.
msgbox % Format("sc{:x}", GetKeySC("Pause")) ; Shows nothing
This is because when we ask MapVirtualKey which sc corresponds to VK_PAUSE (vk13), it fails completely. This is probably related to the special nature of the Pause/Break key, which has been said to produce the scan code e1 1d 45. Although KeyHistory will tell us the scan code is 45, there's probably some truncation happening (possibly before the keyboard hook is called). If we pass the MAPVK_VK_TO_VSC_EX flag MapVirtualKey[Ex] (on Vista or later), it will tell us that the scan code is e1 1d, which is incomplete.

Note that when AutoHotkey shows scan code 1xx, the actual code coming from the keyboard hardware is e0 xx.
I will get wrong scancode - 030.
Can you guess what the problem is? It's MapVirtualKey again. Usually it doesn't matter, because most programs don't care about the scan code. For the rest, you can work around it by specifying both vk and sc.

These three issues can be fixed. There are already a bunch of exceptions for other keys which MapVirtualKey does not support.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

02 Jul 2017, 11:29

Thank You! Now I understand.
Not problem at all.
Just was interesting about this circumstance.
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

05 Jul 2017, 11:08

Can you add 18 multi media keys to exceptions?
The current situation introduces ambiguity and errors.
For example, you need to assign "Q", as VK can not be assigned, otherwise in some language layouts this code corresponds physically to another key.
And if you designate as SC, there may be a false alarm from another key:

Code: Select all

SendLevel, 1
Sleep, 1000
Send {Media_Prev}

sc10::MsgBox this scan code Q eng layout

Code: Select all

Browser_Back			vkA6     sc16A
Browser_Forward			vkA7     sc169
Browser_Refresh			vkA8     sc167
Browser_Stop			vkA9     sc168
Browser_Search			vkAA     sc165
Browser_Favorites		vkAB     sc166
Browser_Home			vkAC     sc132
Volume_Mute				vkAD     sc120
Volume_Down				vkAE     sc12E
Volume_Up				vkAF     sc130
Media_Next				vkB0     sc119
Media_Prev				vkB1     sc110
Media_Stop				vkB2     sc124
Media_Play_Pause		vkB3     sc122
Launch_Mail				vkB4     sc16C
Launch_Media			vkB5     sc16D
Launch_App1				vkB6     sc16B
Launch_App2				vkB7     sc121
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

05 Jul 2017, 22:51

I could, but I'm not going to.

The scan codes for multimedia keys are not documented by Microsoft as far as I can tell, and historically differ between keyboard manufacturers. I suspect that all keyboards which work out of the box with modern versions of Windows use the same scan codes, but that isn't certain. Specifically handling these scan codes means relying on information gained through experimentation, which may not be reliable.

The next AutoHotkey release will use the MAPVK_VK_TO_VSC_EX flag (which I mentioned in passing) with MapVirtualKey. Although it is poorly documented and evidently still unreliable for some keys, it is more likely to work if the mapping of multimedia keys is ever dependent on keyboard layout, and it will not increase code size as much as adding a case for each multimedia key. The drawback is that Windows 2000 and XP will still use the wrong scan codes.

Pause, LWin, RWin and (on Vista+) CtrlBreak will also be fixed.
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

06 Jul 2017, 03:56

We tried on several computers, all like here [url] http://www.computer-engineering.org/ps2 ... odes1.html [/ url] without "1".

Code: Select all

c := 0
Loop % 0xB7 - 0xA6 + 1
{
	++c, vk := Format("vk{:X}", A_Index + 0xA6 - 1)
	Str .= GetKeyName(vk) "`t" vk "`t" (sc := Format("sc{:X}", GetKeySC(vk))) "`t" GetKeyName(sc) "`n"
}
MsgBox, , % c, % Clipboard := Str
In the last column, duplicates with which a conflict is now possible.
If you are not sure, then you can simply not get the scan code for virtual codes from 0xA6 to 0xB7, it will be a simple condition. After all, scan codes of these keys are not needed, and their absence will not lead to coincidence with duplicates.
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

06 Jul 2017, 04:25

Is it still possible to add a flag to GetKeySC and GetKeyVK, to return not as an example 0xFF or 255, but immediately in the format AHK, as - vkFF or scFF.
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

15 Jul 2017, 21:16

v1.1.26.01 includes the fixes I mentioned.
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

16 Jul 2017, 02:39

Thanks, there is it:

Code: Select all

msgbox % Format("sc{:x}", GetKeySC("PrintScreen")) ; I get 154, but should be 137.
msgbox % GetKeyName("sc137")  ; I get NumpadMult , but should be PrintScreen.
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

17 Jul 2017, 04:23

The PrintScreen key is sometimes labeled "PrtScn | SysReq", with PrintScreen being E0 37 (sc137) and System Request being 54 (sc054). It's explained here (though I think some of the details don't apply to modern systems):
The Print Screen key with Set 1 scan code shown as 37/54+ sends E0 37, [...]. When the Alt key is held down, it behaves as a System Request key, and sends scan code 54 [...]
Source: Scan Codes Demystified
MapVirtualKey returns 0x54, therefore I think it is fair to say that GetKeySC returning 0x54 would be correct.

But it doesn't. The internal function used by GetKeySC (and Send, etc.) adds 0x100 to the value, making it 0x154. That seems to indicate the original developer (Chris) was expecting MapVirtualKey to return 0x37, or maybe that it actually did in some versions of Windows or on some systems.

The problem with AutoHotkey explicitly mapping anything to a scan code is that it does not respect the keyboard layout. A keyboard layout can potentially map any scan code to any virtual keycode, meaning that sc137 could mean anything in a custom keyboard layout. I think it can also map a scan code to different virtual keycodes depending on modifier keys, though obviously GetKeySC/GetKeyVK/MapVirtualKey can't account for that. Currently AutoHotkey explicitly maps the modifier and Numpad keys, because MapVirtualKey does not support them reliably. That being the case (and I just added an exception for Pause), I suppose it would be reasonable to add an exception for PrintScreen.
msgbox % GetKeyName("sc137") ; I get NumpadMult , but should be PrintScreen.
I do not. I get PrintScreen.

Interesting historical note, though:
[...] the original IBM PC keyboard had the Print Screen function on the same key as the asterisk, [...]
Source: Scan Codes Demystified
This is why PrintScreen and NumpadMult both have scan code 0x37 (but PrintScreen has the extended-key flag).

N.B. If you are using Windows 2000 or XP, you are expected to get the wrong result.
Fixed SC → VK translation for multimedia keys, CtrlBreak, Pause, LWin and RWin, with the exception that multimedia keys and CtrlBreak are still translated incorrectly on Windows 2000 and XP.
Source: v1.1.26.01
The multimedia keys, CtrlBreak and apparently PrintScreen are supported by way of passing the full extended scan code to MapVirtualKey. Only Windows Vista and later support this.
lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"  Topic is solved

27 Dec 2017, 20:57

The issues in serzh82saratov's last post were fixed in v1.1.27.00.
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: GetKeySC() incorrect values returned with "Escape" & "Esc"

30 Dec 2017, 16:13

1.1.27.00

Code: Select all

MsgBox % GetKeyName("vk9scF")  ;	Empty
MsgBox % GetKeyName("scF")
MsgBox % GetKeyName("vk9")
MsgBox % GetKeyName("vk9sc0F")

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 49 guests