Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

GetKeyVK and GetKeySC bug?



  • Please log in to reply
9 replies to this topic
AutomateEverything
  • Members
  • 60 posts
  • Last active: Aug 25 2014 06:02 AM
  • Joined: 03 Dec 2012

Hi,

GetKeyVK and GetKeySC return wrong results:

vkEsc := GetKeyVK("Esc")
scEsc := GetKeySC("Esc")
MsgBox % vkEsc . ": " . GetKeyName("vk" . vkEsc) ; "27: NumpadRight"
MsgBox % scEsc . ": " . GetKeyName("sc" . scEsc) ; "0: "
MsgBox % GetKeyName("vk1B") ; "Escape"
MsgBox % GetKeyName("sc001") ; "Escape"

vkH := GetKeyVK("H")
scH := GetKeySC("H")
MsgBox % vkH . ": " . GetKeyName("vk" . vkH) ; "72: F3"
MsgBox % scH . ": " . GetKeyName("sc" . scH) ; "35: -"
MsgBox % GetKeyName("vk48") ; "H"
MsgBox % GetKeyName("sc023") ; "H"

Fortunately, I can look up the right code of a key in the key history. Anyway, is this a bug?



RHCP
  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006
✓  Best Answer
SetFormat, IntegerFast, hex
vkEsc := GetKeyVK("Esc")
scEsc := GetKeySC("Esc")
MsgBox % vkEsc . ": " . GetKeyName("vk" . vkEsc) ;
MsgBox % scEsc . ": " . GetKeyName("sc" . scEsc) ; "0: "

 GetKeyVK() and GetKeyVK() were returning the correct VKs and SCs, but they were being passed to the functions as decimal numbers. When passing the key code via VK/SC, you must ensure they are in hex.



AutomateEverything
  • Members
  • 60 posts
  • Last active: Aug 25 2014 06:02 AM
  • Joined: 03 Dec 2012

Thanks, that explains a lot :)



stealzy
  • Members
  • 4 posts
  • Last active: Apr 30 2017 12:22 PM
  • Joined: 01 Nov 2015
Useful function for get vk code in ahk format:
; Example: GetKeyVKahk("v") return vk56
GetKeyVKahk(ButtonLetter) {
	format:=A_FormatInteger
	SetFormat, IntegerFast, hex
	VKHex:=GetKeyVK(Button)
	StringReplace, VKcodeAHK, VKHex, 0x, vk
	SetFormat, IntegerFast, %format%
	Return VKcodeAHK
}


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Shorter version.

GetKeyVKx(key) {
	return format("vk{:02x}", GetKeyVK(key))
}

I'd normally just use format() directly.



stealzy
  • Members
  • 4 posts
  • Last active: Apr 30 2017 12:22 PM
  • Joined: 01 Nov 2015
Lexikos, please, tell me why AutoHotKey don't use vk-code by default in this cases:
^c::
Send ^v
If you use not english keyboard layout, then this hotkey don't work.
Show me that freak, who use different hotkey in different layout, I want to kill him!
I even can't use GetKeyVKx directly (pseudocode):
^%GetKeyVKx("c")%::
Send ^{%GetKeyVKx("v")%}


Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
Hotkey, % "~^" GetKeyVKx("c"), Copy
return

Copy:
Send % "^{" GetKeyVKx("v") "}"
return

GetKeyVKx(key) {
	return format("vk{:02x}", GetKeyVK(key))
}
this works fine for me

You can't use variables or function calls in an hotkey label

Only by using the hotkey command can you define hotkeys like that...
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Lexikos, please, tell me why AutoHotKey don't use vk-code by default in this cases:

This seem to be irrelevant to the original topic, but anyway:

I don't understand your question. "v" is not a keycode. What do you expect AutoHotkey to do?

Why don't you use keycodes by default? Tell AutoHotkey to use a keycode, and it will use a keycode.

If you specify a character, AutoHotkey converts it to a keycode. Characters have different meanings on different keyboard layouts, so obviously ^v won't work if "v" doesn't exist on the current layout. Or maybe it will work (as far as the machine is concerned) but it will send a different key than the one you were expecting.

stealzy
  • Members
  • 4 posts
  • Last active: Apr 30 2017 12:22 PM
  • Joined: 01 Nov 2015
Thanks for answers.
Of course, "v" is a letter of button.
In all programs there I can set keyboard shortcuts - browsers, text editors, etc. -
shortcuts with modifier, like "Ctrl+V", "Alt+Win+R" found on vk-code, not current layout depend character, although there are characters shown in set menu.
"Why don't you use keycodes by default?" - this is awkward. Compare:
!vk43::Send % "^{" GetKeyVKx("v") "}" ; necessary comment that character means vk43 key code

GetKeyVKx(key) {
	return format("vk{:02x}", GetKeyVK(key))
}

; VS ================================================================================================================
; Autohotkey, I hereby order to use the vk keycode for combination with modifier, except "Shift" only(#f, +!^q, etc.)

#UseVkCodeWithModifier
!c::^v
If I may ask - why use a character code with modifier? Anyone use this?

Of course, this is not big problem, but when I started to use AHK, GetKeyVK yet doesn't exist, so I used another progam for get vk/sc code.
Just, it's a shame that for simple remapping keys, you need get vk code.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
AutoHotkey already uses the VK code, if there is one corresponding to the character you specified. As I said,

If you specify a character, AutoHotkey converts it to a keycode.


The difference between ^v and % "^{" GetKeyVKx("v") "}" is that you are explicitly converting it according to the script's current keyboard layout, which may well get the wrong result. Normally it's the system's default layout, but it can be changed if your script has a GUI (or you open the script's main window via the tray icon, ListLines or similar).

You seem to want AutoHotkey to convert the character according to an arbitrary keyboard layout -- whichever one gives the result you want. But there's no way AutoHotkey can know what you want unless you specify it. You can specify that you want that virtual key (not character) by writing the virtual keycode in the first place.

The keycode will not change unless you change keyboard layouts. You can just hardcode it in your script, rather than calling a function.