Results 1 to 7 of 7

Thread: [2.0] Detecting when a key is pressed even if the application is not in focus

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    [2.0] Detecting when a key is pressed even if the application is not in focus

    Hi. I'm trying to create a sort of keylogger application that records everything the user types on a computer.

    Now I have to add this feature onto a watchdog application that I developed as a window application hidden showing a notify icon.

    Now what i'm trying to do is when a key is pressed, add that key to a file that the watchdog will write to. I could do all of that but there is one thing missing.

    I could add a Key event handler so when a key is pressed, something is done e.g.:

    Code:
    private void AddEvent(Control control)
    {
    	control.KeyDown += new KeyEventHandler(control_KeyDown);
    
    	foreach (Control co in control.Controls)
    	{
    		AddEvent(co);
    
    	}
    
    }
    ...
    
    private void control_KeyDown(object sender, KeyEventArgs e)
    {
    	// User presses A, Key numeration code is 65.
    	if (e.KeyValue == 65)
    	{
    				MessageBox.Show("A");
    
    	}
    
    }
    This works fine, but since the watchdog is an application, this handler only kicks in when the application is on focus. The way it has to work is in the background even if the application is not in focus.

    Does anyone know how to do this?

    Jennifer

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Detecting when a key is pressed even if the application is not in focus

    You need to set a keyboard hook using the Windows API so that your app receives a message each time a key is pressed.

    http://search.microsoft.com/results....=keyboard+hook
    http://search.microsoft.com/results....-AU&FORM=QBME1

    Following the first link on that second page led me to this also:

    http://forums.microsoft.com/MSDN/Sho...14651&SiteID=1
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Detecting when a key is pressed even if the application is not in focus

    heh wow john i can't believe u replied to this post

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Detecting when a key is pressed even if the application is not in focus

    Quote Originally Posted by Fromethius
    heh wow john i can't believe u replied to this post
    It's not against the rules to assist if it's for a legit purpose and I think Jen is trustworthy.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Detecting when a key is pressed even if the application is not in focus

    Indeed. If a newcomer or someone with a dodgy reputation (like you Fromethius ) then I would have at least asked for an indication of a legit purpose. I guess you can never know for sure but if someone says that their app is going to sit in the system tray then they're unlikely to be a black hat.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Detecting when a key is pressed even if the application is not in focus

    heh yea I don't mind though. I'm only 13. I just come here, screw around, help people out. Besides, I already know how :P

  7. #7

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [2.0] Detecting when a key is pressed even if the application is not in focus

    Thank you jmcilhinney. I got it out and its working fine.

    Hi Penegate!!!! Happy new year, I hope 2007 brings a lot to all of you..... ESPECIALLY MEEEE!!!!!

    Jennifer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width