0

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?

user1002973
  • 2,088
  • 6
  • 22
  • 31
  • 1
    C# alone can't handle this. You need to get lower level than this. You can use pinvoke to call the necessary keyboard hooks. Here's an example (abandoned, I'm afraid, but the functionality to receive keyboard events is already there) -- https://github.com/eberle1080/Kaptivate – Chris Eberle Feb 02 '12 at 01:50
  • 1
    http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx – Mitch Wheat Feb 02 '12 at 01:54
  • [This question](http://stackoverflow.com/questions/1645815/how-can-i-programmatically-generate-keypress-events-in-c) includes answers referencing pretty much all of the variations I know. – M.Babcock Feb 02 '12 at 02:02
  • 3
    Some projects can be a really bad choice when you get started on programming. This is one of them. – Hans Passant Feb 02 '12 at 02:04
  • I answered for the same question in another page. Please check [here](http://stackoverflow.com/questions/1645815/how-can-i-programmatically-generate-keypress-events-in-c/12228166#12228166). Hope this helps. – Rajesh Sep 01 '12 at 13:33

1 Answers1

1

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.

Community
  • 1
  • 1
Raathigesh
  • 2,306
  • 3
  • 26
  • 32