ALT KEY 的虚拟键码
Virtual key code of ALT KEY
根据 MSDN,我正在制作钩子并捕捉键盘虚拟键码, these are 存在的键码。
每个关键代码都可以正常工作,因为我将在下面给出我的代码的示例。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
HHOOK altKey;
KBDLLHOOKSTRUCT kbdHK;
MSG message;
LRESULT CALLBACK kbdProc(int nCode, WPARAM wPar, LPARAM lPar);
int main(int argc, char *argv[]) {
altKey = SetWindowsHookEx(WH_KEYBOARD_LL, kbdProc, NULL, 0);
while(GetMessage(&message, NULL, 0, 0) > 0){
TranslateMessage(&message);
DispatchMessage(&message);
}
return 0;
}
LRESULT CALLBACK kbdProc(int nCode, WPARAM wPar, LPARAM lPar){
if(nCode >= 0){
if(wPar == 256){
kbdHK = *(KBDLLHOOKSTRUCT *)lPar;
if(kbdHK.vkCode == 0x20){
printf("spacebar pressed!!\n");
}
}
}
return CallNextHookEx(NULL, nCode, wPar, lPar);
}
但是当我用 0x12
替换虚拟键码时,根据 MSDN,这是 ALT KEY,我无法得到结果。可能是什么问题?
编辑:
当我使用这行代码时,我可以得到每个键的 scanCode
但不是 alt 键,这很有趣。
printf("%d", kbd.scanCode);
我建议挂钩 the WM_SYSKEYDOWN
messages,并注意这些值对应于更常具有标识符的常量(在本例中为 VK_MENU
)。毕竟,作为程序员,我们很早就被教导 不要使用幻数 !
WM_SYSKEYDOWN message
Posted to the window with the keyboard focus when the user presses the F10 key (which activates the menu bar) or holds down the ALT key and then presses another key.
正如评论中指出的,另一种选择是使用 GetAsyncKeyState
。您对无法使用 vkCode
的担忧似乎很奇怪,因为 GetAsyncKeyState
s 参数 是 虚拟关键代码,毕竟,所以你要使用的代码应该是这样的:
short result = GetAsyncKeyState(VK_MENU);
if (!result) {
// no alt keystrokes since last call
}
这是您能够区分左 alt (VK_LMENU
) 和右 alt (VK_RMENU
) 键的唯一方法,但 不是 window-循环方式。现在你两者都有了。
根据 MSDN,我正在制作钩子并捕捉键盘虚拟键码, these are 存在的键码。
每个关键代码都可以正常工作,因为我将在下面给出我的代码的示例。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
HHOOK altKey;
KBDLLHOOKSTRUCT kbdHK;
MSG message;
LRESULT CALLBACK kbdProc(int nCode, WPARAM wPar, LPARAM lPar);
int main(int argc, char *argv[]) {
altKey = SetWindowsHookEx(WH_KEYBOARD_LL, kbdProc, NULL, 0);
while(GetMessage(&message, NULL, 0, 0) > 0){
TranslateMessage(&message);
DispatchMessage(&message);
}
return 0;
}
LRESULT CALLBACK kbdProc(int nCode, WPARAM wPar, LPARAM lPar){
if(nCode >= 0){
if(wPar == 256){
kbdHK = *(KBDLLHOOKSTRUCT *)lPar;
if(kbdHK.vkCode == 0x20){
printf("spacebar pressed!!\n");
}
}
}
return CallNextHookEx(NULL, nCode, wPar, lPar);
}
但是当我用 0x12
替换虚拟键码时,根据 MSDN,这是 ALT KEY,我无法得到结果。可能是什么问题?
编辑:
当我使用这行代码时,我可以得到每个键的 scanCode
但不是 alt 键,这很有趣。
printf("%d", kbd.scanCode);
我建议挂钩 the WM_SYSKEYDOWN
messages,并注意这些值对应于更常具有标识符的常量(在本例中为 VK_MENU
)。毕竟,作为程序员,我们很早就被教导 不要使用幻数 !
WM_SYSKEYDOWN message
Posted to the window with the keyboard focus when the user presses the F10 key (which activates the menu bar) or holds down the ALT key and then presses another key.
正如评论中指出的,另一种选择是使用 GetAsyncKeyState
。您对无法使用 vkCode
的担忧似乎很奇怪,因为 GetAsyncKeyState
s 参数 是 虚拟关键代码,毕竟,所以你要使用的代码应该是这样的:
short result = GetAsyncKeyState(VK_MENU);
if (!result) {
// no alt keystrokes since last call
}
这是您能够区分左 alt (VK_LMENU
) 和右 alt (VK_RMENU
) 键的唯一方法,但 不是 window-循环方式。现在你两者都有了。