C
C#8mo ago
Mustafa

[✅] 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 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");
}
}
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.
21 Replies
Jester
Jester8mo ago
i know about the win32 raw input api and that one sends messages to your window message loop. you can also make it het global messages, not only in your window. i feel like you have tried wappers of it here but idk why those dont seem to work that way?
Mustafa
MustafaOP8mo ago
wouldn't the win32 raw input api only work for a mouse/keyboard? I don't think it supports game controllers, though I could be wrong the previous thread i had opened for this was: #Receiving Game Controller Input while Minimized (WinForms C#)
Jester
Jester8mo ago
it should have other hid devices too but maybe game controllers are special
Mustafa
MustafaOP8mo ago
Raw Input - Win32 apps
This section describes how the system provides raw input to your application and how an application receives and processes that input.
Mustafa
MustafaOP8mo ago
to be quite honest im not sure how i could use it to implement a game controller, haven't seen any documentation on how to do anything like that or similar
Jester
Jester8mo ago
hmm yeah idk different than raw input: i got input from a simple wacom tablet by reading it as a file but i had to figure out what the bytes mean myself
Mustafa
MustafaOP8mo ago
that's what i mean i have no clue how to read the bytes or even get them properly lol
Jester
Jester8mo ago
GitHub
GitHub - QubitTooLate/RawWacom: Small app to use a Wacom tablet as ...
Small app to use a Wacom tablet as a mouse. Contribute to QubitTooLate/RawWacom development by creating an account on GitHub.
Mustafa
MustafaOP8mo ago
you think anything like that exists for game controllers too?
Jester
Jester8mo ago
:rozoa_shrug:
Mustafa
MustafaOP8mo ago
ive tried looking but i havent found any
Jester
Jester8mo ago
you might be able to read some bytes like what i did there but might not be useful
Mustafa
MustafaOP8mo ago
yeah getasynckeystate() is the easiest way to get input while minimized but that's only for keyboards
Jester
Jester8mo ago
yeah
Mustafa
MustafaOP8mo ago
i mean i have this
public enum GamepadButtons : uint
{
None = 0u,
Menu = 1u,
View = 2u,
A = 4u,
B = 8u,
X = 0x10u,
Y = 0x20u,
DPadUp = 0x40u,
DPadDown = 0x80u,
DPadLeft = 0x100u,
DPadRight = 0x200u,
LeftShoulder = 0x400u,
RightShoulder = 0x800u,
LeftThumbstick = 0x1000u,
RightThumbstick = 0x2000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle1 = 0x4000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle2 = 0x8000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle3 = 0x10000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle4 = 0x20000u
}
public enum GamepadButtons : uint
{
None = 0u,
Menu = 1u,
View = 2u,
A = 4u,
B = 8u,
X = 0x10u,
Y = 0x20u,
DPadUp = 0x40u,
DPadDown = 0x80u,
DPadLeft = 0x100u,
DPadRight = 0x200u,
LeftShoulder = 0x400u,
RightShoulder = 0x800u,
LeftThumbstick = 0x1000u,
RightThumbstick = 0x2000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle1 = 0x4000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle2 = 0x8000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle3 = 0x10000u,
[ContractVersion("Windows.Foundation.UniversalApiContract", 196608u)]
Paddle4 = 0x20000u
}
Mustafa
MustafaOP8mo ago
that's just what GamepadButtons accesses
No description
Mustafa
MustafaOP8mo ago
but i can't say i'm sure how i could use this to achieve the functionality im looking for ds4windows does something similar but their entire process is vastly different since it's translating playstation input into xbox input
Jester
Jester8mo ago
maybe a lowlevelhook if that input even gets sent trough that
Mustafa
MustafaOP8mo ago
yea i could try that UPDATE: @186 days until 🎃 @Yawnder - [▰▰▰▰▱] @Buddy, Citizen of Pluto I apologize for the mass ping, but since you three were helping me, I owe a thank you. I've found my solution in the form of using the SharpDX.XInput nuget package. The following usage has resulted in success, and the messagebox shows up regardless of whether or not I am focused:
private Gamepad _gamepad;
private Controller _controller;

private async void T_Tick(object sender, EventArgs e)
{
_controller = new Controller(UserIndex.One);
if (_controller.IsConnected)
{
_controller.GetState(out var state);
var dpadUp = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp);
if (dpadUp) Log("Hi bro it worked");
}
else await Log("not connected");
}
private Gamepad _gamepad;
private Controller _controller;

private async void T_Tick(object sender, EventArgs e)
{
_controller = new Controller(UserIndex.One);
if (_controller.IsConnected)
{
_controller.GetState(out var state);
var dpadUp = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp);
if (dpadUp) Log("Hi bro it worked");
}
else await Log("not connected");
}
Jester
Jester8mo ago
:Thumbs:
Yawnder
Yawnder8mo ago
Great!
Want results from more Discord servers?
Add your server