private IntPtr WndProcHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            const int WM_INPUT = 0x00FF;

            if (msg == WM_INPUT)
            {
                var data = RawInputData.FromHandle(lParam);

                switch (data)
                {
                case RawInputMouseData mouse:
                    if (mouse.Mouse.Buttons != RawMouseButtonFlags.None && !mouse.Mouse.Buttons.ToString().Contains("Up"))
                    {
                        var button = GetButtonFromFlags(mouse.Mouse.Buttons);

                        if (button != RawMouseButton.None)
                        {
                            SetTextBoxText(String.Format("{0} {1}", GetFancyDeviceName(mouse.Device), button), data);
                        }
                    }
                    break;

                case RawInputKeyboardData keyboard:
                    SetTextBoxText(String.Format("{0} {1}", GetFancyDeviceName(keyboard.Device), (Keys)keyboard.Keyboard.VirutalKey), data);
                    break;
                }
            }

            return(IntPtr.Zero);
        }
Esempio n. 2
0
 private void OnReceiveRawInput(RawInputEventData data)
 {
     if (RawInputData.FromHandle(data.LParam) is RawInputMouseData mouseData)
     {
         ReceiveRawInputMouseData?.Invoke(mouseData);
     }
 }
 private void OnReceiveRawInput(RawInputEventData data)
 {
     if (RawInputData.FromHandle(data.LParam) is RawInputKeyboardData keyboardData)
     {
         ReceiveRawInputKeyboardData?.Invoke(keyboardData);
     }
 }
Esempio n. 4
0
        private static void HandleMessage(object sender, WindowMessageEventArgs e)
        {
            // Handle WM_INPUT
            if (e.Message != 0x00FF)
            {
                return;
            }

            try
            {
                var data = RawInputData.FromHandle(e.LParam);

                // Game already registers and uses Mouse, so pass that back.
                if (data.Device.UsageAndPage == HidUsageAndPage.Mouse)
                {
                    return;
                }

                // Mark the message as handled and return 0 for consumed.
                e.Result = IntPtr.Zero;

                ProcessInput(data);
            }
            catch (Exception ex)
            {
                Logging.Log($"Failed to process window message: {ex.Message}\n{ex.StackTrace}");
            }
        }
