Hotkey by VK + SC

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
RolandoXIX
Posts: 1
Joined: 28 Sep 2022, 09:53

Hotkey by VK + SC

Post by RolandoXIX » 28 Sep 2022, 10:02

Hello...

I'm trying to create a media_pause mapping, but I only want it to be done when it comes from my headphones.
How can I capture the media_pause button and filter from action by SC?

I already tried something like this but dosent work....

Code: Select all

vkB1::
If (GetKeyState("vkB1sc019")) {
    return
}
and this...

Code: Select all

vkB1sc019::return
Hope someone can help me.

(Sorry about my english)

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Hotkey by VK + SC

Post by mikeyww » 29 Sep 2022, 22:12

Welcome to this AutoHotkey forum!

I'm not sure, but you may want to try GetKeyState with mode P as noted in the documentation. You can display the value of the GetKeyState function to understand what it is.

lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: Hotkey by VK + SC

Post by lexikos » 30 Sep 2022, 19:29

Known limitation: This command cannot differentiate between two keys which share the same virtual key code, such as Left and NumpadLeft.
Source: GetKeyState() / GetKeyState - Syntax & Usage | AutoHotkey
Warning: Only Send, GetKeyName(), GetKeyVK(), GetKeySC() and #MenuMaskKey support combining VKnn and SCnnn.
Source: List of Keys (Keyboard, Mouse and Joystick) | AutoHotkey

AutoHotkey does not support restricting a hotkey to a specific combination of VK and SC.


vkB1 is defined in the Windows SDK as
#define VK_MEDIA_PREV_TRACK 0xB1
i.e. Media_Prev, not Media_Play_Pause.

There is no (standard) Media_Pause button.

I'm certain that vkB1 wouldn't normally have the effect of pausing media, regardless of which scancode it is paired with. So putting that side, or assuming you meant Media_Prev...

For media keys, sc019 (actually sc119, which is just scancode 0x19 with the "extended key" flag) is normally paired with Media_Next (vkB0), not Media_Prev, which would be paired with sc110.


In any case, InputHook provides a way to detect a keypress and determine both its SC and VK:

Code: Select all

ih := InputHook("L0 V")
ih.KeyOpt("{vkB1}", "N")
ih.OnKeyDown := Func("ih_KeyDown")
ih.Start()

ih_KeyDown(ih, vk, sc) {
    MsgBox % Format("sc{:03x}", sc)
}
If you want to detect more keys, just add them to the KeyOpt call.

To suppress the key, change "N" to "NS". You cannot conditionally suppress a key, but you can suppress it and then send the key again. In that case, you would need to change "L0 V" to "L0 V I" so that the sent key is ignored. Omitting the "I" option allows it to be triggered by Send, which I used for testing.

Post Reply

Return to “Ask for Help (v1)”