Click here to Skip to main content
14,459,361 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello there, i want to write an application like hotkey helpers for games, for example, in Warcraft 3, i use numpad keys for some reason. Is it possible to send a "Numpad 1 key pressed" message to the OS by pressing the "X" key? I've been trying to do this using hooks but i couldnt figure out. I hope i was clear.

I've used this class for hooks : Global System Hooks in .NET[^]

Simply my code is like this :
globalKeyboardHook hook = new globalKeyboardHook(); // A Global Hook
       private void btnHook_Click(object sender, EventArgs e)
       {
           hook.hook(); // Set the hook
           hook.HookedKeys.Add(Keys.X); // Hook the X key
           // i removed other hooks purify the code, six hooks total.
           hook.KeyDown += new KeyEventHandler(hook_KeyDown); // add eventhandler
           Program.hookActive = true; // inform the program that hook is active.
           Guncelle(); // update form components (buttons enabling/disabling according to hook status etc.)
       }

       private void hook_KeyDown(object sender, KeyEventArgs e)
       {
           KeyEventArgs k = new KeyEventArgs(Keys.NoName);
           switch (e.KeyCode)
           {
               case Keys.X:
                   k = new KeyEventArgs(Keys.NumPad1);
                   OnKeyDown(k);
                   break;
               // i removed other cases to purify the code, six cases total.

           }
       }



Thanks,
Seckin.
Posted
Updated 30-Jun-11 3:29am
v2
Comments
Prerak Patel 30-Jun-11 8:10am
   
What you have tried so far?
Rate this:
Please Sign up or sign in to vote.

Solution 2

Sure it is possible. But instead of using hook in .NET, i would prefer using some good APIs instead.

The two APIs you want to use are GetASyncKeyState to catch key presses and keybd_event function to simulate key press. With the first API set up a thread with 1ms sleep and check for each key you want to check then simulate a keypress event by second API for each key catched.

However the key which pressed will NOT be ignored. I mean when you press X, it will be pressed and for example numlock 1 will be pressed too. Also the program have to be started as admin. Its a long and unrelated story dont ask why :)

Another way of doing this is low level keyboard hooking which is quite harder and for a small application this is not really necessary. But it allows the program to block key press events and to send another key instead. BTW it will also need admin privileges.

So google this APIs for further info. I am not available for example codes ATM sorry bout that :(

Regards
Gun Deniz AKKOC
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Get a handle to the windows where the event needs to be fired, and register this handle with the hot key. That's it you don't have to do listen to key press, the window will be notified when the hot key is pressed.

You might find the hot key function used in this article helpful.

OneNote Style Screen Capture Utility - with Preview and Auto Save options[^]

Hope this helps! Mark this as answer if this helps!


Vallarasu S.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Your main problem is going to be finding un-assigned keys, that is, keys not already used by the game. beyond that, how is pressing the X key going to be any better or less confusing than pressing the actual key that the X key simulates? That's just a bizarre train of thought as far as I'm concerned.
   
Comments
Mehmet Seçkin 30-Jun-11 8:17am
   
I just want to make the game think "numpad 1 is pressed!" by pressing another button just like X or ENTER or else. I've hooked the X key and told it "when its pressed go fire an event numpad1 pressed", my program fires the event but this only affects my program, not the game.
johannesnestler 30-Jun-11 8:19am
   
Try with a GLOBAL Hook: maybe this helps http://www.codeproject.com/KB/system/globalsystemhook.aspx
Mehmet Seçkin 30-Jun-11 8:21am
   
Thank you but i'm already using this class, downloaded from the link you've just given.
Rate this:
Please Sign up or sign in to vote.

Solution 4

the same problem that i have... but i used globalhook so that even though i have no form i only have the notific...

i even have suspend and resume keys.. its simple just register unregister vice versa...

still i got a problem cause when user presses ALT + Q = i cant use the SendKeys.Send("{NUMPAD7}"),

so the only solution that i would used would be keybd_event or SendKeys.Send("{HOME}"); or something the same as that... ill test this later...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100