Esempio n. 5
0
        private void OnReceiveRawInput(IntPtr lParam)
        {
            var data = RawInputData.FromHandle(lParam);

            if (data is RawInputMouseData mouseData && mouseData.Mouse.Flags.HasFlag(RawMouseFlags.MoveRelative))
            {
                AddDif(mouseData.Mouse.LastX, mouseData.Mouse.LastY);
            }
 private void OnReceiveRawInput(IntPtr lParam)
 {
     if (RawInputData.FromHandle(lParam) is RawInputMouseData data &&
         data.Mouse.Flags.HasFlag(RawMouseFlags.MoveRelative))
     {
         AddDif(data.Mouse.LastX, data.Mouse.LastY);
     }
 }
    protected override void WndProc(ref Message m)
    {
        const int WM_INPUT = 0x00FF;

        if (m.Msg == WM_INPUT)
        {
            var data = RawInputData.FromHandle(m.LParam);
            Input?.Invoke(this, new RawInputEventArgs(data));
        }

        base.WndProc(ref m);
    }
Esempio n. 8
0
        protected override void WndProc(ref Message m)
        {
            const int WM_INPUT = 0x00FF;

            if (m.Msg == WM_INPUT)
            {
                var data = RawInputData.FromHandle(m.LParam);

                Input?.Invoke(this, new RawInputEventArgs(data));
            }
            Console.WriteLine("[+]uMsg: " + m.Msg + ", hwnd: " + m.HWnd + ", wParam: " + m.WParam + ", lParam: {0:x}", m.LParam.ToInt32());

            base.WndProc(ref m);
        }
Esempio n. 9
0
        protected override void WndProc(ref Message m)
        {
            const int WM_INPUT = 0x00FF;

            if (m.Msg == WM_INPUT)
            {
                RawInputData data = RawInputData.FromHandle(m.LParam);

                if (Input != null)
                {
                    Input(data);
                }
            }

            base.WndProc(ref m);
        }
Esempio n. 10
0
        protected override void WndProc(ref Message m)
        {
            const int WM_INPUT = 0x00FF;
            const int WM_CHAR  = 0x0102;

            if (m.Msg == WM_INPUT)
            {
                var data = RawInputData.FromHandle(m.LParam);

                Input?.Invoke(this, new RawInputEventArgs(data));
            }
            else if (m.Msg == WM_CHAR)
            {
                ImGui.GetIO().AddInputCharacter((uint)m.WParam.ToInt64());
            }

            base.WndProc(ref m);
        }
        private void SpongeOnWndProcCalled(object sender, Message message)
        {
            if (message.Msg != WM_INPUT)
            {
                return;
            }

            RawInputData data = RawInputData.FromHandle(message.LParam);

            switch (data)
            {
            case RawInputMouseData mouse:
                HandleMouseData(data, mouse);
                break;

            case RawInputKeyboardData keyboard:
                HandleKeyboardData(data, keyboard);
                break;
            }
        }
Esempio n. 12
0
        protected override void WndProc(ref Message m)
        {
            const int WM_INPUT = 0x00FF;

            if (m.Msg == WM_INPUT)
            {
                var data = RawInputData.FromHandle(m.LParam);
                if (data is RawInputMouseData mouseData)
                {
                    var mouse = mouseData.Mouse;
                    inputHandler.Handle(new MouseInput(mouse.LastX, mouse.LastY, mouse.ButtonData, (MouseFlags)mouse.Flags, (MouseButton)mouse.Buttons));
                }
                else if (data is RawInputKeyboardData keyboardData)
                {
                    var keyboard = keyboardData.Keyboard;
                    inputHandler.Handle(new KeyboardInput(keyboard.VirutalKey, keyboard.ScanCode, (KeyboardFlags)keyboard.Flags));
                }
            }

            base.WndProc(ref m);
        }
Esempio n. 13
0
    private IntPtr MessageSink(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        const int WM_INPUT = 0x00FF;

        if (msg == WM_INPUT)
        {
            var data = RawInputData.FromHandle(lParam);
            Logger.Trace(data);

            switch (data)
            {
            case RawInputKeyboardData keyboard:
                ParseKeyboardGestures(keyboard);
                break;

            case RawInputMouseData mouse:
                ParseMouseGestures(mouse);
                break;
            }
        }

        return(IntPtr.Zero);
    }
Esempio n. 14
0
        protected IntPtr Hook(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
        {
            const int WM_INPUT = 0x00FF;

            // You can read inputs by processing the WM_INPUT message.
            if (msg == WM_INPUT)
            {
                // Create an RawInputData from the handle stored in lParam.
                var data = RawInputData.FromHandle(lparam);

                // You can identify the source device using Header.DeviceHandle or just Device.
                //var sourceDeviceHandle = data.Header.DeviceHandle;
                //var sourceDevice = data.Device;

                // The data will be an instance of either RawInputMouseData, RawInputKeyboardData, or RawInputHidData.
                // They contain the raw input data in their properties.
                switch (data)
                {
                case RawInputMouseData mouse:
                    //RawInput only gives relative mouse movement value.. cheating here with Winform library.
                    var M = System.Windows.Forms.Control.MousePosition;
                    switch (mouse.Mouse.Buttons)
                    {
                    case Linearstar.Windows.RawInput.Native.RawMouseButtonFlags.LeftButtonDown:
                        MouseLBtnDownSimulate(M.X, M.Y);
                        break;

                    case Linearstar.Windows.RawInput.Native.RawMouseButtonFlags.LeftButtonUp:
                        MouseLBtnUpSimulate(M.X, M.Y);
                        break;

                    case Linearstar.Windows.RawInput.Native.RawMouseButtonFlags.RightButtonDown:
                        //issue: click being skipped.
                        //SetupDesktop.MouseRBtnDownSimulate(M.X, M.Y);
                        break;

                    case Linearstar.Windows.RawInput.Native.RawMouseButtonFlags.RightButtonUp:
                        //issue: click being skipped.
                        //SetupDesktop.MouseRBtnUpSimulate(M.X, M.Y);
                        break;

                    case Linearstar.Windows.RawInput.Native.RawMouseButtonFlags.None:
                        MouseMoveSimulate(M.X, M.Y);
                        break;

                    case Linearstar.Windows.RawInput.Native.RawMouseButtonFlags.MouseWheel:
                        /*
                         * https://github.com/ivarboms/game-engine/blob/master/Input/RawInput.cpp
                         * Mouse wheel deltas are represented as multiples of 120.
                         * MSDN: The delta was set to 120 to allow Microsoft or other vendors to build
                         * finer-resolution wheels (a freely-rotating wheel with no notches) to send more
                         * messages per rotation, but with a smaller value in each message.
                         * Because of this, the value is converted to a float in case a mouse's wheel
                         * reports a value other than 120, in which case dividing by 120 would produce
                         * a very incorrect value.
                         * More info: http://social.msdn.microsoft.com/forums/en-US/gametechnologiesgeneral/thread/1deb5f7e-95ee-40ac-84db-58d636f601c7/
                         */

                        //Disabled, not tested yet.

                        /*
                         * // One wheel notch is represented as this delta (WHEEL_DELTA).
                         * const float oneNotch = 120;
                         *
                         * // Mouse wheel delta in multiples of WHEEL_DELTA (120).
                         * float mouseWheelDelta = mouse.Mouse.RawButtons;
                         *
                         * // Convert each notch from [-120, 120] to [-1, 1].
                         * mouseWheelDelta = mouseWheelDelta / oneNotch;
                         *
                         * MouseScrollSimulate(mouseWheelDelta);
                         */
                        break;
                    }
                    break;
                }
            }
            return(IntPtr.Zero);
        }
Esempio n. 15
0
        public void WndProcReceived(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            const int WM_INPUT = 0x00FF;

            if (msg == WM_INPUT)
            {
                var data = RawInputData.FromHandle(lParam);

                string path = "null";

                if (data != null && data.Device != null && data.Device.DevicePath != null)
                {
                    path = data.Device.DevicePath;
                }

                switch (data)
                {
                case RawInputMouseData mouse:
                    // Handle mouse button presses
                    if (mouse.Mouse.Buttons != RawMouseButtonFlags.None)
                    {
                        RawMouseButtonFlags flags = mouse.Mouse.Buttons;

                        // Multiple buttons can be pressed/released in single event so check them all
                        if (flags.HasFlag(RawMouseButtonFlags.LeftButtonDown) || flags.HasFlag(RawMouseButtonFlags.LeftButtonUp))
                        {
                            foreach (var jsButton in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && btn.RawInputButton.MouseButton == RawMouseButton.LeftButton))
                            {
                                HandleRawInputButton(jsButton, flags.HasFlag(RawMouseButtonFlags.LeftButtonDown));
                            }
                        }

                        if (flags.HasFlag(RawMouseButtonFlags.RightButtonDown) || flags.HasFlag(RawMouseButtonFlags.RightButtonUp))
                        {
                            foreach (var jsButton in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && btn.RawInputButton.MouseButton == RawMouseButton.RightButton))
                            {
                                HandleRawInputButton(jsButton, flags.HasFlag(RawMouseButtonFlags.RightButtonDown));
                            }
                        }

                        if (flags.HasFlag(RawMouseButtonFlags.MiddleButtonDown) || flags.HasFlag(RawMouseButtonFlags.MiddleButtonUp))
                        {
                            foreach (var jsButton in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && btn.RawInputButton.MouseButton == RawMouseButton.MiddleButton))
                            {
                                HandleRawInputButton(jsButton, flags.HasFlag(RawMouseButtonFlags.MiddleButtonDown));
                            }
                        }

                        if (flags.HasFlag(RawMouseButtonFlags.Button4Down) || flags.HasFlag(RawMouseButtonFlags.Button4Up))
                        {
                            foreach (var jsButton in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && btn.RawInputButton.MouseButton == RawMouseButton.Button4))
                            {
                                HandleRawInputButton(jsButton, flags.HasFlag(RawMouseButtonFlags.Button4Down));
                            }
                        }

                        if (flags.HasFlag(RawMouseButtonFlags.Button5Down) || flags.HasFlag(RawMouseButtonFlags.Button5Up))
                        {
                            foreach (var jsButton in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && btn.RawInputButton.MouseButton == RawMouseButton.Button5))
                            {
                                HandleRawInputButton(jsButton, flags.HasFlag(RawMouseButtonFlags.Button5Down));
                            }
                        }
                    }

                    // Handle position
                    if (mouse.Mouse.Flags.HasFlag(RawMouseFlags.MoveAbsolute))
                    {
                        // Lightgun
                        foreach (var gun in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && (btn.InputMapping == InputMapping.P1LightGun || btn.InputMapping == InputMapping.P2LightGun)))
                        {
                            HandleRawInputGun(gun, mouse.Mouse.LastX, mouse.Mouse.LastY, true);
                        }
                    }
                    else if (mouse.Mouse.Flags.HasFlag(RawMouseFlags.MoveRelative))
                    {
                        // Windows mouse cursor
                        foreach (var gun in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == "Windows Mouse Cursor" && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && (btn.InputMapping == InputMapping.P1LightGun || btn.InputMapping == InputMapping.P2LightGun)))
                        {
                            HandleRawInputGun(gun, Cursor.Position.X, Cursor.Position.Y, false);
                        }

                        // Other relative movement mouse like device
                        foreach (var gun in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Mouse && (btn.InputMapping == InputMapping.P1LightGun || btn.InputMapping == InputMapping.P2LightGun)))
                        {
                            int calcX = 0;
                            int calcY = 0;

                            if (gun.InputMapping == InputMapping.P1LightGun)
                            {
                                calcX = Math.Min(Math.Max(_lastPosP1X + mouse.Mouse.LastX, _windowLocationX), _windowLocationX + _windowWidth);
                                calcY = Math.Min(Math.Max(_lastPosP1Y + mouse.Mouse.LastY, _windowLocationY), _windowLocationY + _windowHeight);

                                _lastPosP1X = calcX;
                                _lastPosP1Y = calcY;
                            }
                            else
                            {
                                calcX = Math.Min(Math.Max(_lastPosP2X + mouse.Mouse.LastX, _windowLocationX), _windowLocationX + _windowWidth);
                                calcY = Math.Min(Math.Max(_lastPosP2Y + mouse.Mouse.LastY, _windowLocationY), _windowLocationY + _windowHeight);

                                _lastPosP2X = calcX;
                                _lastPosP2Y = calcY;
                            }

                            HandleRawInputGun(gun, calcX, calcY, false);
                        }
                    }

                    break;

                case RawInputKeyboardData keyboard:
                    if ((Keys)keyboard.Keyboard.VirutalKey == Keys.ControlKey && !keyboard.Keyboard.Flags.HasFlag(RawKeyboardFlags.Up))
                    {
                        dontClip = true;
                    }
                    else
                    {
                        dontClip = false;
                    }

                    foreach (var jsButton in _joystickButtons.Where(btn => btn.RawInputButton.DevicePath == path && btn.RawInputButton.DeviceType == RawDeviceType.Keyboard && btn.RawInputButton.KeyboardKey == (Keys)keyboard.Keyboard.VirutalKey))
                    {
                        HandleRawInputButton(jsButton, !keyboard.Keyboard.Flags.HasFlag(RawKeyboardFlags.Up));
                    }

                    break;
                }
            }
        }
