-1

I have a tray application (written in C#) that controls (starts/stops) the monitoring of my activity on the desktop. Monitored activity is logged into a log file. Atm whenever a file is accessed, information abt editing gets logged based on changes to the current file"s Length. I would like to use a cleaner approach, based on whether keystrokes were recorded while the window was in focus. Thanks in advance.

karlu
  • 3
  • 3

2 Answers2

1

I think what you are looking for is described in this article. The code installs and uninstalls the hook when your application starts/ends and calls Console.WriteLine((Keys)vkCode) every time a key is pressed. You can change that point in the code; vkCode contains the virtual key code of the pressed key. You can convert it by casting it to System.Windows.Forms.Keys and then using System.Windows.Forms.KeysConverter.

0

It is not possible in C# only, you will need an unmanaged dll to install a Windows hook (SetWindowsHookEx) and inject that dll into target processes.

W.Schmerz
  • 97
  • 6
  • I found the following [thread](http://stackoverflow.com/questions/3312752/capturing-mouse-keyboard-events-outside-of-form-app-running-in-background) which says its possible using **global hook**. This [article](http://www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET) also refers to global hooks, however installing and uninstalling a hook is performed through a form. In my case I do not have a form and would like keyboard activity on every focused window to be captured. – karlu Apr 12 '13 at 12:07
  • It is definitely possible using global hook. The only problem here is that the injected code for the global hook should reside in a separate unmanaged dll (unlike of the local hook, which might be done completely in managed code, but will intercept only the keystrokes sent to the application which installed the hook). It does not matter whether you use a visible form or an invisible one - the form there needed for a rudimentary inter-process communication. – W.Schmerz Apr 12 '13 at 13:10