Search code examples
c#windowsbackgroundkeystrokes

C# Detect Keystrokes and Send to Background Windows


I've looked around for six hours today in search of a method to complete the task I'm looking to accomplish. However with little luck every method I've tried has come out not working.

So the program I'm working on is a multiboxing application for video games. Essentially I want to have my created application running in the background. The user will check on checkbox's to state which keys they want to be captured, so not every key is being captured. Then while they are playing the main game, the application will send the keys that are checked to the games running in the background.

I've tried global hotkeys however never could get more than one key working. I've also tried to hook keys but for some reason, couldn't get that functional. I also dabbled into sendmessage with little luck there either.

Was just curious if anyone else had some ideas for going about doing this. To give an example of another program that does this same thing would be HotKeyNet, KeyClone, and ISboxer. I know there are more out there but that gives you an idea of what I'm trying to do with my application.


Solution

  • Alright, after quite a bit of research into different methods for sending keystrokes and reading keystrokes. I finally was able to splice to different types of coding together to provide the results I was looking for.

    I'm posting up the answer, so anyone that is looking for the answer to this question later down the road has it available to them.

    My two references for splicing this code together are the following: http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook

    Send combination of keystrokes to background window

    I used the global low level hook and postmessage for sending keystrokes to the background application.

    1. So first you will need to follow the instructions from the first link, to get the starting code working.

    2. Download the working source code from link one, and use the globalKeyboardHook.cs in your application.

    3. Then in references place the following:

    using System.Runtime.InteropServices; //Grabs for your DLLs

    using Utilities; //Grabs from the file you added to your application.

    1. Now you will want to place the following code in your class:

    globalKeyboardHook gkh = new globalKeyboardHook();

    [DllImport("user32.dll")] //Used for sending keystrokes to new window.

    public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)] //Used to find the window to send keystrokes to.

    public static extern IntPtr FindWindow(string className, string windowName);

    1. Now go ahead place your keystrokes you want to be grabbed this I found is best in Form1_Loaded:

    gkh.HookedKeys.Add(Keys.A);//This collects the A Key.

    gkh.HookedKeys.Add(Keys.B);//This collects the B Key.

    gkh.HookedKeys.Add(Keys.C);//This collects the C Key.

    gkh.KeyDown += new KeyEventHandler(gkh_KeyDown); //Event for pressing the key down.

    gkh.KeyUp += new KeyEventHandler(gkh_KeyUp); //Event for the release of key.

    1. After that you will want to go ahead and place in the following in your code as well:

    void gkh_KeyUp(object sender, KeyEventArgs e) //What happens on key release.

    {

    lstLog.Items.Add("Up\t" + e.KeyCode.ToString());

    e.Handled = false; //Setting this to true will cause the global hotkeys to block all outgoing keystrokes.

    }

    void gkh_KeyDown(object sender, KeyEventArgs e) //What happens on key press.

    {

    lstLog.Items.Add("Down\t" + e.KeyCode.ToString());

    e.Handled = false;

    }

    1. Once that is in place just put this little bit in the gkh_KeyDown to get your keystrokes to send to another window of your choosing:

    const uint WM_KEYDOWN = 0x100;

    IntPtr hWnd = FindWindow(null, "Example1"); //Find window Example1 for application.

    switch (e.KeyCode)

    {

    case Keys.A: //Makes it so it only sends that key when it's pressed and no other keys.

    if(chkA.Checked == true)

    {

    PostMessage(hWnd, WM_KEYDOWN, (IntPtr)(Keys.A), IntPtr.Zero); //Sends to key A to new window assigned hWnd which equals Example1.

    }

    break;

    }

    }

    The code that I have provided is setup so people can use checkbox's to tell the program which keys they want to send over to the second application.

    If you have any questions regarding to this post just let me know, and I will do my best to walk you through the process. Hope this helps someone out later down the road.