C
C#Mustafa

Receiving Game Controller 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;
}
}
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. I've experimented with the use of GetAsyncKeyState(), but as far as I know, that wouldn't work for a controller.
B
Buddy16d ago
Did staff approve? I remember seeing the same thread where they said to contact staff to validate that it does not break ToS
Y
Yawnder16d ago
Yes they did. Here is the proof, now we can help! https://discord.com/channels/143867839282020352/1233614811523452981 @Mustafa Where is the Gamepad class from? Which package?
B
Buddy16d ago
Don't gui windows receive input messages from controllers, etc? So you can just hook into wndproc
Y
Yawnder16d ago
I don't think they do when not focused, but I could be wrong.
B
Buddy16d ago
I believe they do
M
Mustafa16d ago
yeah they did Windows.Gaming.Input
Y
Yawnder16d ago
@Mustafa I'd look into https://learn.microsoft.com/en-us/uwp/api/windows.gaming.input.gamepad.fromgamecontroller?view=winrt-22621 Look at the C# example. RawGameController seems to be what you're looking for.
M
Mustafa16d ago
Alright thank you, I'll take a look
Y
Yawnder16d ago
Some people talk about using P/Invoke to hook to the OS directly, but apparently that could trigger anti-virus and other stuff since it's "less official"
M
Mustafa16d ago
yeah hooking to the OS would probably cause a lot of problems for anyone with an antivirus which I'd rather not deal with
B
Buddy16d ago
Hooking the window messages would be ideal. Significantly less trouble. Moreso window messages of the process, not your program.
M
Mustafa16d ago
Hooking into roblox?
B
Buddy16d ago
If that is allowed by the ToS
M
Mustafa16d ago
Nah, Roblox's anticheat doesn't allow you to hook processes to roblox It falls under their cheating policy as far as my knowledge goes
B
Buddy16d ago
I see. I assume your program is for controller support, or am I wrong?
M
Mustafa16d ago
it's basically just a program that allows for more freedom of roblox utilities and customizations. I've had a lot of requests to add options for controller players to zoom in/out, so I thought it'd be a good idea to add it
M
Mustafa16d ago
No description
M
Mustafa16d ago
No description
B
Buddy16d ago
Ah, I see. Then messages doesn't really matter then.
M
Mustafa16d ago
my other features are just edits to roblox files so i'm not actually attaching anything to the roblox process yeah
B
Buddy16d ago
I just assumed you wanted to "convert" the inputs. My apologies
M
Mustafa16d ago
Nah, no need to apologize. I appreciate that you're helping The way roblox works right now, the game that my program is mainly designed for doesn't have a good zooming feature, and it doesn't map any dpad buttons to zoom So I thought it would probably be convenient to just make it so that dpad up would zoom in and dpad down would zoom out
B
Buddy16d ago
You can use Spy++ to check whether your program receives inputs in background, if it does then great! Otherwise you might have to use a wrapper Spy++ is included with the installation of VS
M
Mustafa16d ago
Alright, thanks. I'll look into that as well as the solution provided above by Yawnder and see what I can manage to get.
B
Buddy16d ago
Should be in the Tools directory of your VS path
M
Mustafa15d ago
@Yawnder - [▰▱▱▱▱] I implemented the method used in the article you sent me. While the input does work, it still only registers when I have my program as the foreground window, whereas I would need it to work while I'm minimized to achieve the functionality I'm aiming for. @Buddy, Citizen of Pluto My program doesn't receive input in the background The issue isn't my program as a whole but rather the method for receiving controller input the controller input is only received when i have my program as the foreground window
Want results from more Discord servers?
Add your server
More Posts
process startIve been trying to figure out why the proccess.start() hasnt been working✅ Configurations say that appsettings is returning nullBasically, i have a configuration and i'm trying to access a value from my appsettings, and all that✅ ASP.NET Core Web Api + SignalRHi, I have an API deployed on IIS but when a person wants to access it they get an error: "ERR_CONNE✅ .NET sdk not workingSo, I've installed the .NET sdk and did what I was told, but it's still not being detected by vscodeMEF No exports were found that match the constraintHi, i'm trying to implement MEF in my WPF app, however i'm getting this error, not sure why anymore.✅ WPF - set UIElement (image) in XAML depending on databound Property Enum valueI am trying to dynamically change a StatusBar image via a view's XAML depending upon a databound VieASP.NET Controller API + Blazor Client UI resources and helpHello! I'm doing a side project where I created the backend API with simple CRUD functionality and A✅ Run PuppeteerSharp in Docker ContainerI have an endpoint that creates PDFs using PuppeteerSharp. I work fine locally because it can automaDocker + Blazor + MSSQL handshake errorWhen I run dotnet ef database update I get the following error : ``Microsoft.Data.SqlClient.SqlExceProblem with corner radius XAML propertyI use a border with these properties: ```<Border CornerRadius="3" Background="{StaticResource Edito✅ :white_check_mark: Dictionary<int, ...> lookup time seems a bit too slowI was profiling a program, and I saw this: ```cs 100 % HasObject • 136 ms • 189 217 Calls • Engine.Rhow to read .txt file next to the Program.csi have the following code that is begin run ```cs string fileContent = File.ReadAllText("hamlet.txt✅ Linker Error Trying to Build OpenSSL Static LinkingHello, I've been trying to compile and build OpenSSL to try and encrypt a text file using AES 256 buHow things get to the placeI have found this one thing by Microsoft that confused me so much ```cs public async Task<IActionResVisual code studio and msbuildim currently having problems while scripting apparently when i press (Run Build Task) im getting thiMediatR - System.Text.Json.JsonException: 'S' is an invalid start of a value.I have a project with Clean Architecture and MediatR. My API gets the request fine and the command Blazor, auto unsubscribe patternI have created website using blazor, and if some component subscribe to something then it implement ✅ Form1.resx file gives error on Internet or Restricted zoneForm1.resx file gives error on Internet or Restricted zone I can't fix it error (english):Severity LHelp with API structureI want to create modular API structure https://timdeschryver.dev/blog/maybe-its-time-to-rethink-our-✅ WPF - how to correctly call a method in the ViewModel when an event occurs in the View?I want to call a method in the ViewModel when an event happens in the View. That is, when the View