Solved

Hooking into keyboard to log user activity

Posted on 2004-09-28
3
661 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Mobile 6.5 device Apache Flex 3 44
Windows Drag & Drop Location 2 94
Fix insertion and shifting issue 5 60
Microsoft C++ code failing in executable that worked 9 85
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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now