Results 1 to 2 of 2
  1. #1
    d.vel.oper's Avatar
    Join Date
    Oct 2007
    Gender
    male
    Location
    d3d8.dll->Direct3DCreate8
    Posts
    133
    Reputation
    12
    Thanks
    13

    [Source] WH_KEYBOARD Hook Hotkeys

    First off, let me explain why you'd want to use this rather than GetAsyncKeyState(). I used to use GetAsyncKeyState all the time, however, the possibility of figuring out if a key was really 'pressed' (not held down, not up) took far too much work (keeping track of keystates yourself, it's very hackish). Using this method you can notify your application when a key is 'pressed' (down and then up), when a key is down (held down) or when a key is up (released).

    Without further delay, here is the source.

    HotkeyListener.h
    Code:
    #pragma once
    
    extern HHOOK hHotkeyHook;
    LRESULT CALLBACK HotkeyMessageProc(int code, WPARAM wParam, LPARAM lParam);
    
    void InstallHotkeyHook();
    void RemoveHotkeyHook();
    HotkeyListener.cpp
    Code:
    #include "stdafx.h"
    #include "HotkeyListener.h"
    
    HHOOK hHotkeyHook;
    
    #define HK_IS_PRESSED ((DWORD)lParam & 0x40000000)
    #define HK_IS_HELD ((DWORD)lParam & 0x40000000)
    #define HK_IS_RELEASED (!((DWORD)lParam & 0x40000000))
    
    LRESULT CALLBACK HotkeyMessageProc(int code, WPARAM wParam, LPARAM lParam)
    {
    	if (code > 0) return CallNextHookEx(hHotkeyHook, code, wParam, lParam);
    
    	else if (code == HC_ACTION)
    	{
    		if (HK_IS_PRESSED)
    		{
    			// Key was pressed, VK_KEY code is in wParam.
    		}
    		else if (HK_IS_HELD)
    		{
    
    		}
    		else if (HK_IS_RELEASED)
    		{
    
    		}
    	}
    
    	return CallNextHookEx(hHotkeyHook, code, wParam, lParam);
    }
    
    void InstallHotkeyHook()
    {
    	hHotkeyHook = SetWindowsHookEx(WH_KEYBOARD, HotkeyMessageProc, GetModuleHandle(NULL), GetCurrentThreadId());
    }
    
    void RemoveHotkeyHook()
    {
    	UnhookWindowsHookEx(hHotkeyHook);
    }
    Example DLLMain.cpp
    Code:
    #include "stdafx.h"
    #include "HotkeyListener.h"
    
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
    					 )
    {
    	if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    	{
    		InstallHotkeyHook();
    	}
    	else if (ul_reason_for_call == DLL_PROCESS_DETACH)
    	{
    		RemoveHotkeyHook();
    	}
    
        return TRUE;
    }
    Code should be relatively self explanatory.
    Questions? Ask them.
    Quote Originally Posted by Bury Your Dead
    You say he lost his heart that night,
    He never had one anyway.
    You say your drinking starts the fights,
    Well I'm digging his fucking grave.

  2. The Following 3 Users Say Thank You to d.vel.oper For This Useful Post:

    oilu (05-26-2008),pbsucks (11-03-2007),Taranis (02-20-2008)

  3. #2
    c0lo's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    1
    How can make SetWindowsHookEx for other aplication mmm draw line and read pixel?

    You can talk me pm.

Similar Threads

  1. crossfire wallhack source for hook
    By GangsterCode in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 9
    Last Post: 10-04-2010, 06:47 AM
  2. HotKey hack source
    By powerfear in forum Combat Arms Hack Coding / Programming / Source Code
    Replies: 22
    Last Post: 04-23-2010, 01:44 AM
  3. [GUIDE]Hooking the source engine.
    By Hell_Demon in forum C++/C Programming
    Replies: 15
    Last Post: 02-23-2010, 04:33 PM
  4. [Source]Vtable Hook?
    By SoreBack in forum Combat Arms Hacks & Cheats
    Replies: 17
    Last Post: 01-29-2009, 11:46 AM
  5. Counter Strike Source Killa Hook
    By chrisisfat in forum CounterStrike (CS) 1.6 Hacks / Counter Strike: Source (CSS) Hacks
    Replies: 0
    Last Post: 12-15-2008, 05:51 AM