public static extern bool GetMessage(out Message lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
public static extern bool TranslateMessage(ref Message m);
private bool WndPrc(ref Message message) { try { switch (message.Id) { case NativeConstants.WM_DEADCHAR: { break; } case NativeConstants.WM_KEYDOWN: case NativeConstants.WM_KEYUP: case NativeConstants.WM_CHAR: { WmKeyEvent(ref message); break; } case NativeConstants.WM_SYSKEYDOWN: case NativeConstants.WM_SYSKEYUP: case NativeConstants.WM_SYSCHAR: { NativeMethods.TranslateMessage(ref message); WmKeyEvent(ref message); return WP_NOPASSTHROUGH; } case NativeConstants.WM_SYSCOMMAND: { break; } case NativeConstants.WM_MOUSEMOVE: { WmMouseMove(ref message); break; } case NativeConstants.WM_LBUTTONDOWN: { WmMouseDown(ref message, MouseButtonInternal.Left, 1); break; } case NativeConstants.WM_RBUTTONDOWN: { WmMouseDown(ref message, MouseButtonInternal.Right, 1); break; } case NativeConstants.WM_MBUTTONDOWN: { WmMouseDown(ref message, MouseButtonInternal.Middle, 1); break; } case NativeConstants.WM_LBUTTONUP: { WmMouseUp(ref message, MouseButtonInternal.Left, 1); return WP_PASSTHROUGH; } case NativeConstants.WM_RBUTTONUP: { WmMouseUp(ref message, MouseButtonInternal.Right, 1); return WP_PASSTHROUGH; } case NativeConstants.WM_MBUTTONUP: { WmMouseUp(ref message, MouseButtonInternal.Middle, 1); return WP_PASSTHROUGH; } case NativeConstants.WM_LBUTTONDBLCLK: { WmMouseDown(ref message, MouseButtonInternal.Left, 2); return WP_PASSTHROUGH; } case NativeConstants.WM_RBUTTONDBLCLK: { WmMouseDown(ref message, MouseButtonInternal.Right, 2); return WP_PASSTHROUGH; } case NativeConstants.WM_MBUTTONDBLCLK: { WmMouseDown(ref message, MouseButtonInternal.Middle, 2); return WP_PASSTHROUGH; } case NativeConstants.WM_MOUSEWHEEL: { WmMouseWheel(ref message); return WP_PASSTHROUGH; } case NativeConstants.WM_XBUTTONDOWN: { WmMouseDown(ref message, GetXButton(Message.HighWord(message.WParam)), 1); return WP_PASSTHROUGH; } case NativeConstants.WM_XBUTTONUP: { WmMouseUp(ref message, GetXButton(Message.HighWord(message.WParam)), 1); return WP_PASSTHROUGH; } case NativeConstants.WM_XBUTTONDBLCLK: { WmMouseDown(ref message, GetXButton(Message.HighWord(message.WParam)), 2); return WP_PASSTHROUGH; } } } catch { //TODO: log...crash...what? } return WP_PASSTHROUGH; }
/// <summary> /// Reads the supplied message and executes any Mouse Wheel events required. /// </summary> /// <param name="message">The Message to parse</param> private void WmMouseWheel(ref Message message) { OnMouseWheel(new EventArgsMouse( translateWParamIntoMouseButtons(Message.SignedLowWord(message.WParam)), Message.SignedHighWord(message.WParam), Message.SignedLowWord(message.LParam), Message.SignedHighWord(message.LParam), (int)(long)message.WParam, getModifierKeys() )); }
/// <summary> /// Reads the supplied message and executes any Mouse Up events required. /// </summary> /// <param name="message">The Message to parse</param> /// <param name="button">The Mouse Button the Message is for</param> /// <param name="clicks">The number of clicks for the Message</param> private void WmMouseUp(ref Message message, MouseButtonInternal button, int clicks) { // HandleMouseBindings(); OnMouseUp(new EventArgsMouse( button, clicks, Message.SignedLowWord(message.LParam), Message.SignedHighWord(message.LParam), (int)(long)message.WParam, getModifierKeys() )); }
/// <summary> /// Reads the supplied message and executes any Keyboard events required. /// </summary> /// <param name="message">The Message to parse</param> /// <returns>A Boolean value indicating wether the Key events were handled or not</returns> private void WmKeyEvent(ref Message message) { // HandleKeyBindings(); // KeyPressEventArgs keyPressEventArgs = null; EventArgsKeyboard EventArgsKeyboard = null; if ((message.Id == NativeConstants.WM_CHAR) || (message.Id == NativeConstants.WM_SYSCHAR)) { // Is this extra information necessary? // wm_(sys)char: http://msdn.microsoft.com/en-us/library/ms646276(VS.85).aspx EventArgsKeyboard = new EventArgsKeyboard( (WinKeys)(int)(long)message.WParam, (int)(long)message.LParam, getModifierKeys() ); IntPtr zero = (IntPtr)0;// (char)((ushort)((long)message.WParam)); OnChar(EventArgsKeyboard); } else { // wm_(sys)keydown: http://msdn.microsoft.com/en-us/library/ms912654.aspx // wm_(sys)keyup: http://msdn.microsoft.com/en-us/library/ms646281(VS.85).aspx EventArgsKeyboard = new EventArgsKeyboard( (WinKeys)(int)(long)message.WParam, (int)(long)message.LParam, getModifierKeys() ); if ((message.Id == NativeConstants.WM_KEYDOWN) || (message.Id == NativeConstants.WM_SYSKEYDOWN)) { OnKeyDown(EventArgsKeyboard); } else if ((message.Id == NativeConstants.WM_KEYUP) || (message.Id == NativeConstants.WM_SYSKEYUP)) { OnKeyUp(EventArgsKeyboard); } } }
protected override IntPtr WndProcHook(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { Message message = new Message(msg, wParam, lParam); if (WndPrc(ref message) == WP_NOPASSTHROUGH) return IntPtr.Zero; return base.WndProcHook(hWnd, msg, wParam, lParam); }