Esempio n. 16
0
        protected override void WndProc(ref Message m)
        {
            // Raw input event
            if (m.Msg == WM_INPUT)
            {
                var data = RawInputData.FromHandle(m.LParam);

                if (data == null)
                {
                    Log("RawInputData NULL!");
                    base.WndProc(ref m);
                    return;
                }

                // Device type
                switch (data)
                {
                case RawInputMouseData mouse:
                    string handleM = "00000000";

                    if (mouse.Device != null)
                    {
                        handleM = RawInputDeviceHandle.GetRawValue(mouse.Device.Handle).ToString("X8");
                    }

                    var nameM = GetFancyDeviceName(mouse.Device);
                    var mode  = mouse.Mouse.Flags;
                    var x     = mouse.Mouse.LastX;
                    var y     = mouse.Mouse.LastY;

                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        if ((string)(table.Rows[i].Cells[0].Value) == handleM)
                        {
                            bool resize = false;

                            if (table.Rows[i].Cells[6].Value.ToString() != mode.ToString())
                            {
                                resize = true;
                            }

                            table.Rows[i].Cells[6].Value = mode;
                            table.Rows[i].Cells[7].Value = x;
                            table.Rows[i].Cells[8].Value = y;

                            if (mode.HasFlag(RawMouseFlags.MoveRelative))
                            {
                                // Super advanced position determination
                                table.Rows[i].Cells[9].Value  = Math.Min(Math.Max(Int32.Parse(table.Rows[i].Cells[9].Value.ToString()) + x, 0), screenWidth);
                                table.Rows[i].Cells[10].Value = Math.Min(Math.Max(Int32.Parse(table.Rows[i].Cells[10].Value.ToString()) + y, 0), screenHeight);
                            }
                            else if (mode.HasFlag(RawMouseFlags.MoveAbsolute))
                            {
                                table.Rows[i].Cells[9].Value  = Math.Round((float)x / (float)0xFFFF * (float)screenWidth).ToString();
                                table.Rows[i].Cells[10].Value = Math.Round((float)y / (float)0xFFFF * (float)screenHeight).ToString();
                            }

                            if (resize)
                            {
                                table.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                            }

                            break;
                        }
                    }

                    // Log event?
                    if (chkEnabled.Checked)
                    {
                        if (mouse.Mouse.Buttons != RawMouseButtonFlags.None || (mouse.Mouse.Buttons == RawMouseButtonFlags.None && chkPosition.Checked))
                        {
                            Log($"Handle: {handleM:X8}\tName: {nameM}\tX: {mouse.Mouse.LastX}\tY: {mouse.Mouse.LastY}\tFlags: {mouse.Mouse.Flags}\tButtons: {mouse.Mouse.Buttons}\tData: {mouse.Mouse.ButtonData}");
                        }
                    }

                    break;

                case RawInputKeyboardData keyboard:
                    string handleK = "00000000";

                    if (keyboard.Device != null)
                    {
                        handleK = RawInputDeviceHandle.GetRawValue(keyboard.Device.Handle).ToString("X8");
                    }

                    var nameK = GetFancyDeviceName(keyboard.Device);

                    // Log event?
                    if (chkEnabled.Checked)
                    {
                        Log($"Handle: {handleK:X8}\tName: {nameK}\tKeyCode: {keyboard.Keyboard.VirutalKey}\tScanCode: {keyboard.Keyboard.ScanCode}\tKey: {(Keys)keyboard.Keyboard.VirutalKey}\tFlags: {keyboard.Keyboard.Flags}");
                    }

                    break;

                case RawInputHidData hid:
                    Log(hid.Hid.ToString());
                    break;
                }
            }
            // Device added or removed event
            else if (m.Msg == WM_DEVICECHANGE)
            {
                if ((int)m.WParam == DBT_DEVNODES_CHANGED)
                {
                    DeviceListUpdate();
                }
            }

            base.WndProc(ref m);
        }