Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hooking into keyboard to log user activity

Posted on 2004-09-28
3
Medium Priority
?
678 Views
Last Modified: 2013-12-03
I would like to write a proram that logs every keystroke. I've read enough to know that I have to use keyboard hooks, but do not know how to do this. I need a good easy-to-understand explaination and possibly some sample code on this.

I'd like to write this in C#, but am very well versed in vb & vb.net.
0
Comment
Question by:apm825
3 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 2000 total points
ID: 12282280
Below is a C program that does what you need - just replace the 'printf' with log output code.

I don't know C# I'm afraid.  8-(  At http://www.codeproject.com/csharp/NetWin32Hooks.asp is an article on implementing hooks in C#, with source code for a hook library.  Between that article and this code, you should find what you need.

Write this code into a file called keylogger.c, then at a command prompt do:

cl keylogger.c -link -subsystem:console user32.lib
keylogger

Now whenever you press an alphabetical key in any application, keylogger will log that even to the command prompt(it only prints alphabetical keys to make it simple - it traps all keys, but just doesn't print the non-alphabetical ones).

This uses a low-level keyboard hook, which is not available on Win9x.  You'll need to use an ordinary WH_KEYBOARD hook, rather than a WH_KEYBOARD_LL, if you want your code to work on Win9x.  I believe the code in the article I mentioned above will support that.  (I didn't use WH_KEYBOARD in my example below because WH_KEYBOARD hooks need to live in a DLL, which complicates the example.  WH_KEYBOARD_LL hooks can live in the main executable.)

#define _WIN32_WINNT 0x0400
#include "windows.h"

HHOOK g_Hook;

LRESULT CALLBACK HookFn(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HC_ACTION)
    {
        KBDLLHOOKSTRUCT* pKHS = (KBDLLHOOKSTRUCT*) lParam;
        if (pKHS->vkCode >= 'A' && pKHS->vkCode <= 'Z' && !(pKHS->flags & LLKHF_UP))
        {
            printf("You pressed %c\n", pKHS->vkCode);
        }
    }

    return CallNextHookEx(g_Hook, nCode, wParam, lParam);
}

int main(void)
{
    HMODULE Module = GetModuleHandle(NULL);
    g_Hook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) HookFn, Module, 0);
    MessageBox(NULL, "Press OK to stop logging", "Key Logger", MB_ICONINFORMATION | MB_OK);
    UnhookWindowsHookEx(g_Hook);
    return 0;
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question