Zac
Zac

Reputation: 2279

c++ KeyLogger not logging first shift key or control key press

I'm writting a key logger but not for malicious purposes. Its actually to create a file that is then read "polled" by an xsplit broadcast program pluging which shows my keypresses on screen while I broadcast my screen.

Its working fine but the problem is the shift and control keys are not showing up.

This is because the save function isn't being called when these buttons are pressed initially as it waits to see if I just want a capital letter or similar.

I really want to call the save function immediately on the button press but not sure how to do this.

#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <winuser.h>
#include <stdio.h>

using namespace std;

/*
 * a program to log keys to file
 */

int Save (int key_stroke, char *file);


int main(int argc, char** argv) {

    char i;
    while (1) 
    {
    for (i =8; i <= 190; i++)
    {
        if (GetAsyncKeyState(i) == -32767)
            Save (i, "LOG.TXT");
    }
    }

    return 0;
}

/********************************************************************************/
/********************************************************************************/

int Save (int key_stroke, char *file)
{
    FILE *OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "w");
    fprintf(OUTPUT_FILE, "%s", "<xsplit>");

    cout << key_stroke << endl;
    if (key_stroke == 8)
        fprintf(OUTPUT_FILE, "%s", "[Backspace]");
    else if (key_stroke == 13)
        fprintf(OUTPUT_FILE, "%s", "[Return]");
    else if (key_stroke == 32)
        fprintf(OUTPUT_FILE, "%s", "[Space]");
    else if (key_stroke == VK_SHIFT)
        fprintf(OUTPUT_FILE, "%s", "[Shift]"); 
    else if (key_stroke == VK_ESCAPE)
        fprintf(OUTPUT_FILE, "%s", "[Escape]");
    else if (key_stroke == VK_CONTROL)
        fprintf(OUTPUT_FILE, "%s", "[Control]");
    else if (key_stroke == VK_END)
        fprintf(OUTPUT_FILE, "%s", "[END]");
    else if (key_stroke == VK_HOME)
        fprintf(OUTPUT_FILE, "%s", "[HOME]");
    else if (key_stroke == 1)
        fprintf(OUTPUT_FILE, "%s", "[LMOUSE]");
    else if (key_stroke == 2)
        fprintf(OUTPUT_FILE, "%s", "[RMOUSE]");    


    else
   fprintf(OUTPUT_FILE, "%s", &key_stroke);   

   fprintf(OUTPUT_FILE, "%s", "</xsplit>");

fclose(OUTPUT_FILE);
return 0;
}

/********************************************************************************/
/********************************************************************************/

The created file is constantly being rewritten over and replaced with new key presses if you want to test it out its best just to replace the "w" with "a+" on the fopen() function.

Upvotes: 0

Views: 1620

Answers (3)

Michael Haephrati
Michael Haephrati

Reputation: 4225

I hope this article of mine might be of help to you http://www.codeproject.com/Articles/635134/Target-Eye-Revealed-part-4-Keyboard-Capturing

regards,

Michael Haephrati

Upvotes: 0

George
George

Reputation: 21

Change:

for (i =8; i <= 190; i++) 

to:

for (i =0; i <= 255; i++)    

Upvotes: 2

paddy
paddy

Reputation: 63471

I would be using an event loop for this, and processing WM_KEYDOWN and WM_KEYUP messages. That'll also avoid pushing one of your cores to 100% with that busy-loop in main().

Upvotes: 0

Related Questions