how to capture the keyboard input in windows service

Posted by Kasani007 under C# on 3/13/2018 | Points: 10 | Views : 4816 | Status : [Member] | Replies : 2
i am struggling a lot, to capture the key press input in Windows Service

i have KeystrokeAPI which works in console application,

i wrote the below code in OnStart() of the service

protected override void OnStart()
       
{
using (var api = new KeystrokeAPI())
           
{
               
System.Timers.Timer t = new System.Timers.Timer();
                t
.Elapsed += new ElapsedEventHandler(TimerCallback);                    
                t
.Interval = 30000;
                t
.Enabled = true;
                t
.AutoReset = true;
                api
.CreateKeyboardHook((character) => {
                   
// Console.Write(character);
                    count
++;

               
});
           
}
}

and Event Handler

 protected void TimerCallback(object sender, ElapsedEventArgs e)
   
{
       
// Display the date/time when this method got called.
       
//Console.WriteLine("In TimerCallback: " + DateTime.Now);
       
//Console.WriteLine("Count Of Keys: " + count);
       
StreamWriter sw = new StreamWriter(@"d:\ChaiKeyAPILog.txt", true);
        sw
.WriteLine();
        sw
.WriteLine("In TimerCallback: " + DateTime.Now);
        sw
.WriteLine("Count Of Keys: " + count);
        sw
.Close();
        count
= 0;
       
// Force a garbage collection to occur for this demo.
        GC
.Collect();
   
}

as i kept timer, so it diplays the datetime for every given interval of time, but it is not displaying the count of the pressed keys of the keyboard.

i tried it in writing this code in Main() also. but no use.

please help me.. to get the count of the pressed keys for every 1 minute of time In windows services




Responses

Posted by: Annaeverson on: 3/15/2018 [Member] Starter | Points: 25

Up
0
Down
cool, thanks a lot for submiting this one

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 3/28/2018 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
This looks like you are creating a Key-logger and a Virus as classified in most cases. be careful not to be caught i you are.

Thank you for posting at Dotnetfunda
[Administrator]

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response