SHARE
Facebook X Pinterest WhatsApp

Keystroke Logging

Key Logger This article is about how to log keystrokes. There is an article on this topic, “Hooking the Keyboard,” already on CodeGuru. Regarding that key logger, it is a system-wide hook. But, that article is a little bit old, and says that if we need to install a system-wide hook, we have to make […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Dec 13, 2004
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Key Logger

This article is about how to log keystrokes. There is an article on this topic, “Hooking the Keyboard,” already on CodeGuru.

Regarding that key logger, it is a system-wide hook. But, that article is a little bit old, and says that if we need to install a system-wide hook, we have to make it in a shared DLL, but that it will divide our code into some pieces and it will be difficult to hide it in the system.

Windows 2000 onwards provides system-wide, low-level hooks. By using these hooks, we can have a system-wide hook in a single program/project. We can use this functionality to set a system-wide hook.

HHOOK
SetWindowsHookEx
(int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId);

idHook is the hook ID for which we want to hook. Windows supports the following hooks:

  • WH_CALLWNDPROC
  • WH_CALLWNDPROCRET
  • WH_CBT
  • WH_DEBUG
  • WH_FOREGROUNDIDLE
  • WH_GETMESSAGE
  • WH_JOURNALPLAYBACK
  • WH_JOURNALRECORD
  • WH_KEYBOARD
  • WH_KEYBOARD_LL
  • WH_MOUSE
  • WH_MOUSE_LL
  • WH_MSGFILTER
  • WH_SHELL
  • WH_SYSMSGFILTER

The lpfn parameter is a function pointer to a callback function that we want to invoke after getting the event.

hMod is the handle of the current application. It is different from the window handle; it can be retrieved by calling the AfxGetInstanceHandle function of MFC.

dwThreadId,

This specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

I am enclosing a running example for hooking the keyboard.

Recommended for you...

How To Make Windows 11 Faster
Enrique Stone
Nov 30, 2022
Working with the Windows Registry in C#
Joydip Kanjilal
Jun 17, 2022
Using Multiple Programming Languages to Create an ASP.NET Website
Tariq Siddiqui
Jun 11, 2022
Finding a Microsoft Office version with .NET
Hannes DuPreez
May 20, 2022
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.