Thread: C++ Global Keyboard Hook

  1. #1
    Registered User darknite135's Avatar
    Join Date
    Dec 2007
    Posts
    16

    Talking C++ Global Keyboard Hook

    Hey there. Right now I'm working on a C++ Keylogger (educational purposes) so I can better understand how to hook things. I want to create a global Keyboard hook so I can read keystrokes that I press. How would I do this? Where is a good place to start? So far I know that I have to use the Virtual Key system if I want to single out a key like: VK_RETURN would be enter. Can anyone help me with this project? Thank you.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm.
    If you scour msn for SetWindowsHookEx I think you'll find plenty of information.
    I haven't used hooks very much so I don't remember how to do it.
    Just note that global hooks must be placed in a dll and will also slow the system down somewhat.
    Last edited by Elysia; 02-01-2008 at 06:40 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User darknite135's Avatar
    Join Date
    Dec 2007
    Posts
    16

    Talking

    Quote Originally Posted by Elysia View Post
    Hmmm.
    If you travel msn for SetWindowsHookEx I think you'll find plenty of information.
    I haven't used hooks very much so I don't remember how to do it.
    Just note that global hooks must be placed in a dll and will also slow the system down somewhat.
    They must be placed in a dll? So I compile a dll in C++ and declare it in another program?

    I've read somewhere that you can use Sleep(50); to speed it up a bit.

    EDIT: I searched around and found out the easy way to set up a global keyboard hook, now how would I make it display messages? And what is the different between a global and a thread hook?

    Thank you.
    Last edited by darknite135; 02-01-2008 at 06:40 PM. Reason: More content

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You will have to create both a dll and an application (exe) to make it work.
    Then link that dll you your application. Tutorials on that? Hmmm. There was some at the board before. Let's see if I can find it.
    And using Sleep with hooks is generally a bad idea.
    The problem is that Windows will call your program code for every keyboard message that gets passed around in the system. And your program must respond to them and pass on the hook and message to its next recipient.
    So sleeping is bad idea there.

    Ah, here it is.
    This applies mostly to Visual Studio:
    http://cboard.cprogramming.com/showthread.php?t=98191

    Quote from MSDN:
    The global hooks are a shared resource, and installing one affects all applications in the same desktop as the calling thread. All global hook functions must be in libraries. Global hooks should be restricted to special-purpose applications or to use as a development aid during application debugging. Libraries that no longer need a hook should remove its hook procedure.
    So essentially, global affects all applications in the system - it's global.
    Last edited by Elysia; 02-01-2008 at 06:49 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    http://cboard.cprogramming.com/showthread.php?t=98557

    Ok your other thread was about hacking a game and this thread is about hooking the keyboard?

    Sounds suspicious to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulate Keys with a Keyboard Hook
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 11-14-2008, 08:14 PM
  2. blocking or passing keys with global hook
    By pmouse in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2007, 02:54 PM
  3. Global hook only works on one window?
    By CornedBee in forum Windows Programming
    Replies: 5
    Last Post: 01-19-2004, 12:25 PM
  4. How to keep static data in a keyboard hook?
    By junbin in forum Windows Programming
    Replies: 1
    Last Post: 01-19-2003, 03:24 AM