0

I need to write a Windows application which monitors keystrokes regardless of focus. When it detects a particular keystroke (Ctrl + V, specifically), it needs to perform certain actions.

How can I monitor keystrokes in Windows from C# regardless of focus?

Gabe
  • 84,912
  • 12
  • 139
  • 238
Ozzah
  • 10,631
  • 16
  • 77
  • 116
  • Have a look here: http://stackoverflow.com/questions/1100285/c-how-to-detect-the-currently-pressed-key/1927644#1927644 – Papa Mufflon Jul 15 '11 at 06:00
  • I think the general term for this is **Keylogger** – V4Vendetta Jul 15 '11 at 06:00
  • Yip sounds like a kind of keylogger to me. I saw a very simple way to do this in python once, was only like 20 lines of code or something, but thats for another day. :) – Jethro Jul 15 '11 at 06:03
  • Check [my answer](http://stackoverflow.com/questions/5852455/background-listener/5852519#5852519) to a more specific version of your question. It shows full C# code for a key logger. – Ergwun Jul 15 '11 at 06:17

3 Answers3

2

I am not fully understand your question, but If you would like to register global key regardless of your window focus you can use RegisterHotKey windows API.

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • If I register [Ctrl+V] using RegisterHotKey, then will the user still be able to use this keyboard shortcut to past from the clipboard in other applications or will my program intercept it? The reason I'm writing this is so that the program will automatically queue the next item in the clipboard so the user just has to press [Ctrl+v] repeatedly (They are doing data entry from app-to-app). – Ozzah Jul 27 '11 at 01:05
  • Based on MSDN description of the RegisterHotKey it should work globally, so you should be able to queue all messages in your program even if it is not active. However I am not sure about Ctrl+V, since it is already registered in system. Just try. – Vlad Bezden Jul 27 '11 at 01:52
  • I tried and my application captures Ctrl+V and stops the focused window from receiving it, which is not what I want: http://stackoverflow.com/questions/6838540/c-detecting-ctrlv-with-registerhotkey-but-not-intercepting-it – Ozzah Jul 27 '11 at 02:12
1

A really easy way to do it is with GetASyncKeyState. I use it only for games but I think it would work here.

Import this:

[DllImport("user32.dll")]
static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey); 

Then you can just do (in a loop or timer)

if(GetAsyncKeyState(Keys.ControlKey) && GetAsyncKeyState(Keys.K))
{
 //DO SOME STUFF!!
}

If you need it to happen just once when it's pressed you can declare

bool oldK; //at class scope

then in your loop/timer

if(!oldK && GetAsyncKeyState(Keys.ControlKey) && GetAsyncKeyState(Keys.K))
{
     //DO SOME STUFF!!
}
oldK = GetAsyncKeyState(Keys.K);
Dmiters
  • 2,011
  • 3
  • 23
  • 31
  • This sounds like a way to tell if the key is currently pressed, not a way to be notified when it is pressed. – Gabe Jul 15 '11 at 06:44
0

checkout this article Detecting and recording key strokes in C#

you need to write the code into a class.Then add this class to a windows service.Instaniate into start() method of windows service.olace the buffering code into some timer e.g

     this.timerBuffFlush = new System.Timers.Timer();
        this.timerBuffFlush.Enabled = true;
        this.timerBuffFlush.Elapsed += new System.Timers.ElapsedEventHandler(this.timerBuffFlush_Elapsed);
        this.timerBufferFlush.Interval = 60000;
    }
Zara_me
  • 268
  • 3
  • 17