Esempio n. 1
0
        private bool ProcessRawInput(IntPtr hdevice)
        {
            if (_deviceList.Count == 0)
            {
                return(false);
            }
            var size = 0;

            Win32Methods.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref size,
                                         Marshal.SizeOf(typeof(RawInputHeader)));
            InputData rawBuffer;

            if (
                Win32Methods.GetRawInputData(hdevice, DataCommand.RID_INPUT, out rawBuffer, ref size,
                                             Marshal.SizeOf(typeof(RawInputHeader))) != size)
            {
                Debug.WriteLine("Error getting the rawinput buffer");
                return(false);
            }
            int vKey     = rawBuffer.data.keyboard.VKey;
            int makecode = rawBuffer.data.keyboard.Makecode;
            int flags    = rawBuffer.data.keyboard.Flags;

            if (vKey == Win32Consts.KEYBOARD_OVERRUN_MAKE_CODE)
            {
                return(false);
            }

            RawKeyboardDevice device;

            lock (_lock)
            {
                if (!_deviceList.TryGetValue(rawBuffer.header.hDevice, out device))
                {
                    Debug.WriteLine("Handle: {0} was not in the device list.", rawBuffer.header.hDevice);
                    return(false);
                }
            }

            var isE0BitSet    = (flags & Win32Consts.RI_KEY_E0) != 0;
            var isBreakBitSet = (flags & Win32Consts.RI_KEY_BREAK) != 0;

            var message    = rawBuffer.data.keyboard.Message;
            var key        = KeyInterop.KeyFromVirtualKey(AdjustVirtualKey(rawBuffer, vKey, isE0BitSet, makecode));
            var keyPressed = KeyPressed;

            if (keyPressed != null)
            {
                var rawInputEventArgs = new RawInputEventArgs(device, isBreakBitSet ? KeyPressState.Up : KeyPressState.Down,
                                                              message, key, vKey);
                keyPressed(this, rawInputEventArgs);
                if (rawInputEventArgs.Handled)
                {
                    MSG msg;
                    Win32Methods.PeekMessage(out msg, IntPtr.Zero, Win32Consts.WM_KEYDOWN, Win32Consts.WM_KEYUP, Win32Consts.PM_REMOVE);
                }
                return(rawInputEventArgs.Handled);
            }
            return(false);
        }
Esempio n. 2
0
        private bool ProcessRawInput(IntPtr hdevice)
        {
            if (_deviceList.Count == 0)
            {
                return false;
            }
            int size = 0;
            Win32Methods.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref size, Marshal.SizeOf(typeof(RawInputHeader)));
            InputData rawBuffer;
            if (Win32Methods.GetRawInputData(hdevice, DataCommand.RID_INPUT, out rawBuffer, ref size, Marshal.SizeOf(typeof(RawInputHeader))) != size)
            {
                Debug.WriteLine("Error getting the rawinput buffer");
                return false;
            }
            int vKey = rawBuffer.data.keyboard.VKey;
            int makecode = rawBuffer.data.keyboard.Makecode;
            int flags = rawBuffer.data.keyboard.Flags;
            if (vKey == Win32Consts.KEYBOARD_OVERRUN_MAKE_CODE)
            {
                return false;
            }

            RawKeyboardDevice device;
            if (!_deviceList.TryGetValue(rawBuffer.header.hDevice, out device))
            {
                Debug.WriteLine("Handle: {0} was not in the device list.", rawBuffer.header.hDevice);
                return false;
            }

            var isE0BitSet = ((flags & Win32Consts.RI_KEY_E0) != 0);
            bool isBreakBitSet = (flags & Win32Consts.RI_KEY_BREAK) != 0;

            uint message = rawBuffer.data.keyboard.Message;
            Key key = KeyInterop.KeyFromVirtualKey(AdjustVirtualKey(rawBuffer, vKey, isE0BitSet, makecode));
            EventHandler<RawInputEventArgs> keyPressed = KeyPressed;
            if (keyPressed != null)
            {
                var rawInputEventArgs = new RawInputEventArgs(device, isBreakBitSet ? KeyPressState.Up : KeyPressState.Down,
                    message, key, vKey);
                keyPressed(this, rawInputEventArgs);
                if (rawInputEventArgs.Handled)
                {
                    MSG msg;
                    Win32Methods.PeekMessage(out msg, IntPtr.Zero, Win32Consts.WM_KEYDOWN, Win32Consts.WM_KEYUP, Win32Consts.PM_REMOVE);
                }
                return rawInputEventArgs.Handled;
            }
            return false;
        }