Results 1 to 8 of 8
  1. #1
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,706
    Reputation
    907
    Thanks
    7,295
    My Mood
    Mellow

    Keyboard class + a shitty hook.

    This isn't really "CA specific" as it'll work in any application that uses the Windows Message Queue. Basically it's a way to introduce "callback-oriented" keyboard commands; a nice alternative from everyone's spammed calls to "GetAsyncKeyState". I've included my basic Hook class too so I'll add a bit of explanation about that in case someone wants to rip it out and use it somewhere.

    For people that haven't ever done any Win32 Windows programming, essentially all Windows applications (GUI-subsystem, not console applications) implement a "message queue" at some level (yes even in the .NET framework, although you never really see this unless you're overriding the famous "WndProc" event). To read more about the Message Queue, check here.

    The Keyboard Class
    The way a window behaves depends on the messages it receives in its message queue (is the mouse down, it it in a draggable area..etc etc). A typical message loop is implemented something like this:

    Code:
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg); // dispatches the message to the window handler, typically WndProc
    }
    That's it! In DirectX games you may see a message loop that doesn't block so much, like the following:
    Code:
    // Enter the infinite message loop
    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        if(msg.message == WM_QUIT)
            break;
    
        // Run game code here
        // ...
        // ...
    }
    Source: DirectXTutorial.com | Lesson 4: The Real-Time Message Loop

    Either way, we see some common API calls here. Why is this relevant? Your keyboard messages also go through this message loop before being processed by the game. By hooking into one of these public API calls, you can intercept keyboard messages and do your own processing to respond to the user's input. This is what I've done.

    The hook is very simple:
    1) Call the real API to translate the message properly.
    2) Check if the MSG we received is relevant to us (a keyboard message)
    3) If yes to 2) construct the KBDLLHOOKSTRUCT and run the callbacks. If no to 2), skip this step and go to 4
    4) Reset the hook and return the real value of the TranslateMessage call.

    Easy peasy. There are only 3 methods you need to concern yourself with in the Keyboard class:
    Code:
    Keyboard::set();
    Keyboard:unset();
    Keyboard::add_callback();
    That's it. I've written the class so you can even specify member functions as a callback (i.e class methods)

    Your callback must have the following signature:
    Code:
    void __cdecl KeyboardProc(key_message msg, const KBDLLHOOKSTRUCT *pKeyData);
    An example of using the class is demonstrated below:
    Code:
    #include "keyboard.h"
    
    HANDLE	hMainThread = NULL;
    BOOL	bExiting	= FALSE;
    
    void __cdecl KeyboardCallback(key_message msg, const KBDLLHOOKSTRUCT *pKbData) {
    	if (msg == KEYDOWN) {
    		char szKey[2];
    		sprintf(szKey, "%c", (char)pKbData->vkCode);
    		MessageBoxA(NULL, szKey, "key pressed", MB_OK);
    		// ZOMG DO H4X HERE
    	}
    }
    
    DWORD WINAPI EntryProc(LPVOID) {
    	// add the custom callback, then set the hook
    	Keyboard::add_callback(&KeyboardCallback);
    	Keyboard::set();
    
    	while (!bExiting)
    		Sleep(150);
    
    	// module is being unloaded now, unset the hook to be as unintrusive as possible
    	Keyboard::unset();
    
    	return TRUE;
    }
    
    BOOL APIENTRY DllMain(HMODULE hThis, DWORD dwReason, LPVOID lpReserved) {
        // typical entry point code blah blah.
    	if (dwReason == DLL_PROCESS_ATTACH) {
    		hMainThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&EntryProc, NULL, 0, NULL);
    	}
    	else if (dwReason == DLL_PROCESS_DETACH) {
    		// flag the module as closing to exit the infinite loop
    		bExiting = TRUE;
    		WaitForSingleObject(hMainThread, 150);
    		CloseHandle(hMainThread);
    	}
    	return TRUE;
    }
    Super simple stuff.

    The Hook Class
    Alright to support this I needed to be able to hook into the API. I wrote a very simple Hook class to do this and it's included in the source code in the attachment. Unlike most hooks this is more OO, some people might not like this and deem it "inefficient" or whatever; I don't really give a fuck, replace my hook with your own, it's not very difficult.

    Basically I've implemented 3 "types" of hook into this. You can configure a bunch of shit in the header file to change the way that the Hook is implemented at runtime.

    Code:
    #	define JMP_HOOK			0
    #	define PUSHRET_HOOK		1
    #	define MOVJMP_HOOK		2
    
    #	define DEFAULT_HOOK		MOVJMP_HOOK
    //#	define VARIABLE_HOOKS 	// comment this #define out to get a single lean and mean default hook
    The first 3 defines need to remain exactly as they are. Touch them and any ensuing assfuckery is your own stupid fault.
    DEFAULT_HOOK specifies, you guessed it, the default hooking method to use. If you have VARIABLE_HOOKS #defined, this value is ignored.
    If you uncomment VARIABLE_HOOKS the Hook class will compile into a dynamic hooking beast, using 1 of the 3 available hooks each time the hook is reset.

    I've commented the source code for all the included files so I won't spend too much time going over how it all works and how to use it. Read the code comments, then see how I've hooked the TranslateMessage API in keyboard.cc to see how to properly use the Hook class.

    Hopefully someone pulls their head out of their ass and uses this, but I doubt it.

    Cheers,
    Jason
    <b>Downloadable Files</b> Downloadable Files

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  2. The Following 10 Users Say Thank You to Jason For This Useful Post:

    Dave's Sheep #3 (02-21-2013),demtrios (02-21-2013),[MPGH]Flengo (02-21-2013),G-Forc3 (12-04-2014),gibam761 (02-21-2013),kypk (06-10-2013),Otaviomorais (02-21-2013),pDevice (05-07-2013),R3d_L1n3 (02-21-2013),Saltine (02-21-2013)

  3. #2
    BACKD00R's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    Brazil
    Posts
    10,709
    Reputation
    1814
    Thanks
    31,799
    My Mood
    Drunk
    Approved!!!



     

    Skype : BACKD00R-MPGH

     

    • Contributor: October, 31th 2011
    • CA BR Minion: January, 03th 2012
    • CF AL Minion: April, 07th 2012
    • CA Minion: April, 15th 2012
    • CF Minion: July, 03th 2012
    • PB Minion: January, 25th 2013
    • AVA Minion : February, 02th 2013
    • Arctic Combat minion: April, 03th 2013
    • Warface Minion: April, 03th 2013

    • Minion + : July 08th 2012
    • Moderator : January 21th 2013
    • Global Moderator : August 1st 2013







  4. #3
    Flengo's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Ontario, Canada
    Posts
    15,695
    Reputation
    3319
    Thanks
    11,108
    My Mood
    Happy
    What happened to CBF writing a thread

    Really glad you released it, looks really nice.


    I Read All Of My PM's & VM's
    If you need help with anything, just let me know.
     
     
    VM | PM

    Publicist Since 04.04.2015
    Middleman Since Unknown - Unknown
    Global Moderator Since 08.01.2013
    Donator Since 05.29.2013

    Minion+ Since 04.18.2013

    District 187 Minion Since 04.04.2013
    Steam Minion Since 02.26.2013
    WarRock Minion Since 02.19.2013
    A.V.A Minion Since 02.13.2013
    DayZ Minion Since 01.21.2013
    Combat Arms Minion Since 12.26.2012
    Contributor Since 11.16.2012
    Member Since 05.11.2010


  5. #4
    Ch40zz-C0d3r's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Posts
    839
    Reputation
    44
    Thanks
    400
    My Mood
    Twisted
    Thanks, but I already knew this method
    Any idea how to Block input of ca?

    Progress with my game - "Disbanded"
    • Fixed FPS lag on spawning entities due to the ent_preload buffer!
    • Edit the AI code to get some better pathfinding
    • Fixed the view bug within the sniper scope view. The mirror entity is invisible now!
    • Added a new silencer for ALL weapons. Also fixed the rotation bugs
    • Added a ton of new weapons and the choice to choose a silencer for every weapon
    • Created a simple AntiCheat, noobs will cry like hell xD
    • The name will be Disbanded, the alpha starts on the 18th august 2014



    Some new physics fun (Serversided, works on every client)



    My new AI
    http://www.youtube.com/watch?v=EMSB1GbBVl8

    And for sure my 8 months old gameplay with 2 friends
    http://www.youtube.com/watch?v=Na2kUdu4d_k

  6. #5
    ~FALLEN~'s Avatar
    Join Date
    May 2009
    Gender
    male
    Location
    devenv.exe
    Posts
    529
    Reputation
    23
    Thanks
    326
    My Mood
    Inspired
    I would recommend event based input achieved via raw input and AttachThreadInput. Good job though
    -Pyro

  7. #6
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,706
    Reputation
    907
    Thanks
    7,295
    My Mood
    Mellow
    Quote Originally Posted by Ch40zz-C0d3r View Post
    Thanks, but I already knew this method
    Any idea how to Block input of ca?
    You could hook DispatchMessage, filter the message coming in and then just return a garbage value without calling the real DispatchMessage. Haven't tested this but it should work.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  8. #7
    Ch40zz-C0d3r's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Posts
    839
    Reputation
    44
    Thanks
    400
    My Mood
    Twisted
    Quote Originally Posted by Jason View Post


    You could hook DispatchMessage, filter the message coming in and then just return a garbage value without calling the real DispatchMessage. Haven't tested this but it should work.
    They are using DirectInput, I tried hooking it but it resultwd in only no mouse movement on the y-axe

    Progress with my game - "Disbanded"
    • Fixed FPS lag on spawning entities due to the ent_preload buffer!
    • Edit the AI code to get some better pathfinding
    • Fixed the view bug within the sniper scope view. The mirror entity is invisible now!
    • Added a new silencer for ALL weapons. Also fixed the rotation bugs
    • Added a ton of new weapons and the choice to choose a silencer for every weapon
    • Created a simple AntiCheat, noobs will cry like hell xD
    • The name will be Disbanded, the alpha starts on the 18th august 2014



    Some new physics fun (Serversided, works on every client)



    My new AI
    http://www.youtube.com/watch?v=EMSB1GbBVl8

    And for sure my 8 months old gameplay with 2 friends
    http://www.youtube.com/watch?v=Na2kUdu4d_k

  9. #8
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,706
    Reputation
    907
    Thanks
    7,295
    My Mood
    Mellow
    Quote Originally Posted by Ch40zz-C0d3r View Post
    They are using DirectInput, I tried hooking it but it resultwd in only no mouse movement on the y-axe
    If they do use DirectInput, you'll need to hook whatever functions DirectInput use for processing the input. I don't know off the top of my head what those would be.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

Similar Threads

  1. [Source Code] Basic Keyboard Hooks
    By Kantanomo in forum C# Programming
    Replies: 0
    Last Post: 05-25-2011, 06:20 PM
  2. Replies: 7
    Last Post: 07-25-2010, 10:31 AM
  3. [Vb.net] Keyboard Hook?
    By ppl2pass in forum Visual Basic Programming
    Replies: 2
    Last Post: 07-11-2010, 06:19 PM
  4. [Help]Keyboard,SendKeys,Hook[Solved]
    By Qizzle15401 in forum Visual Basic Programming
    Replies: 6
    Last Post: 05-08-2010, 09:07 AM
  5. Keyboard hook in C++
    By B1ackAnge1 in forum C++/C Programming
    Replies: 15
    Last Post: 11-24-2009, 03:20 AM