如何在C#中模拟按键?

发布于 2025-01-01 17:33:10 字数 273 浏览 5 评论 0原文

Google 提供了 this 但它似乎只适用于 WindowsForms 类。我对 C# 完全陌生。我想要制作的是一个监视事件的程序,然后(例如)就像按下了“H”键一样。我真的不想担心活动窗口是什么或任何东西,或者向应用程序发送击键,我只是希望程序表现得就像我实际按下了键盘上的“H”按钮一样。 SendKeys 类似乎在一般类中不起作用。我的处理方式完全错误吗?

Google gives this but it seems to only work for a WindowsForms class. I'm completely new to C#. What I'm trying to make is a program that monitors for an event, and then (for example), acts as if the 'H' key has been pressed. I don't really want to worry about what the active window is or anything, or sending a keystroke to an application, I just want the program to act as if I have physically pressed the 'H' button on my keyboard. The SendKeys class doesn't seem to work in a general class. Am I going about this completely the wrong way?

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

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

发布评论

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

评论(1

对你再特殊 2025-01-08 17:33:10

来自如何:在代码中模拟鼠标和键盘事件< /strong>

Windows 窗体提供了多个用于以编程方式模拟鼠标和键盘输入的选项。

模拟鼠标输入
模拟鼠标事件的最佳方法是调用引发您要模拟的鼠标事件的 OnEventName 方法。

模拟键盘输入
虽然您可以使用上面讨论的鼠标输入策略来模拟键盘输入,但 Windows 窗体还提供 SendKeys 类用于将击键发送到活动应用程序。

From How to: Simulate Mouse and Keyboard Events in Code:

Windows Forms provides several options for programmatically simulating mouse and keyboard input.

Simulating Mouse Input
The best way to simulate mouse events is to call the OnEventName method that raises the mouse event you want to simulate.

Simulating Keyboard Input
Although you can simulate keyboard input by using the strategies discussed above for mouse input, Windows Forms also provides the SendKeys class for sending keystrokes to the active application.

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