C#C
C#2y ago
CheckMyBio

[✅] Receiving Game Controller Input while Minimized (WinForms C#) | Repost

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)
        {
            Gamepad gamepad = null;

            if (RawGameController.RawGameControllers.Count > 0)
            {
                RawGameController rawGameController = RawGameController.RawGameControllers[0];
                gamepad = Gamepad.FromGameController(rawGameController);
            }

            if (gamepad != null)
            {
                var reading = gamepad.GetCurrentReading();

                switch (reading.Buttons)
                {
                    case GamepadButtons.DPadUp:
                        MessageBox.Show("input received");
                        break;
                    case GamepadButtons.DPadDown:
                        break;
                }
            }
            else
            {
                MessageBox.Show("gamepad is equal to null");
            }
        }

        private async void controllerZooming_CheckedChanged(object sender, EventArgs e)
        {
            SaveSettings();
            GetSettings();
            if (controllerZooming.Checked)
            {
                t.Tick += T_Tick;
                t.Interval = 100;
                t.Start();
            }
            else
            {
                if (t.Enabled) t.Stop();
                await Log("Stopped");
            }
        }


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.

I've experimented with trying to use something of similar function to GetAsyncKeyState(), but as far as I know, nothing of such would work for a controller.

Previously, I created a thread regarding this question and received two answers; one to use RawGameController rather than Windows.Gaming.Input.Gamepad, and the other to check if my application receives input in the background. While the use of RawGameController did work for the project, my program did not receive background input while minimized. Upon using Spy++ as another user recommended, I reaffirmed that my program could not receive input when minimized.

I've tried to run my inputs via a background thread, but to no avail, as controller input was still only received while my program was the foreground window. Would appreciate some help on what direction I should try going with this.
Was this page helpful?