WPF使用什么捕获鼠标和键盘输入? [英] What does WPF use to capture mouse and keyboard input?

查看:473
本文介绍了WPF使用什么捕获鼠标和键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个系统范围内使用 SetWindowsHookEx WH_MOUSE_LL 过滤某些鼠标单击。问题是它不适用于WPF应用程序(所有WPF应用程序都会检测鼠标单击,无论我是否已指示系统忽略这些单击)。我已经问过一个此处类似的问题,但是我假设WPF使用 DirectInput 而不是标准Windows消息来检测输入。



我已经找到了能够使用 SendMessage 将鼠标单击注入WPF应用程序。如果可以的话,我认为这意味着WPF不使用 DirectInput 进行鼠标输入。但是,为什么不能用 SetWindowsHookEx 阻止WPF应用程序检测鼠标单击?



尽管这个问题是主要是关于鼠标的输入,我也想知道它也可以用于键盘输入。



示例



<我已经快速创建了以下解决方案来重现WPF的异常行为。它由3个项目组成:




  • HookTester

    自动启动项目启动其他2个项目,因此您应该主要关注这一项目。在启动时安装鼠标钩子,在关闭表单时卸载钩子。


  • WinFormsTest

    包含具有默认上下文菜单的TextBox,您可以在其中测试鼠标右键。当HookTester运行时,您将不能使用鼠标右键调用上下文菜单。


  • WpfTest

    也包含一个TextBox,带有自定义上下文菜单(尽管我也可以使用默认菜单),因此这里还是测试鼠标右键的地方。只要HookTester正在运行,您就不能调用上下文菜单(使用鼠标右键),但是由于某种原因,菜单仍然会显示(为什么?)。




警告:运行解决方案时,HookTester项目将启动,并将立即安装该钩子以拒绝任何右击鼠标单击(系统范围)。您只需关闭HookTester表单即可轻松卸载该挂钩。谨慎测试。



下载SO5036143.ZIP: 镜像1 镜像2

解决方案


  • WPF窗口创建HwndSource
    (Window.CreateSourceWindow)。

  • HwndSource创建HwndWrapper
    (HwndSource.Initialize)。

  • HwndWrapper使用窗口
    过程创建Win32窗口,该过程将Windows
    消息委派给由
    HwndSource指定的挂钩。

  • 一个钩子是
    HwndSource.InputFilterMessage,其中
    将Windows消息委派给四个
    输入提供程序:手写笔,鼠标,
    键盘,appcommand 。

  • 提供程序解析
    适当的Windows消息,
    调用InputManager引发元素上的输入
    事件。



HwndMouseInputProvider处理诸如WM_MOUSEMOVE,WM_LBUTTONDOWN之类的消息。因此,我认为没有DirectInput用于处理鼠标和键盘输入。


I globally (system-wide) filter certain mouse clicks using SetWindowsHookEx and WH_MOUSE_LL. The problem is it doesn't work for WPF applications (all WPF applications detect mouse clicks whether or not I have instructed the system to ignore these clicks). I've already asked a similar question here, but I made an assumption that WPF uses DirectInput instead of standard Windows messages for detecting the input. But does it?

I've been able to find a code that was able to inject mouse clicks into WPF applications using SendMessage. If that's possible, then I think it somehow means WPF does not use DirectInput for mouse input. But then, why it's not possible to prevent WPF applications from detecting mouse clicks with SetWindowsHookEx?

Although this question is mainly about the mouse input, I would also like to know how it works for keyboard input as well.

Example

I've quickly created the following solution to reproduce the odd WPF behavior. It consists of 3 projects:

  • HookTester
    StartUp project, automatically launches other 2 projects, so you should be mainly concerned about this one. Installs mouse hook when launched, and uninstalls the hook when you close the form.

  • WinFormsTest
    Contains a TextBox with default context menu where you can test the right mouse button. When the HookTester is running, you should not be able invoke the context menu using right mouse button.

  • WpfTest
    Contains a TextBox as well, with custom context menu (although I could use default menu as well), so this is again the place to test right mouse button. You should not be able to invoke the context menu (using right mouse button) as long as HookTester is running, but for some reason, the menu will be showed anyway (Why???).

WARNING: When you run the solution, the HookTester project will start and will immediately install the hook to reject any right-buttom mouse clicks (system-wide). You can easily uninstall the hook simply by closing the HookTester form. Test with caution.

Download SO5036143.ZIP: mirror 1, mirror 2

解决方案

  • WPF Window creates HwndSource (Window.CreateSourceWindow).
  • HwndSource creates HwndWrapper (HwndSource.Initialize).
  • HwndWrapper creates Win32 window with window procedure that delegate Windows messages to hooks, specified by HwndSource.
  • One hook is the HwndSource.InputFilterMessage that delegate Windows messages to four input providers: stylus, mouse, keyboard, appcommand.
  • Provider parses appropriate Windows message and invokes InputManager to raise input events on elements.

HwndMouseInputProvider process messages such as WM_MOUSEMOVE, WM_LBUTTONDOWN, etc. So I think there is no DirectInput used to handle mouse and keyboard input.

这篇关于WPF使用什么捕获鼠标和键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