是否有本机 .NET 方法或与 user32.dll 的 GetKeyboardState 等效的方法?

发布于 2024-09-08 08:01:40 字数 772 浏览 10 评论 0原文

答案:此方法没有本机管理的等效方法。不过,可以在 pinvoke.net 找到一个很好的托管代码 API 示例。

大家好,

我有一个接受“跟踪模式”标志的应用程序。跟踪模式打开包含文本框的窗体。文本框通过自定义 TraceListener 对象显示输出。如果命令行上有 -t,跟踪窗口将从其自己的低优先级线程中打开。对于在出现问题时尝试诊断应用程序的顾问来说,这非常方便。

一些顾问要求在启动过程中按一些晦涩的组合键应该打开跟踪模式,这样他们就不必每次都进入命令行来激活它。这是一个合理的要求。

我不想为此给他们一个按钮或菜单项,因为在实例化和初始化各种组件时,在创建主窗体之前,顾问所关心的许多关键信息都会在启动时显示到 Trace 中。

我认为在 .NET 中检测当前键盘状态应该足够简单 - 但似乎在我看到的所有地方,建议都是使用 [DllImport("user32.dll", EntryPoint = "GetKeyboardState", SetLastError = true) ] 作为检测正在发生的情况的方法。

一切都很好——我对此感到满意。然而,作为一般经验法则,在尝试推出自己的功能之前,我会尝试确保对 Windows 功能使用本机 .NET 托管代码。

是否有本机 .NET 方法或与 user32.dll 的 GetKeyboardState 等效的方法?

ANSWER: There isn't a natively managed equivalent for this method. However, a good example managed code API can be found over at pinvoke.net.

Hi all

I have an application that accepts a flag for 'trace mode'. Trace mode turns on a form containing a text box. The text box displays output via a custom TraceListener object. If there is a -t present on the command-line, the trace window will be opened from within its own low-priority thread. It's handy for consultants that are trying to diagnose the application when something goes wrong.

A few consultants have requested that pressing some obscure key combination during startup should turn on trace mode so they don't have to go to the command line to activate it every time. It's a reasonable request.

I don't want to give them a button or menu item for this because a lot of the critical information that the consultants will care about is displayed to Trace at startup before the main form is created as the various components are instantiated and initialized.

I thought it should have been simple enough to detect the current keyboard state in .NET - but it seems that everywhere I look, the advice is to use [DllImport("user32.dll", EntryPoint = "GetKeyboardState", SetLastError = true)] as the method to detect what's going on.

That's all well and good - I'm comfortable with that. However, as a general rule of thumb I try to make sure I use native .NET managed code for Windows functions before I go trying to roll my own.

Is there a native .NET method or equivalent to user32.dll's GetKeyboardState?

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

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

发布评论

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

评论(2

难得心□动 2024-09-15 08:01:40

Control.ModiferKeysControl 的静态属性,当前按下了修改键(如果有)。我不知道有什么可以获取一般键盘状态,似乎大多数代码 P/Invoke 都使用 GetKeyboardStateGetKeyState 来检查特定键。

Control.ModiferKeys is a static property of Control which, if any, modifier keys are currently pressed. I'm not aware of anything to get general keyboard state, seems like most code P/Invoke with either GetKeyboardState or GetKeyState for checking specific keys.

淡水深流 2024-09-15 08:01:40

您确定 PreviewKeyDownEventArgs 没有您需要的内容吗?如果是这样,那么您可以使用 PreviewKeyDown 事件。

另一种选择可能是加速键。 Windows API 函数可以使用应用程序资源中的快捷键表将键盘序列映射到命令,其中命令与菜单项的作用相同;你知道,一个 WM_COMMAND 消息。我知道使用 C++ 在 MFC 中很容易做到这一点,但是当我试图找到 C# 方法来做到这一点时,我迷失了方向。如果您能弄清楚如何使用 C# 来执行加速键,那么它可能是一个灵活的解决方案。

Are you sure that PreviewKeyDownEventArgs does not have what you need? If it does, then you can use the PreviewKeyDown event.

Another option might be accelerator keys. A Windows API function can use an accelerator table in the application's resources to map keyboard sequences to commands, where a command is the same thing that a menu item does; you know, a WM_COMMAND message. I know it is easy to do in MFC using C++ but when I tried to find the C# way to do it, I got lost. If you can figure out how to do accelerator keys using C# then it could be a flexible solution.

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