惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Security Affairs
PCI Perspectives
PCI Perspectives
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Privacy & Cybersecurity Law Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
V
Visual Studio Blog
博客园 - 聂微东
Scott Helme
Scott Helme
博客园 - 【当耐特】
K
Kaspersky official blog
Security Latest
Security Latest
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
博客园 - 叶小钗
C
Check Point Blog
V2EX - 技术
V2EX - 技术
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
T
Tor Project blog
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
雷峰网
雷峰网
博客园_首页
美团技术团队
Y
Y Combinator Blog
C
CERT Recently Published Vulnerability Notes
AWS News Blog
AWS News Blog
月光博客
月光博客
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
C
Cybersecurity and Infrastructure Security Agency CISA

博客园 - wuty007

C# 范围运算符 C# 调用WGC 实现桌面屏幕的捕获 完善基于WPF开发的标尺控件(含实例代码) C# 依赖注入 Microsoft.Extensions.DependencyInjection 实现 控制反转(IOC) C# 获取Windows系统的设备名称 记录 Windows系统开启hyper-v ,部分端口被保留,导致端口不能使用而报错的问题 WPF 调用 Win32的SetWindowDisplayAffinity 函数 实现捕获屏幕时,过滤指定的窗口 记录WPF 在清单列表设置了UIACESS为true,没有签名的报错“从服务器返回了一个参照” WPF 的ListBox 去除默认的Item项的 鼠标hover的背景颜色 WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离 记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题 记录 使用PsExec启动System权限的WPF 程序 记录 命令行的 findstr 的使用 排查Windows 下的内存使用率过高,但是任务管理器看不到进程 Everything 支持 多实例 运行 指定应用 在 控制面板或者 设置的安装应用 置灰或隐藏卸载按钮 C# 定时任务 Quartz.NET 的使用 记录一下Windows系统下的命令行参数的字符个数限制 WPF 实现支持动态调整高度的文本显示控件
WPF 通过RawInput 获取 系统全局触摸事件
wuty007 · 2025-08-24 · via 博客园 - wuty007

在做大屏或者平板的业务,或多或少会有监听触摸事件的需求。在WPF 上,监听自身应用的触摸事件是很简单的,可以监听 Windows的 Stylus、Touch、甚至是 Mouse的事件来实现业务逻辑处理。但是如果要监听窗口外的触摸事件,可以使用RawInput.Sharp

1、注册触摸屏和笔的设备:

  RawInputDevice.RegisterDevice(HidUsageAndPage.TouchScreen,
                RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.DevNotify, hwnd);
            RawInputDevice.RegisterDevice(HidUsageAndPage.Pen,
                RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.DevNotify, hwnd);

2、钩子函数获取 带Hid类型的设备

 case RawInputHidData hid:
                        Debug.WriteLine($"Hid {hid.Hid}");
                        text = @$"DevicePath: {hid.Device.DevicePath}
VID:{hid.Device.VendorId:X2} PID:{hid.Device.ProductId:X2}
RawData:{hid.Hid}";

                        Console.WriteLine(text);
                        if (hid is RawInputDigitizerData rawInputDigitizerData)
                        {
                            foreach (var rawInputDigitizerContact in rawInputDigitizerData.Contacts)
                            {
                                text += rawInputDigitizerContact.ToString() + "\r\n";
                            }
                        }
                        Console.WriteLine(text);
                        break;

3、完整代码如下:

using Linearstar.Windows.RawInput;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RawInputTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;

            
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var windowInteropHelper = new WindowInteropHelper(this);
            var hwnd = windowInteropHelper.Handle;

            // Get the devices that can be handled with Raw Input.
            var devices = RawInputDevice.GetDevices();
RawInputDevice.RegisterDevice(HidUsageAndPage.TouchScreen, RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.DevNotify, hwnd); RawInputDevice.RegisterDevice(HidUsageAndPage.Pen, RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.DevNotify, hwnd); HwndSource source = HwndSource.FromHwnd(hwnd); source.AddHook(Hook); } private string text; 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.Mouse}"); break; case RawInputKeyboardData keyboard: Debug.WriteLine(keyboard.Keyboard); break; case RawInputHidData hid: Debug.WriteLine($"Hid {hid.Hid}"); text = @$"DevicePath: {hid.Device.DevicePath} VID:{hid.Device.VendorId:X2} PID:{hid.Device.ProductId:X2} RawData:{hid.Hid}"; Console.WriteLine(text); if (hid is RawInputDigitizerData rawInputDigitizerData) { foreach (var rawInputDigitizerContact in rawInputDigitizerData.Contacts) { text += rawInputDigitizerContact.ToString() + "\r\n"; } } Console.WriteLine(text); break; } } return IntPtr.Zero; } } }

Demo 链接:wutyDemo/RawInputTest at main · wutangyuan/wutyDemo 

参考资料:熙哥的WPF 通过 RawInput 获取触摸消息 - lindexi - 博客园