C#C
C#2y ago
CheckMyBio

✅ Receiving Input While Minimized (WinForms C#)

Pretty general question here. How would I go about receiving input from my user while they aren't tabbed into the program or have a different window as their foreground window? I'm using the InputSimulator Nuget package (InputSimulatorStandard version), and I'd like for my program to send inputs to a game window when it receives controller input. My only issue, now, is that the controller input isn't received unless I have my application as the foreground window.

private async void T_Tick(object sender, EventArgs e)
{
    if (Gamepad.Gamepads.Count > 0)
    {
        Controller = Gamepad.Gamepads.First();
        var Reading = Controller.GetCurrentReading();
        switch (Reading.Buttons) { 
            case GamepadButtons.DPadUp:
                Process[] process = Process.GetProcessesByName("RobloxPlayerBeta");

                Process robloxProcess = process.FirstOrDefault();
                if (robloxProcess != null)
                {
                    IntPtr h = robloxProcess.MainWindowHandle;
                    SetForegroundWindow(h);
                    await Log("We're tabbed in");
                    InputSimulator inputSim = new InputSimulator();
                    inputSim.Mouse.VerticalScroll(3);
                }
                break;
            // add other buttons here
        }
        return;
    }
}


This timer runs when a checkbox is on, and is stopped when that checkbox is turned off. It functions fine, but I can't receive said controller input unless the app is opened, which kind of ruins the whole point of this functionality.
Was this page helpful?