GetRawInputData() private method

private GetRawInputData ( HRAWINPUT RawInput, GetRawInputDataEnum Command, RawInput Data, [ Size, INT SizeHeader ) : INT
RawInput HRAWINPUT
Command GetRawInputDataEnum
Data RawInput
Size [
SizeHeader INT
return INT
Ejemplo n.º 1
0
        public bool ProcessKeyboardEvent(IntPtr raw)
        {
            bool processed = false;

            RawInput rin;

            if (Functions.GetRawInputData(raw, out rin) > 0)
            {
                bool pressed =
                    rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN ||
                    rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN;
                var scancode = rin.Data.Keyboard.MakeCode;
                var vkey     = rin.Data.Keyboard.VKey;

                bool extended0 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E0) != 0;
                bool extended1 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E1) != 0;

                bool is_valid = true;

                ContextHandle handle = new ContextHandle(rin.Header.Device);
                KeyboardState keyboard;
                if (!rawids.ContainsKey(handle))
                {
                    //RefreshDevices();
                }

                if (keyboards.Count == 0)
                {
                    return(false);
                }

                // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0
                // as rin.Header.Device for the "zoom-in/zoom-out" buttons.
                // That's problematic, because no device has a "0" id.
                // As a workaround, we'll add those buttons to the first device (if any).
                int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0;
                keyboard = keyboards[keyboard_handle];

                Key key = WinKeyMap.TranslateKey(scancode, vkey, extended0, extended1, out is_valid);

                if (is_valid)
                {
                    keyboard.SetKeyState(key, pressed);
                    processed = true;
                }

                lock (UpdateLock)
                {
                    keyboards[keyboard_handle] = keyboard;
                    processed = true;
                }
            }

            return(processed);
        }
