3

I've seen some examples of using Windows.Gaming.Input in C++ console apps but is there a way to access it in a non-UWP C# project as well?

Thanks,

DAG
  • 867
  • 10
  • 16
Lee Wood
  • 55
  • 7

2 Answers2

5

I've managed to do this by doing the following:

  1. Make sure VisualStudio is not running.
  2. Open your project *.csproj file in a text editor, e.g. notepad.
  3. Add <TargetPlatformVersion>10.0</TargetPlatformVersion> to the *.csproj file. You can put it after the <TargetFrameworkVersion/> entry.
    Note: In .NET 6+, the tag names are now: <TargetPlatform> and <TargetFramework>.
  4. Open you project in Visual Studio.
  5. Add the System.Runtime.WindowsRuntime reference (I used v4.7.0, newest stable version at the time) with nuget, or manually browse for the DLL file on your system.
  6. In Solution/Project Explorer, right-click References -> Add Reference...
  7. You should be able to see "Universal Windows" on the side as an option now.
  8. Add the Windows.Gaming reference.
  9. In your *.cs files, you'll want to use the reference with using Windows.Gaming.Input;. If the reference is not found, try including it outside the namespace. Or you can also try using global::Windows.Gaming.Input; instead.
Joe DF
  • 5,438
  • 6
  • 41
  • 63
  • 1
    Worked perfectly, thanks for the tips, worked on Visual Studio 2015. – Bruno Marques Dec 06 '21 at 07:50
  • To work with Gamepads, you'll also need to add the System.Runtime.InteropServices.WindowsRuntime otherwise it won't be possible to subscribe and unsubscribe to the events. – Bruno Marques Dec 06 '21 at 19:24
  • I can't see "Universal Windows". This is how my .csproj file looks: ```c# WinExe net7.0-windows 10.0 enable true enable ``` P.S: Formatting didn't go well – Der_Floh Aug 04 '23 at 15:10
  • This does not work. You won't have "Universal Windows" on the left as suggested, so either something has changed or this method has broke in recent .net6+ – viion Aug 14 '23 at 23:32
  • 1
    @viion The method has changed for .net6... They change the tag names in the new csproj format: `` and ``. Updated answer, thanks. – Joe DF Aug 16 '23 at 15:39
0

but is there a way to access it in C# with UWP as well?

Sure, you could access Windows.Gaming.Input with C# within UWP platform. For example,

var controllers = RawGameController.RawGameControllers;
 foreach (var item in controllers)
 {
    var name =  item.DisplayName;
 }

Unfortunately, there is not c# project could be referred, if you do want to c# project, please feel free post your requirement with Windows Feed Backhub app.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36