Solved

Hooking into keyboard to log user activity

Posted on 2004-09-28
3
670 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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…

821 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