代码大概就是,导入了自己写的会hook所有线程的按键,并把按键信息以Message的形式发来这个窗体dll,按button1就是开始hook,button2停止hook,button3按照一定的格式把收到的Message的WParam属性的值显示出来。
部分代码如下
const int WM_CHAR = 0x102;
const int WM_UNICHAR = 0X109;
const int VK_SHIFT = 16;
const int VK_CAPITAI = 20;
char[] ascii=new char[26]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
[DllImport(@"User32.dll")]
protected static extern short GetKeyState(int nVirtualKey);
protected override void WndProc(ref Message m)
{
bool IsRecord = true;
int a = (int)m.WParam;
if (a >= 65 && a <= 90)
{
Console.WriteLine("the m.Msg is : " + m.Msg + " \n");
if (GetKeyState(VK_SHIFT) < 0 || (GetKeyState(VK_CAPITAI) != 0 ? true : false))
{
char c = ascii[a - 65];
char d = (char)(c - 32);
str = str + "the button you press is: " + d + " \n";
}
else str = str + "the button you press is: " + ascii[a - 65] + " \n";
}
base.WndProc(ref m);
}
上图这是后台输出,代码中有,但是收到的信息的id为0和3,即是WM_NULL和WM_MOVE,却不是收到WM_KEYDOWN和WM_KEYUP,但是此信息的WParam属性又是准确对应了键码,这是怎么回事?
keypreview属性设置了么?onkeypress重写了么?