How Do I Access Windows.Gaming.Input in a Non-UWP C# Project?
Asked Answered
B

2

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,

Broder answered 30/4, 2020 at 17:2 Comment(0)
O
6

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.
Oscine answered 9/9, 2021 at 19:28 Comment(7)
Worked perfectly, thanks for the tips, worked on Visual Studio 2015.Tamera
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.Tamera
I can't see "Universal Windows". This is how my .csproj file looks: c# <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net7.0-windows</TargetFramework> <TargetPlatformVersion>10.0</TargetPlatformVersion> <Nullable>enable</Nullable> <UseWindowsForms>true</UseWindowsForms> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <PackageReference Include="System.Runtime.WindowsRuntime" Version="4.7.0" /> </ItemGroup> </Project> P.S: Formatting didn't go wellBurgoo
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+Interfluent
@Interfluent The method has changed for .net6... They change the tag names in the new csproj format: <TargetFramework> and <TargetPlatform>. Updated answer, thanks.Oscine
@JoeDF any ideas for Microsoft Visual Studio Community 2022 The "Universal Windows" does not appear. I was trying to achieve a similar thing for Game Development Kit (GDK), see: mega.nz/file/…Thirzia
I have not used GDK before... maybe you can try adding <ItemGroup> <Reference Include="Windows.Gaming" /> <Reference Include="WindowsBase" /> </ItemGroup>Oscine
P
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.

Prothrombin answered 1/5, 2020 at 3:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.