Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Hook Key events in a single application.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
i__h4x
Grandmaster Cheater Supreme
Reputation: 0

Joined: 09 Jun 2007
Posts: 1005
Location: i__<3's__wpe

PostPosted: Thu Apr 17, 2008 2:28 pm    Post subject: Hook Key events in a single application. Reply with quote

As the title says, i was wondering how to hook the keyboard for just one application, rather that installing a low level keyboard hook that hooks the whole system.

Is there a way to do this?

Preferably in VB.net/C# but if its a different language that I can attempt to translate thats fine Smile

_________________
Mes'a back!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Apr 17, 2008 2:48 pm    Post subject: Reply with quote

Depends on the program and how it handles the keys. You could go through and hook the API that handles key events such as GetAsyncKeyState(), you could subclass the window proc of the window as well and handle the keyboard events yourself. Or of the program is a game, you might have to hook DirectInput to handle them.

Care to be a little more specific? Surprised

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
i__h4x
Grandmaster Cheater Supreme
Reputation: 0

Joined: 09 Jun 2007
Posts: 1005
Location: i__<3's__wpe

PostPosted: Thu Apr 17, 2008 2:54 pm    Post subject: Reply with quote

Its a game, Medal Of Honor Allied Assault.
Not sure what more I can add to this post. The game allows you to create bindings to buttons, however there are limits to the scripts that can be written, (Eg toggling something on then off in one press).

Ive programmed all the code to send the commands to the games console, I was now just looking for a slightly less global way to set up the HotKeys.

Hooking all the API's dosnt sound too fun Smile

_________________
Mes'a back!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Apr 17, 2008 3:03 pm    Post subject: Reply with quote

I would assume probably using DirectInput, but, subclass the window proc first and see if you can handle the key events that way. You can do that using SetWindowLongPtr.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
i__h4x
Grandmaster Cheater Supreme
Reputation: 0

Joined: 09 Jun 2007
Posts: 1005
Location: i__<3's__wpe

PostPosted: Thu Apr 17, 2008 3:56 pm    Post subject: Reply with quote

Ok, ive been reading up on subclassing. All the examples I can find of cross process subclassing are in C++, would It be possible to do in the framework, or will i need a C++ dll to hook the process?

Any examples would be greatly appreciated

Thanks Smile

_________________
Mes'a back!
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Apr 17, 2008 4:27 pm    Post subject: Reply with quote

You can subclass it easily via injection, or doing it from another process. As I said above, use SetWindowLongPtr:

http://msdn2.microsoft.com/en-us/library/ms644898.aspx

Code:
LONG_PTR SetWindowLongPtr(     
    HWND hWnd,
    int nIndex,
    LONG_PTR dwNewLong
);


The first param, hWnd, is the window handle to the window in question. You can obtain the handle through various methods, FindWindow or EnumerateWindows will do fine from an outside program.

The second param is the index of the type you want to set. In this case to subclass the wndproc you want to use GWL_WNDPROC.

The last param would be, in this case, a pointer to the new wndproc that will manage the window messages.

So you would setup something like this:

Code:

WNDPROC lpOriginalWndProc = NULL;
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

void Sethook()
{
   lpOriginalWndProc = (WNDPROC)(LONG_PTR)SetWindowLongPtr( -YourHwndVarHere-, GWL_WNDPROC, (LONG)(LONG_PTR)WndProc );
}

void RemoveHook()
{
   if( lpOriginalWndProc )
   {
      SetWindowLongPtr( -YourHwndVarHere-, GWL_WNDPROC, (LONG)(LONG_PTR)lpOriginalWndProc );
      lpOriginalWndProc = NULL;
   }
}


LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
   switch( uMsg )
   {
      // .. Handle Cases Here ..
   }
   return CallWindowProc( lpOriginalWndProc, hWnd, uMsg, wParam, lParam );
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites