0

Is there anyway for a C# console application to capture keystroke, including those that are pressed outside of the program, similar to a key logger? The one I found and tried is Console.ReadKey() but it can only read the keystroke sent to the console.

IcySnow
  • 851
  • 2
  • 14
  • 23
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/7283397/how-to-use-console-windows-app-without-focus-on-it – raveturned Mar 23 '12 at 15:11
  • From his followup questions, I think this is more like http://stackoverflow.com/questions/177856/how-do-i-trap-ctrl-c-in-a-c-sharp-console-app – Tremmors Mar 23 '12 at 15:26

4 Answers4

0

You'd have to handle global keyboard events. Have a look at A Simple C# Global Low Level Keyboard Hook

Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
0

You need to use a low level keyboard hook. Here is an example using windows forms, but you can easily apply the same concept to a console application:

http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

Hope it helps!

Reinaldo
  • 4,556
  • 3
  • 24
  • 24
  • Can it help me capture key combination, like Ctrl + C or Ctrl + Alt + Delete? – IcySnow Mar 23 '12 at 15:18
  • Unfortunately to handle Ctrl + Alt + Delete you have to write your own GINA implementation in C or C++ http://msdn.microsoft.com/en-us/library/aa380543%28VS.85%29.aspx – Reinaldo Mar 23 '12 at 15:48
  • Thanks. I have something else to ask though: How can I make it so that if the user holds down a certain button for a while, instead of showing that button many times in a row (like "AAAAAAAAAAAAAAA") it will only show once (show "A" only once). – IcySnow Mar 24 '12 at 11:17
0

You will need to use the Windows API, I'd check GetAsyncKeyState.

Nate
  • 30,286
  • 23
  • 113
  • 184
-1

yes...you'll need to look at system keyboard hooks...I think this post has what you need:

Global keyboard capture in C# application

Community
  • 1
  • 1
Timmerz
  • 6,090
  • 5
  • 36
  • 49