# HG changeset patch # User sl # Date 1416849645 -3600 # Node ID 2044181ade126c244ec9ab52e1dada8767a3035e # Parent 0d9f479cbbf74a66e0002a755a978ebe87ea85bc Now capable of handling WM_INPUT when in background. diff -r 0d9f479cbbf7 -r 2044181ade12 Form1.cs --- a/Form1.cs Mon Nov 24 16:46:26 2014 +0100 +++ b/Form1.cs Mon Nov 24 18:20:45 2014 +0100 @@ -32,9 +32,7 @@ _timer = new Timer(); _timer.Interval = 3000; _timer.Enabled = false; - _timer.Tick +=new EventHandler(_timer_Tick); - _remote = new RemoteControlDevice(); - _remote.ButtonPressed +=new Devices.RemoteControl.RemoteControlDevice.RemoteControlDeviceEventHandler(_remote_ButtonPressed); + _timer.Tick +=new EventHandler(_timer_Tick); } /// @@ -110,13 +108,17 @@ private void Form1_Load(object sender, System.EventArgs e) { - + _remote = new RemoteControlDevice(this.Handle); + _remote.ButtonPressed += new Devices.RemoteControl.RemoteControlDevice.RemoteControlDeviceEventHandler(_remote_ButtonPressed); } protected override void WndProc(ref Message message) { - _remote.ProcessMessage(message); + if (_remote != null) + { + _remote.ProcessMessage(message); + } base.WndProc(ref message); } diff -r 0d9f479cbbf7 -r 2044181ade12 RemoteControlDevice.cs --- a/RemoteControlDevice.cs Mon Nov 24 16:46:26 2014 +0100 +++ b/RemoteControlDevice.cs Mon Nov 24 18:20:45 2014 +0100 @@ -126,45 +126,17 @@ public sealed class RemoteControlDevice { - private const int WM_KEYDOWN = 0x0100; - private const int WM_APPCOMMAND = 0x0319; - private const int WM_INPUT = 0x00FF; - - private const int APPCOMMAND_BROWSER_BACKWARD = 1; - private const int APPCOMMAND_VOLUME_MUTE = 8; - private const int APPCOMMAND_VOLUME_DOWN = 9; - private const int APPCOMMAND_VOLUME_UP = 10; - private const int APPCOMMAND_MEDIA_NEXTTRACK = 11; - private const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12; - private const int APPCOMMAND_MEDIA_STOP = 13; - private const int APPCOMMAND_MEDIA_PLAY_PAUSE = 14; - private const int APPCOMMAND_MEDIA_PLAY = 46; - private const int APPCOMMAND_MEDIA_PAUSE = 47; - private const int APPCOMMAND_MEDIA_RECORD = 48; - private const int APPCOMMAND_MEDIA_FAST_FORWARD = 49; - private const int APPCOMMAND_MEDIA_REWIND = 50; - private const int APPCOMMAND_MEDIA_CHANNEL_UP = 51; - private const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52; - - private const int FAPPCOMMAND_MASK = 0xF000; - private const int FAPPCOMMAND_MOUSE = 0x8000; - private const int FAPPCOMMAND_KEY = 0; - private const int FAPPCOMMAND_OEM = 0x1000; - - - public delegate void RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e); public event RemoteControlDeviceEventHandler ButtonPressed; public delegate void HidUsageHandler(ushort aUsage); - //------------------------------------------------------------- // constructors //------------------------------------------------------------- - public RemoteControlDevice() + public RemoteControlDevice(IntPtr aHWND) { // Register the input device to receive the commands from the // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp @@ -174,22 +146,22 @@ rid[0].usUsagePage = 0xFFBC; rid[0].usUsage = 0x88; - rid[0].dwFlags = 0; + rid[0].dwFlags = Const.RIDEV_EXINPUTSINK; + rid[0].hwndTarget = aHWND; rid[1].usUsagePage = 0x0C; rid[1].usUsage = 0x01; - rid[1].dwFlags = 0; + rid[1].dwFlags = Const.RIDEV_EXINPUTSINK; + rid[1].hwndTarget = aHWND; rid[2].usUsagePage = 0x0C; rid[2].usUsage = 0x80; - rid[2].dwFlags = 0; + rid[2].dwFlags = Const.RIDEV_EXINPUTSINK; + rid[2].hwndTarget = aHWND; - if (!Function.RegisterRawInputDevices(rid, - (uint) rid.Length, - (uint) Marshal.SizeOf(rid[0])) - ) + if (!Function.RegisterRawInputDevices(rid,(uint) rid.Length,(uint) Marshal.SizeOf(rid[0]))) { - throw new ApplicationException("Failed to register raw input devices."); + throw new ApplicationException("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } } @@ -200,19 +172,15 @@ public void ProcessMessage(Message message) { - int param; - switch (message.Msg) { - case WM_KEYDOWN: - param = message.WParam.ToInt32(); - ProcessKeyDown(param); + case Const.WM_KEYDOWN: + ProcessKeyDown(message.WParam); break; - case WM_APPCOMMAND: - param = message.LParam.ToInt32(); - ProcessAppCommand(param); + case Const.WM_APPCOMMAND: + ProcessAppCommand(message.LParam); break; - case WM_INPUT: + case Const.WM_INPUT: ProcessInputCommand(ref message); message.Result = new IntPtr(0); break; @@ -225,11 +193,11 @@ // methods (helpers) //------------------------------------------------------------- - private void ProcessKeyDown(int param) + private void ProcessKeyDown(IntPtr wParam) { RemoteControlButton rcb = RemoteControlButton.Unknown; - switch (param) + switch (wParam.ToInt32()) { case (int) Keys.Escape: rcb = RemoteControlButton.Clear; @@ -282,67 +250,68 @@ } if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown) - this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(param))); + this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(wParam))); } - private void ProcessAppCommand(int param) + private void ProcessAppCommand(IntPtr lParam) { RemoteControlButton rcb = RemoteControlButton.Unknown; - int cmd = (int) (((ushort) (param >> 16)) & ~FAPPCOMMAND_MASK); + int cmd = Macro.GET_APPCOMMAND_LPARAM(lParam); + //(int) (((ushort) (param >> 16)) & ~Const.FAPPCOMMAND_MASK); switch (cmd) { - case APPCOMMAND_BROWSER_BACKWARD: + case Const.APPCOMMAND_BROWSER_BACKWARD: rcb = RemoteControlButton.Back; break; - case APPCOMMAND_MEDIA_CHANNEL_DOWN: + case Const.APPCOMMAND_MEDIA_CHANNEL_DOWN: rcb = RemoteControlButton.ChannelDown; break; - case APPCOMMAND_MEDIA_CHANNEL_UP: + case Const.APPCOMMAND_MEDIA_CHANNEL_UP: rcb = RemoteControlButton.ChannelUp; break; - case APPCOMMAND_MEDIA_FAST_FORWARD: + case Const.APPCOMMAND_MEDIA_FAST_FORWARD: rcb = RemoteControlButton.FastForward; break; - case APPCOMMAND_VOLUME_MUTE: + case Const.APPCOMMAND_VOLUME_MUTE: rcb = RemoteControlButton.VolumeMute; break; - case APPCOMMAND_MEDIA_PAUSE: + case Const.APPCOMMAND_MEDIA_PAUSE: rcb = RemoteControlButton.Pause; break; - case APPCOMMAND_MEDIA_PLAY: + case Const.APPCOMMAND_MEDIA_PLAY: rcb = RemoteControlButton.Play; break; - case APPCOMMAND_MEDIA_PLAY_PAUSE: + case Const.APPCOMMAND_MEDIA_PLAY_PAUSE: rcb = RemoteControlButton.PlayPause; break; - case APPCOMMAND_MEDIA_RECORD: + case Const.APPCOMMAND_MEDIA_RECORD: rcb = RemoteControlButton.Record; break; - case APPCOMMAND_MEDIA_PREVIOUSTRACK: + case Const.APPCOMMAND_MEDIA_PREVIOUSTRACK: rcb = RemoteControlButton.PreviousTrack; break; - case APPCOMMAND_MEDIA_REWIND: + case Const.APPCOMMAND_MEDIA_REWIND: rcb = RemoteControlButton.Rewind; break; - case APPCOMMAND_MEDIA_NEXTTRACK: + case Const.APPCOMMAND_MEDIA_NEXTTRACK: rcb = RemoteControlButton.NextTrack; break; - case APPCOMMAND_MEDIA_STOP: + case Const.APPCOMMAND_MEDIA_STOP: rcb = RemoteControlButton.Stop; break; - case APPCOMMAND_VOLUME_DOWN: + case Const.APPCOMMAND_VOLUME_DOWN: rcb = RemoteControlButton.VolumeDown; break; - case APPCOMMAND_VOLUME_UP: + case Const.APPCOMMAND_VOLUME_UP: rcb = RemoteControlButton.VolumeUp; break; } if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown) - this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(param))); + this.ButtonPressed(this, new RemoteControlEventArgs(rcb, GetDevice(lParam))); } /// @@ -413,8 +382,18 @@ private void ProcessInputCommand(ref Message message) { + //We received a WM_INPUT message Debug.WriteLine("================WM_INPUT================"); + //Check if we received this message while in background or foreground + if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUT) + { + Debug.WriteLine("================FOREGROUND"); + } + else if (Macro.GET_RAWINPUT_CODE_WPARAM(message.WParam) == Const.RIM_INPUTSINK) + { + Debug.WriteLine("================BACKGROUND"); + } //Declare a pointer IntPtr rawInputBuffer = IntPtr.Zero; @@ -535,16 +514,16 @@ } - private InputDevice GetDevice(int param) + private InputDevice GetDevice(IntPtr lParam) { InputDevice inputDevice; - switch ((int) (((ushort) (param >> 16)) & FAPPCOMMAND_MASK)) + switch (Macro.GET_DEVICE_LPARAM(lParam)) { - case FAPPCOMMAND_OEM: + case Const.FAPPCOMMAND_OEM: inputDevice = InputDevice.OEM; break; - case FAPPCOMMAND_MOUSE: + case Const.FAPPCOMMAND_MOUSE: inputDevice = InputDevice.Mouse; break; default: diff -r 0d9f479cbbf7 -r 2044181ade12 Win32RawInput.cs --- a/Win32RawInput.cs Mon Nov 24 16:46:26 2014 +0100 +++ b/Win32RawInput.cs Mon Nov 24 18:20:45 2014 +0100 @@ -16,9 +16,61 @@ public extern static int GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize); } + + static partial class Macro + { + /// + /// Retrieves the input code from wParam in WM_INPUT. + /// See RIM_INPUT and RIM_INPUTSINK. + /// + /// + /// + public static int GET_RAWINPUT_CODE_WPARAM(IntPtr wParam) + { + return (wParam.ToInt32() & 0xff); + } + + public static int GET_APPCOMMAND_LPARAM(IntPtr lParam) + { + return ((short)HIWORD(lParam.ToInt32()) & ~Const.FAPPCOMMAND_MASK); + } + + public static int GET_DEVICE_LPARAM(IntPtr lParam) + { + return ((ushort)(HIWORD(lParam.ToInt32()) & Const.FAPPCOMMAND_MASK)); + } + + public static int HIWORD(int val) + { + return ((val >> 16) & 0xffff); + } + + + //#define HIWORD(l) ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff)) + //#define LOWORD(l) ((WORD)(((DWORD_PTR)(l)) & 0xffff)) + //#define LOBYTE(w) ((BYTE)(((DWORD_PTR)(w)) & 0xff)) + //#define HIBYTE(w) ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff)) + + //#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK)) + //#define GET_DEVICE_LPARAM(lParam) ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK)) + //#define GET_MOUSEORKEY_LPARAM GET_DEVICE_LPARAM + //#define GET_FLAGS_LPARAM(lParam) (LOWORD(lParam)) + //#define GET_KEYSTATE_LPARAM(lParam) GET_FLAGS_LPARAM(lParam) + + } + + + static partial class Const { /// + /// Windows Messages + /// + public const int WM_KEYDOWN = 0x0100; + public const int WM_APPCOMMAND = 0x0319; + public const int WM_INPUT = 0x00FF; + + /// /// GetRawInputDeviceInfo pData points to a string that contains the device name. /// public const uint RIDI_DEVICENAME = 0x20000007; @@ -48,6 +100,91 @@ public const int RID_INPUT = 0x10000003; public const int RID_HEADER = 0x10000005; + /// + /// Possible value taken by wParam for WM_INPUT. + /// + /// Input occurred while the application was in the foreground. The application must call DefWindowProc so the system can perform cleanup. + /// + public const int RIM_INPUT = 0; + /// + /// Possible value taken by wParam for WM_INPUT. + /// + /// Input occurred while the application was not in the foreground. The application must call DefWindowProc so the system can perform the cleanup. + /// + public const int RIM_INPUTSINK = 1; + + /// + /// If set, the application command keys are handled. RIDEV_APPKEYS can be specified only if RIDEV_NOLEGACY is specified for a keyboard device. + /// + public const uint RIDEV_APPKEYS = 0x00000400; + + /// + /// If set, the mouse button click does not activate the other window. + /// + public const uint RIDEV_CAPTUREMOUSE = 0x00000200; + + /// + /// If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE notifications for device arrival and device removal. + /// Windows XP: This flag is not supported until Windows Vista + /// + public const uint RIDEV_DEVNOTIFY = 0x00002000; + + /// + /// If set, this specifies the top level collections to exclude when reading a complete usage page. This flag only affects a TLC whose usage page is already specified with RIDEV_PAGEONLY. + /// + public const uint RIDEV_EXCLUDE = 0x00000010; + + /// + /// If set, this enables the caller to receive input in the background only if the foreground application does not process it. In other words, if the foreground application is not registered for raw input, then the background application that is registered will receive the input. + /// Windows XP: This flag is not supported until Windows Vista + /// + public const uint RIDEV_EXINPUTSINK = 0x00001000; + + /// + /// If set, this enables the caller to receive the input even when the caller is not in the foreground. Note that hwndTarget must be specified. + /// + public const uint RIDEV_INPUTSINK = 0x00000100; + + /// + /// If set, the application-defined keyboard device hotkeys are not handled. However, the system hotkeys; for example, ALT+TAB and CTRL+ALT+DEL, are still handled. By default, all keyboard hotkeys are handled. RIDEV_NOHOTKEYS can be specified even if RIDEV_NOLEGACY is not specified and hwndTarget is NULL. + /// + public const uint RIDEV_NOHOTKEYS = 0x00000200; + + /// + /// If set, this prevents any devices specified by usUsagePage or usUsage from generating legacy messages. This is only for the mouse and keyboard. See Remarks. + /// + public const uint RIDEV_NOLEGACY = 0x00000030; + + /// + /// If set, this specifies all devices whose top level collection is from the specified usUsagePage. Note that usUsage must be zero. To exclude a particular top level collection, use RIDEV_EXCLUDE. + /// + public const uint RIDEV_PAGEONLY = 0x00000020; + + /// + /// If set, this removes the top level collection from the inclusion list. This tells the operating system to stop reading from a device which matches the top level collection. + /// + public const uint RIDEV_REMOVE = 0x00000001; + + public const int APPCOMMAND_BROWSER_BACKWARD = 1; + public const int APPCOMMAND_VOLUME_MUTE = 8; + public const int APPCOMMAND_VOLUME_DOWN = 9; + public const int APPCOMMAND_VOLUME_UP = 10; + public const int APPCOMMAND_MEDIA_NEXTTRACK = 11; + public const int APPCOMMAND_MEDIA_PREVIOUSTRACK = 12; + public const int APPCOMMAND_MEDIA_STOP = 13; + public const int APPCOMMAND_MEDIA_PLAY_PAUSE = 14; + public const int APPCOMMAND_MEDIA_PLAY = 46; + public const int APPCOMMAND_MEDIA_PAUSE = 47; + public const int APPCOMMAND_MEDIA_RECORD = 48; + public const int APPCOMMAND_MEDIA_FAST_FORWARD = 49; + public const int APPCOMMAND_MEDIA_REWIND = 50; + public const int APPCOMMAND_MEDIA_CHANNEL_UP = 51; + public const int APPCOMMAND_MEDIA_CHANNEL_DOWN = 52; + + public const int FAPPCOMMAND_MASK = 0xF000; + public const int FAPPCOMMAND_MOUSE = 0x8000; + public const int FAPPCOMMAND_KEY = 0; + public const int FAPPCOMMAND_OEM = 0x1000; } @@ -60,7 +197,7 @@ [MarshalAs(UnmanagedType.U2)] public ushort usUsage; [MarshalAs(UnmanagedType.U4)] - public int dwFlags; + public uint dwFlags; public IntPtr hwndTarget; }