0

I am trying to make a way of passing keyboard and mouse inputs over a LAN connection. While I have been successful at doing this using the SendInput() function for things like Notepad and Visual Studio, the inputs are not registered in many video games. After doing some research, it appears that I'll need to use DeviceIOControl to accomplish the task, however I have no idea how I am supposed to do this when the keycodes have to be determined at runtime.

I'm not sure if I can just arbitrarily set the hDevice parameter to the incoming keycode or not, or what the code would look like in the end.

If it helps, the below code is what is currently being used to input key presses. The server receives a string which is parsed out to determine the key press and the state of the key (whether it's a key up or key down event). This information is passed as the parameters to this function:

void keyPress(WORD k, int state)
{
    INPUT Inputs;

    std::cout<<"Key="<<k<<", state="<<state<<std::endl;
    Inputs.type=INPUT_KEYBOARD;
    Inputs.ki.wVk=k;

    if(state==1)
    {
        Inputs.ki.dwFlags=0;
        SendInput(1, &Inputs, sizeof(INPUT));
    }
    else
    {
        Inputs.ki.dwFlags=KEYEVENTF_KEYUP;
        SendInput(1, &Inputs, sizeof(INPUT));
    }

    if((int)k!=0)     //Keycode of 0 means that the last key pressed is being held down.
    {
        LastKey=k;
        key[  0]=LastKey;
    }
}

Also, if I am going down the wrong rabbit hole and there is a better solution, please let me know!

Thank you,

  • Have you read how to setup the `Inputs`? https://stackoverflow.com/a/22419083/4123703 It seems you did not initialized `Inputs` so `time`,`dwExtraInfo` and other not initialized variables might be undetermined value. – Louis Go Aug 12 '20 at 00:11
  • @LouisGo I have added `Inputs.ki.wScan=0;` `Inputs.ki.time=0;` and `Inputs.ki.dwExtraInfo=0;` to the function right below the `Inputs.ki.wVk=k;` line, but this did not change the outcome. – Benjamin Dennison Aug 12 '20 at 00:28
  • Take a look at [this blog](https://batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput/) and [this post](https://stackoverflow.com/a/41057051/4123703). I checked the blog solution and it did press `a` to another program. Don't test with network, just test with hardcoded input. If it's still failed, post your [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Also, don't test with the game, test with something like notepad to reduce question complexity. Because a game might have mechanisms to filter your input. – Louis Go Aug 12 '20 at 00:54
  • @LouisGo Perhaps I should have clarified. The code that I originally posted does work perfectly fine over the network and on applications like Notepad, Visual Studio, and Google Chrome. It is only in video games where the input is failing to function properly. – Benjamin Dennison Aug 12 '20 at 01:03
  • Then that's a different question...Please edit your post with the clarification and title should reflect your actual intent. Also it depends on which game are your trying to use. Some games might have auto-it prevention mechanisms.. – Louis Go Aug 12 '20 at 01:12
  • @LouisGo I have made the changes to hopefully make it more clear what the intent is. I should note that there must be some way to do this, as there are game streaming software like Google Stadia which are purpose built for sending keyboard and mouse inputs over the network for gaming. – Benjamin Dennison Aug 12 '20 at 01:36

0 Answers0