C++ KeyboardProc() 的用法疑问
嘛我也真是很久没逛论坛了,回来一下吧就是想问一下怎么把键盘所按的按键的数值调出来用(int)看了很多例子,最接近的就是这个了
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
HHOOK g_hKeyboard;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
PMSLLHOOKSTRUCT pmll = (PMSLLHOOKSTRUCT) lParam;
printf("msg: %lu, x:%ld, y:%ld\n", wParam, pmll->pt.x, pmll->pt.y);
}
return CallNextHookEx(g_hKeyboard, nCode, wParam, lParam);
}
int main(void)
{
MSG msg;
g_hKeyboard = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0 );
if (!g_hKeyboard) printf("Hook error: %d\n", GetLastError());
while ( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(g_hKeyboard);
return (int) msg.wParam;
}
但是就是不知道怎么把key的数值换成int,想请各位高手提点一下
附件:msdn 上对于这些数值的定义
LRESULT CALLBACK KeyboardProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
);
wParam
Specifies the virtual-key code of the key that generated the keystroke message.
lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. This parameter can be one or more of the following values
直接把wParam转换成long型,得到的就是虚拟键码
之后可以通过查表和检查lParam的FLAG状态找到对应的ASCII码
键码表:http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731.aspx
lParam的信息:http://msdn.microsoft.com/en-us/library/windows/desktop/ms646267.aspx
另外还可以通过TranslateMessage来辅助
页:
[1]