Solved

Hooking into keyboard to log user activity

Posted on 2004-09-28
3
665 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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