4

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.

Dpayne2841
  • 91
  • 1
  • 9
  • Basically you want to communicate between two running process. – Pavan Chandaka Oct 21 '16 at 04:13
  • Essentially yes, currently I have it set up so it takes the two running processes and gives them both names like so, Example1 and Example2. I want while someone is playing on Example1, my application will grab the keys its told to grab when they press them and send them to the background process Example2, so it can perform that keystroke on that process as well. – Dpayne2841 Oct 21 '16 at 04:15

2 Answers2

5

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.

Community
  • 1
  • 1
Dpayne2841
  • 91
  • 1
  • 9
  • This works but in my app the app stops listening to the keys after a couple presses. How often do you need to initialize those globalKeyboardHook/KeyEventHandlers? – Christopher Mar 23 '19 at 14:46
0

My suggestion is to go with "mapped memory" (in operating system concepts: shared memory)

First process creates (may be your check state program) creates a mapped memory and writes values to it.

All other game process reads the value from memory map created by first process.

Here is a very nice example regarding how to do it.

https://msdn.microsoft.com/en-us/library/dd267552(v=vs.110).aspx

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • 1
    My only issue with this method would be that it's writing to memory and I know some games out there like World of Warcraft for example. Use software to detect memory injection and places permanent bans on accounts for using it. I wouldn't want to be the reason someone gets banned on that aspect. I know they allow multiboxing but I don't know if they allow it through memory injection since a lot of people making programs to play the game for them that way. I could be mistaken and this might not be memory injection if I'm incorrect please let me know. – Dpayne2841 Oct 21 '16 at 04:23