简体   繁体   English

如何在C#中模拟按键?

[英]How to simulate Keystrokes in C#?

Google gives this but it seems to only work for a WindowsForms class. Google提供了此功能,但它似乎仅适用于WindowsForms类。 I'm completely new to C#. 我是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. 我要制作的是一个监视事件的程序,然后(例如)像按“ H”键那样工作。 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. 我真的不想担心活动窗口是什么或什么,也不必担心向应用程序发送击键,我只想让程序像实际按下键盘上的“ H”按钮一样起作用。 The SendKeys class doesn't seem to work in a general class. SendKeys类似乎在通用类中不起作用。 Am I going about this completely the wrong way? 我会完全以错误的方式去做吗?

From How to: Simulate Mouse and Keyboard Events in Code : 如何:在代码中模拟鼠标和键盘事件

Windows Forms provides several options for programmatically simulating mouse and keyboard input. Windows窗体提供了几个用于以编程方式模拟鼠标和键盘输入的选项。

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. 模拟鼠标事件的最佳方法是调用OnEventName方法,该方法引发要模拟的鼠标事件。

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. 尽管您可以使用上面讨论的用于鼠标输入的策略来模拟键盘输入,但是Windows Forms还提供了SendKeys类,用于将击键发送到活动应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM