int RememberMouse(int code, IntPtr wParam, IntPtr lParam) {//只能在鼠标活动触发,静止不会触发 if (code >= 0) { HookRelated.MSLLHOOKSTRUCT mouseHookStruct = (HookRelated.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(HookRelated.MSLLHOOKSTRUCT)); //MouseButtons mb = MouseButtons.None; //int delta = 0; int wheel = 0; switch ((int)wParam) { //case (int)HookRelated.MouseConduct.WM_LBUTTONDOWN: // mb = MouseButtons.Left; // break; //case (int)HookRelated.MouseConduct.WM_MBUTTONDOWN: // mb = MouseButtons.Middle; // break; //case (int)HookRelated.MouseConduct.WM_RBUTTONDOWN: // mb = MouseButtons.Right; // break; case (int)HookRelated.MouseConduct.WM_MOUSEWHEEL: //此处只能获得滚轮方向,无法获得滚轮数据。可重写Form的OnMouseWheel事件来获得数据 //delta = mouseHookStruct.MouseData / 65536; //delta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); if (mouseHookStruct.MouseData > 0) { ++wheel; } else { --wheel; } break; } MouseIputEvent(new Structure.Mouse(mouseHookStruct.Point, (int)wParam, wheel)); } return(HookRelated.CallNextHookEx(m_Code, code, wParam, lParam)); }
int RememberKeyboard(int code, IntPtr wParam, IntPtr lParam) { if (code >= 0) { HookRelated.KeyboardHookStruct keyboardHookStruct = (HookRelated.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(HookRelated.KeyboardHookStruct)); Keys key = (Keys)keyboardHookStruct.vkCode; Structure.Keyboard keyboardData = new Structure.Keyboard(); byte[] ch = new byte[256]; int a = Marshal.ReadInt32(lParam); Marshal.Copy(lParam, ch, 0, 255); switch ((int)wParam) { case (int)HookRelated.KeyboardConduct.WM_SYSKEYDOWN: //只有Alt在此 if (key == Keys.LMenu || key == Keys.RMenu) { m_IsDownAlt = true; } keyboardData = new Structure.Keyboard(key, m_IsDownCtrl, m_IsDownAlt, m_IsDownShift, m_IsDownCapsLock); break; case (int)HookRelated.KeyboardConduct.WM_SYSKEYUP: //Alt+?按下,当?释放触发 break; case (int)HookRelated.KeyboardConduct.WM_KEYDOWN: if (key == Keys.LControlKey || key == Keys.RControlKey) { m_IsDownCtrl = true; } m_IsDownCapsLock = HookRelated.GetKeyState((int)HookRelated.KeyboardASCII.CapeLock) == 1 ? true : false; m_IsDownShift = HookRelated.GetKeyState((int)HookRelated.KeyboardASCII.Shift) == 1 ? true : false; byte[] keyState = new byte[256]; int n = HookRelated.GetKeyboardState(keyState); byte[] inBuffer = new byte[2]; if (HookRelated.ToAscii(keyboardHookStruct.vkCode, keyboardHookStruct.scanCode, keyState, inBuffer, keyboardHookStruct.flags) == 1) { //KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]); keyboardData = new Structure.Keyboard(key, m_IsDownCtrl, m_IsDownAlt, m_IsDownShift, m_IsDownCapsLock); } //else //{ // if(m_IsDownCapsLock) // keyboardData = new Structure.Keyboard(Keys.CapsLock, m_IsDownCtrl, m_IsDownAlt, m_IsDownShift, m_IsDownCapsLock); // else if(m_IsDownCtrl) // keyboardData = new Structure.Keyboard(Keys.Control, m_IsDownCtrl, m_IsDownAlt, m_IsDownShift, m_IsDownCapsLock); // else if (m_IsDownShift) // keyboardData = new Structure.Keyboard(Keys.Shift, m_IsDownCtrl, m_IsDownAlt, m_IsDownShift, m_IsDownCapsLock); //} break; case (int)HookRelated.KeyboardConduct.WM_KEYUP: if (key == Keys.LControlKey || key == Keys.RControlKey) { m_IsDownCtrl = false; } if (key == Keys.LMenu || key == Keys.RMenu) { m_IsDownAlt = false; } break; } KeyboardIputEvent(keyboardData); } return(HookRelated.CallNextHookEx(m_Code, code, wParam, lParam)); }