如何从 .NET 2.0 程序集中挂钩 winforms 或 WPF 中的热键?

发布于 2024-08-14 02:13:14 字数 889 浏览 4 评论 0原文

我有一个面向 .NET 2.0 的程序集,以便与广泛的应用程序兼容。当与桌面应用程序(winforms 或 WPF)一起使用时,我想挂钩某些热键和弹出非模式对话框窗口。棘手的一点是找到一个可以在 .NET 2.0 下实现且与 WPF 兼容的解决方案。

对于winforms,我可以使用 RegisterHotKey 或实施IMessageFilter 并在 WM_KEYDOWN 中挂钩="http://msdn.microsoft.com/en-us/library/system.windows.forms.imessagefilter.prefiltermessage.aspx" rel="nofollow noreferrer">PreFilterMessage 方法,但它们不起作用与 WPF 配合良好。

对于 WPF,InputBinding 类非常有用很好,但这在 .NET 2.0 中不可用。

理想的解决方案是:

  • 构建面向 .NET 的程序集

  • 挂钩 WPF 或 Winforms 的击键

  • 提供应用程序范围,而不是系统范围

I have assembly that targets .NET 2.0 to be compatible with a broad range of applications. When used with a desktop application (either winforms or WPF) I want to hook certain hotkeys and popup modeless dialog windows. The tricky bit is finding a solution that can be implemented under .NET 2.0 that is compatible with WPF.

For winforms I could use RegisterHotKey or implement IMessageFilter and hook WM_KEYDOWN in the PreFilterMessage method, but they don't work well with WPF.

For WPF, the InputBinding class is very nice, but this isn't available in .NET 2.0.

An ideal solution would:

  • Build in an assembly targeting .NET

  • Hook keystrokes for either WPF or Winforms

  • Provide application-wide, not system-wide scope

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2024-08-21 02:13:14

您可以构建基于 Windows API(系统范围挂钩)的通用解决方案。 Don Esposito 就此写了一篇很棒的文章。这是一个链接

http://msdn.microsoft.com/en-us/magazine /cc188966.aspx

如果您不需要系统范围的挂钩,您也可以编写应用程序挂钩。这实际上更容易实现。我认为唐也涵盖了这一点。

You could build a universal solution based on Windows API (System wide hook). Don Esposito has written a great article on that. Here's a link

http://msdn.microsoft.com/en-us/magazine/cc188966.aspx

You could write an application hook too if you don't want a system wide hook. That's actually easier to implement. Don covers that too I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文