I'm trying to detect if the windows game bar is open and redirecting input as described on https://devblogs.microsoft.com/oldnewthing/20160629-00/?p=93775 But in Vb.Net or C# Winforms Framework 4.7
After giving up on ChadGPT garbage suggestions i ran the c++ code through a converter and ended up with this.
using Microsoft.WindowsAPICodePack.GamingServices;
using System;
using System.Runtime.InteropServices;
namespace YourNamespace
{
class YourClass
{
static GamingServices.GameBarStatics gameBarStatics;
static bool isVisible;
static bool isInputRedirected;
static EventRegistrationToken tokenVisibility;
static EventRegistrationToken tokenInput;
static void Main(string[] args)
{
gameBarStatics = GamingServices.GameBarStatics.GetDefault();
isVisible = gameBarStatics.IsVisible;
isInputRedirected = gameBarStatics.IsInputRedirected;
tokenVisibility = gameBarStatics.VisibilityChanged += GameBarStatics_VisibilityChanged;
tokenInput = gameBarStatics.InputRedirected += GameBarStatics_InputRedirected;
// Rest of your code...
}
static void GameBarStatics_VisibilityChanged(object sender, object e)
{
// Handle visibility changed event...
}
static void GameBarStatics_InputRedirected(object sender, object e)
{
// Handle input redirected event...
}
}
}
Imports Microsoft.WindowsAPICodePack.GamingServices
Class GameBar
Shared gameBarStatics As GamingServices.GameBarStatics
Shared isVisible As Boolean
Shared isInputRedirected As Boolean
Shared tokenVisibility As EventRegistrationToken
Shared tokenInput As EventRegistrationToken
Public Sub New()
gameBarStatics = GamingServices.GameBarStatics.GetDefault()
isVisible = gameBarStatics.IsVisible
isInputRedirected = gameBarStatics.IsInputRedirected
tokenVisibility = AddressOf GameBarStatics_VisibilityChanged
AddHandler gameBarStatics.VisibilityChanged, AddressOf GameBarStatics_VisibilityChanged
tokenInput = AddressOf GameBarStatics_InputRedirected
AddHandler gameBarStatics.InputRedirected, AddressOf GameBarStatics_InputRedirected
End Sub
Private Shared Sub GameBarStatics_VisibilityChanged(ByVal sender As Object, ByVal e As Object)
Debug.Print("GameBarStatics_VisibilityChanged")
End Sub
Private Shared Sub GameBarStatics_InputRedirected(ByVal sender As Object, ByVal e As Object)
Debug.Print("GameBarStatics_InputRedirected")
End Sub
End Class
However it seems I can't find the correct NuGet package to install that exposes GamingServices
Note the C# code is raw and I'm not proficient enough in it to see if it has errors, I tinkered with the translated VB.Net code as it wasn't perfect but unfortunately I can't test if what I changed even works.
Open
Tools/Options...
and underNuGet Package Manager/General
setDefault package management format:
toPackageReference
While installing the
Microsoft.Windows.SDK.Contracts
NuGet Package click onConfigure
and addWindows.Foundation.UniversalApiContract
as aPacket Source Mapping
You can now reference
Windows.Gaming.UI.GameBar
in your code.Note: Uncomment the following line in
app.manifest
to getEnvironment.OSVersion.Version
to report the right numbers.Elsewhere in your code use
I resorted to stuffing this into a Timer as i couldn't get the
Windows.Gaming.UI.GameBar.IsInputRedirectedChanged
event to work reliably.