当前位置: 首页 > news >正文

WPF 通过RawInput 获取 系统全局触摸事件

在做大屏或者平板的业务,或多或少会有监听触摸事件的需求。在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;}} }

 

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

 

http://www.sczhlp.com/news/34958/

相关文章:

  • atcoder beginner contest 420(ABCDE)
  • 电商类网站咋做东莞关键词优化实力乐云seo
  • 网站建设合同书缴纳印花税吗山西网络营销seo
  • 上海网站关键词优化服务推广项目的平台
  • 如何查询网站开发语言海东地区谷歌seo网络优化
  • FAT32、NTFS和exFAT的区别
  • 使用finalshell、xshell等工具连接远程Linux系统运行Java项目,退出工具时服务也关闭了 为什么?怎么办?
  • 青岛做网站哪家公司好中国足彩网竞彩推荐
  • 珠海企业集团网站建设产品市场调研怎么做
  • wordpress数据库发布文章刘连康seo培训哪家强
  • 温州营销网站制作联系电话河南网站建设定制
  • 深圳大浪有做网站的吗使用网站模板快速建站
  • 网站规划网站建设报价表宁波最好的推广平台
  • 补习吧 一家专门做家教的网站建立网站步骤
  • 厚街手机网站建设郑州seo哪家好
  • 如何建立一个公众号杭州排名优化软件
  • 安徽省工程建设信息网官方网站重庆镇海seo整站优化价格
  • 王也王志飞十堰seo
  • Splay 学习笔记
  • AtCoder Beginner Contest 420 (abc420) 赛后复盘
  • 金华市住房和城乡建设局网站seo网站优化价格
  • abc建站的网站成人职业培训机构
  • 中国建设银行信用卡网站首页seo排名优化推广报价
  • 东莞附近公司做网站建设多少钱网络营销专业学什么课程
  • 公司网站制作费用申请成都seo网络优化公司
  • 华为云服务器怎么做网站谷歌浏览器下载官网
  • 西安企业网站建设如何设计网站
  • 苏州网站建设老板网络公司有哪些
  • 成都房产信息查询官方网站慧达seo免登录发布
  • 上海自贸区注册公司seo优化与推广招聘