Ejemplo n.º 2
0
        // Processes the input Windows Message, routing the buffer to the correct Keyboard, Mouse or HID.
        protected unsafe override IntPtr WindowProcedure(
            IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
        {
            try
            {
                switch (message)
                {
                case WindowMessage.INPUT:
                {
                    // Retrieve the raw input data buffer
                    RawInputHeader header;
                    if (Functions.GetRawInputData(lParam, out header) == RawInputHeader.SizeInBytes)
                    {
                        switch (header.Type)
                        {
                        case RawInputDeviceType.KEYBOARD:
                            if (((WinRawKeyboard)KeyboardDriver).ProcessKeyboardEvent(lParam))
                            {
                                return(IntPtr.Zero);
                            }
                            break;

                        case RawInputDeviceType.MOUSE:
                            if (((WinRawMouse)MouseDriver).ProcessMouseEvent(lParam))
                            {
                                return(IntPtr.Zero);
                            }
                            break;

                        case RawInputDeviceType.HID:
                            if (((WinRawJoystick)JoystickDriver).ProcessEvent(lParam))
                            {
                                return(IntPtr.Zero);
                            }
                            break;
                        }
                    }
                }
                break;

                case WindowMessage.DEVICECHANGE:
                    ((WinRawKeyboard)KeyboardDriver).RefreshDevices();
                    ((WinRawMouse)MouseDriver).RefreshDevices();
                    ((WinRawJoystick)JoystickDriver).RefreshDevices();
                    break;
                }
                return(base.WindowProcedure(handle, message, wParam, lParam));
            }
            catch (Exception e)
            {
                Debug.Print("[WinRawInput] Caught unhandled exception {0}", e);
                return(IntPtr.Zero);
            }
        }
Ejemplo n.º 3
0
        // Processes the input Windows Message, routing the buffer to the correct Keyboard, Mouse or HID.
        protected override IntPtr WindowProcedure(
            IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
        {
            switch (message)
            {
            case WindowMessage.INPUT:
                int size = 0;
                // Get the size of the input buffer
                Functions.GetRawInputData(lParam, GetRawInputDataEnum.INPUT,
                                          IntPtr.Zero, ref size, API.RawInputHeaderSize);

                // Read the actual raw input structure
                if (size == Functions.GetRawInputData(lParam, GetRawInputDataEnum.INPUT,
                                                      out data, ref size, API.RawInputHeaderSize))
                {
                    switch (data.Header.Type)
                    {
                    case RawInputDeviceType.KEYBOARD:
                        if (((WinRawKeyboard)KeyboardDriver).ProcessKeyboardEvent(data))
                        {
                            return(IntPtr.Zero);
                        }
                        break;

                    case RawInputDeviceType.MOUSE:
                        if (((WinRawMouse)MouseDriver).ProcessMouseEvent(data))
                        {
                            return(IntPtr.Zero);
                        }
                        break;

                    case RawInputDeviceType.HID:
                        break;
                    }
                }
                break;

            case WindowMessage.DEVICECHANGE:
                ((WinRawKeyboard)KeyboardDriver).RefreshDevices();
                ((WinRawMouse)MouseDriver).RefreshDevices();
                ((WinMMJoystick)JoystickDriver).RefreshDevices();
                break;
            }
            return(base.WindowProcedure(handle, message, wParam, lParam));
        }
Ejemplo n.º 4
0
        protected override IntPtr WindowProcedure(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
        {
            switch (message)
            {
            case WindowMessage.INPUT:
                int Size = 0;
                Functions.GetRawInputData(lParam, GetRawInputDataEnum.INPUT, IntPtr.Zero, out Size, API.RawInputHeaderSize);
                if (Size == Functions.GetRawInputData(lParam, GetRawInputDataEnum.INPUT, out WinRawInput.data, out Size, API.RawInputHeaderSize))
                {
                    switch (WinRawInput.data.Header.Type)
                    {
                    case RawInputDeviceType.MOUSE:
                        if (((WinRawMouse)this.MouseDriver).ProcessMouseEvent(WinRawInput.data))
                        {
                            return(IntPtr.Zero);
                        }
                        else
                        {
                            break;
                        }

                    case RawInputDeviceType.KEYBOARD:
                        if (((WinRawKeyboard)this.KeyboardDriver).ProcessKeyboardEvent(WinRawInput.data))
                        {
                            return(IntPtr.Zero);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    break;
                }

            case WindowMessage.DEVICECHANGE:
                ((WinRawKeyboard)this.KeyboardDriver).RefreshDevices();
                ((WinRawMouse)this.MouseDriver).RefreshDevices();
                break;
            }
            return(base.WindowProcedure(handle, message, wParam, lParam));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Processes the input Windows Message, routing the buffer to the correct Keyboard, Mouse or HID.
        /// </summary>
        /// <param name="msg">The WM_INPUT message, containing the buffer on the input event.</param>
        protected override void WndProc(ref Message msg)
        {
            switch ((WindowMessage)msg.Msg)
            {
            case WindowMessage.INPUT:
                int size = 0;
                // Get the size of the input buffer
                Functions.GetRawInputData(msg.LParam, GetRawInputDataEnum.INPUT,
                                          IntPtr.Zero, ref size, API.RawInputHeaderSize);

                //if (buffer == null || API.RawInputSize < size)
                //{
                //    throw new ApplicationException("Critical error when processing raw windows input.");
                //}
                if (size == Functions.GetRawInputData(msg.LParam, GetRawInputDataEnum.INPUT,
                                                      out data, ref size, API.RawInputHeaderSize))
                {
                    switch (data.Header.Type)
                    {
                    case RawInputDeviceType.KEYBOARD:
                        if (!keyboardDriver.ProcessKeyboardEvent(data))
                        {
                            Functions.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize);
                        }
                        return;

                    case RawInputDeviceType.MOUSE:
                        if (!mouseDriver.ProcessEvent(data))
                        {
                            Functions.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize);
                        }
                        return;

                    case RawInputDeviceType.HID:
                        Functions.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize);
                        return;

                    default:
                        break;
                    }
                }
                else
                {
                    throw new ApplicationException(String.Format(
                                                       "GetRawInputData returned invalid buffer. Windows error {0}. Please file a bug at http://opentk.sourceforge.net",
                                                       Marshal.GetLastWin32Error()));
                }
                break;

            case WindowMessage.DESTROY:
                Debug.Print("Input window detached from parent {0}.", Handle);
                ReleaseHandle();
                break;

            case WindowMessage.QUIT:
                Debug.WriteLine("Input window quit.");
                this.Dispose();
                break;
            }

            base.WndProc(ref msg);
        }
Ejemplo n.º 6
0
        public bool ProcessMouseEvent(IntPtr raw_buffer)
        {
            bool processed = false;

            RawInput rin;

            if (Functions.GetRawInputData(raw_buffer, out rin) > 0)
            {
                RawMouse      raw    = rin.Data.Mouse;
                ContextHandle handle = new ContextHandle(rin.Header.Device);

                MouseState mouse;
                if (!rawids.ContainsKey(handle))
                {
                    RefreshDevices();
                }

                if (mice.Count == 0)
                {
                    return(false);
                }

                // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0
                // as rin.Header.Device for the "zoom-in/zoom-out" buttons.
                // That's problematic, because no device has a "0" id.
                // As a workaround, we'll add those buttons to the first device (if any).
                int mouse_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0;
                mouse = mice[mouse_handle];

                // Set and release capture of the mouse to fix http://www.opentk.com/node/2133, Patch by Artfunkel
                if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != 0)
                {
                    mouse.EnableBit((int)MouseButton.Left);
                    Functions.SetCapture(Window);
                }
                if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != 0)
                {
                    mouse.DisableBit((int)MouseButton.Left);
                    Functions.ReleaseCapture();
                }
                if ((raw.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_DOWN) != 0)
                {
                    mouse.EnableBit((int)MouseButton.Right);
                    Functions.SetCapture(Window);
                }
                if ((raw.ButtonFlags & RawInputMouseState.RIGHT_BUTTON_UP) != 0)
                {
                    mouse.DisableBit((int)MouseButton.Right);
                    Functions.ReleaseCapture();
                }
                if ((raw.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_DOWN) != 0)
                {
                    mouse.EnableBit((int)MouseButton.Middle);
                    Functions.SetCapture(Window);
                }
                if ((raw.ButtonFlags & RawInputMouseState.MIDDLE_BUTTON_UP) != 0)
                {
                    mouse.DisableBit((int)MouseButton.Middle);
                    Functions.ReleaseCapture();
                }
                if ((raw.ButtonFlags & RawInputMouseState.BUTTON_4_DOWN) != 0)
                {
                    mouse.EnableBit((int)MouseButton.Button1);
                    Functions.SetCapture(Window);
                }
                if ((raw.ButtonFlags & RawInputMouseState.BUTTON_4_UP) != 0)
                {
                    mouse.DisableBit((int)MouseButton.Button1);
                    Functions.ReleaseCapture();
                }
                if ((raw.ButtonFlags & RawInputMouseState.BUTTON_5_DOWN) != 0)
                {
                    mouse.EnableBit((int)MouseButton.Button2);
                    Functions.SetCapture(Window);
                }
                if ((raw.ButtonFlags & RawInputMouseState.BUTTON_5_UP) != 0)
                {
                    mouse.DisableBit((int)MouseButton.Button2);
                    Functions.ReleaseCapture();
                }

                if ((raw.ButtonFlags & RawInputMouseState.WHEEL) != 0)
                {
                    mouse.SetScrollRelative(0, (short)raw.ButtonData / 120.0f);
                }

                if ((raw.ButtonFlags & RawInputMouseState.HWHEEL) != 0)
                {
                    mouse.SetScrollRelative((short)raw.ButtonData / 120.0f, 0);
                }

                if ((raw.Flags & RawMouseFlags.MOUSE_MOVE_ABSOLUTE) != 0)
                {
                    mouse.X = raw.LastX;
                    mouse.Y = raw.LastY;
                }
                else
                {   // Seems like MOUSE_MOVE_RELATIVE is the default, unless otherwise noted.
                    mouse.X += raw.LastX;
                    mouse.Y += raw.LastY;
                }

                lock (UpdateLock)
                {
                    mice[mouse_handle] = mouse;
                    processed          = true;
                }
            }

            return(processed);
        }
Ejemplo n.º 7
0
        public unsafe bool ProcessEvent(IntPtr raw)
        {
            // Query the size of the raw HID data buffer
            int size = 0;

            Functions.GetRawInputData(raw, GetRawInputDataEnum.INPUT, IntPtr.Zero, ref size, RawInputHeader.SizeInBytes);
            if (size > HIDData.Length)
            {
                Array.Resize(ref HIDData, size);
            }

            // Retrieve the raw HID data buffer
            if (Functions.GetRawInputData(raw, HIDData) > 0)
            {
                fixed(byte *pdata = HIDData)
                {
                    RawInput *rin = (RawInput *)pdata;

                    IntPtr handle = rin->Header.Device;
                    Device stick  = GetDevice(handle);

                    if (stick == null)
                    {
                        Debug.Print("[WinRawJoystick] Unknown device {0}", handle);
                        return(false);
                    }

                    if (stick.IsXInput)
                    {
                        return(true);
                    }

                    if (!GetPreparsedData(handle, ref PreparsedData))
                    {
                        return(false);
                    }

                    // Query current state
                    // Allocate enough storage to hold the data of the current report
                    int report_count = HidProtocol.MaxDataListLength(HidProtocolReportType.Input, PreparsedData);

                    if (report_count == 0)
                    {
                        Debug.Print("[WinRawJoystick] HidProtocol.MaxDataListLength() failed with {0}",
                                    Marshal.GetLastWin32Error());
                        return(false);
                    }

                    // Fill the data buffer
                    if (DataBuffer.Length < report_count)
                    {
                        Array.Resize(ref DataBuffer, report_count);
                    }

                    UpdateAxes(rin, stick);
                    UpdateButtons(rin, stick);
                    return(true);
                }
            }

            return(false);
        }