zoukankan      html  css  js  c++  java
  • 2019-11-23-WPF-使用-RawInput-接收裸数据

    title author date CreateTime categories
    WPF 使用 RawInput 接收裸数据
    lindexi
    2019-11-23 16:38:27 +0800
    2019-11-23 16:27:42 +0800
    WPF

    在 Windows 提供很底层的方法接收硬件设备的裸数据,通过接收裸数据可以做到性能更高的全局键盘,还能支持多个鼠标。但是用这个方法需要自己解析裸数据,同时会因为接受到很多消息降低性能

    在微软官方很少有文档说如何使用Raw Input不过我在 github 上找到小伙伴的 rawinput-sharp: C# wrapper library for Raw Input 项目,简单通过 NuGet 安装就能使用

    使用 NuGet 安装 RawInput.Sharp 0.0.2 如果是新项目可以使用下面代码

            <PackageReference Include="RawInput.Sharp" Version="0.0.2" />

    在 MainWindows 注册事件,请看代码

            public MainWindow()
            {
                InitializeComponent();
    
                SourceInitialized += MainWindow_SourceInitialized;
            }
    
            private void MainWindow_SourceInitialized(object sender, EventArgs e)
            {
                var windowInteropHelper = new WindowInteropHelper(this);
                var hwnd = windowInteropHelper.Handle;
    
                // Get the devices that can be handled with Raw Input.
                var devices = RawInputDevice.GetDevices();
    
                // register the keyboard device and you can register device which you need like mouse
                RawInputDevice.RegisterDevice(HidUsageAndPage.Keyboard,
                    RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.NoLegacy, hwnd);
    
                HwndSource source = HwndSource.FromHwnd(hwnd);
                source.AddHook(Hook);
            }

    通过 RawInputDevice.GetDevices 可以知道当前可以注册的设备有哪些,使用 RawInputDevice.RegisterDevice 可以注册事件,这里注册的是键盘事件,小伙伴自己修改 HidUsageAndPage 的值可以注册不同的事件

    注册事件就可以在 Hook 函数接收到 WM_INPUT 消息,通过这个消息解析就可以拿到裸数据,对裸数据处理就可以收到输入,如果需要接入 WPF 可以使用WPF 模拟触摸设备将收到的消息模拟触摸

            private 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:
                            Debug.WriteLine(mouse.Mouse);
                            break;
                        case RawInputKeyboardData keyboard:
                            Debug.WriteLine(keyboard.Keyboard);
                            break;
                        case RawInputHidData hid:
                            Debug.WriteLine(hid.Hid);
                            break;
                    }
                }
    
                return IntPtr.Zero;
            }

    用 RawInput 就是通过

    本文的例子代码在 github 欢迎小伙伴访问

    现在这个项目只支持 dotnet standard 2.0 我将这个项目升级兼容 .NET 4.5 我提交了 MR 请看 Pull Request #3 rawinput-sharp 如何合并了就能兼容

    Using Raw Input

    About Raw Input

  • 相关阅读:
    pytest 框架生成 pytest
    pytest 之数据驱动参数化:pytest.mark.parametrize
    web 网站表单测试、搜索查询测试、删除测试、数据库测试知识点
    pytest 之 fixture 的前置后置功能
    pytest 框架之pytest-html报告生成
    Initialization of bean failed; nested exception is java.lang.
    使用jmeter测试Oracle&&MySQL数据库性能
    jmeter的日常特殊参数化
    压测出现各种奇葩问题,求围观
    weblogic性能调优
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085460.html
Copyright © 2011-2022 走看看