Categories > Coding > C++ >

Systemwide key press hook in C++

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Posted

I made this quite a while ago, probably around the beginning of my C++ journey and while looking at my github repositories I found it and thought I should post it here as it's actually quite a cool beginner thing to make.

 

It creates a system-wide key press hook and outputs the key that was pressed along with its ASCII code ( in base 10 and 16 ).

 

I rewrote it and cleaned it up a bit since I made it a while ago and my code wasn't as clean.

 

github repository: https://github.com/expressiongz/kb-hook/blob/main/main.cpp

 

https://media.discordapp.net/attachments/901227777741692968/1006016162909388951/unknown.png

#include <iostream>
#include <Windows.h>

HHOOK hook = nullptr;

long __stdcall kb_hook( const std::int32_t n_code, const std::uint32_t kb_message, const long llhookstruct )
{
    const auto p_kbd_hook_struct = reinterpret_cast< KBDLLHOOKSTRUCT* >( llhookstruct );

    if ( n_code == HC_ACTION && kb_message == WM_KEYDOWN )
        std::printf( "Hooked Key Down: %c | %u | %x\n", p_kbd_hook_struct->vkCode, p_kbd_hook_struct->vkCode, p_kbd_hook_struct->vkCode );

    return CallNextHookEx(hook, n_code, kb_message, llhookstruct);
}

int main( )
{

    hook = SetWindowsHookExA( WH_KEYBOARD_LL, kb_hook, 0, 0 );

    MSG msg{ nullptr, 0, 0, 0, 0, { 0, 0 } };

    while ( !GetMessageA( &msg, 0, 0, 0 ) ) {
        TranslateMessage( &msg );
        DispatchMessageA( &msg );
    }
    UnhookWindowsHookEx( hook );
    return 0;
}

 

feel free to ask any questions

  • 0

https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png

Posts: 2014

Threads: 198

Joined: Apr, 2021

Reputation: 16

Replied

Vouch

Now i can make my own trojan hehe

  • 0

Random quote here...

Posts: 1316

Threads: 54

Joined: Jul, 2021

Reputation: 64

Replied

so basically a c++ keylogger?

  • 0

Mail me at sirweebdev@protonmail.com if you have any questions or you want to say anything to me, I'll reply (maybe).

Posts: 190

Threads: 19

Joined: Jul, 2021

Reputation: 6

Replied

sick release! 

  • 0

10 years ago

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )