| 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
|
Posted: Thu Apr 17, 2008 2:28 pm Post subject: Hook Key events in a single application. |
|
|
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  _________________
Mes'a back!
 |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
i__h4x Grandmaster Cheater Supreme
Reputation: 0
Joined: 09 Jun 2007 Posts: 1005 Location: i__<3's__wpe
|
Posted: Thu Apr 17, 2008 2:54 pm Post subject: |
|
|
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  _________________
Mes'a back!
 |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Apr 17, 2008 3:03 pm Post subject: |
|
|
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 |
|
 |
i__h4x Grandmaster Cheater Supreme
Reputation: 0
Joined: 09 Jun 2007 Posts: 1005 Location: i__<3's__wpe
|
Posted: Thu Apr 17, 2008 3:56 pm Post subject: |
|
|
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  _________________
Mes'a back!
 |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Apr 17, 2008 4:27 pm Post subject: |
|
|
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 |
|
 |
|