How does one detect if Windows Game Bar is active in .NET?

139 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

Open Tools/Options... and under NuGet Package Manager/General set Default package management format: to PackageReference

While installing the Microsoft.Windows.SDK.Contracts NuGet Package click on Configure and add Windows.Foundation.UniversalApiContract as a Packet Source Mapping

You can now reference Windows.Gaming.UI.GameBar in your code.

Private gbSupported As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim osVer As Version = Environment.OSVersion.Version
    If osVer.Major >= 10 AndAlso osVer.Build >= 10586 Then
        gbSupported = True
    End If
End Sub

Note: Uncomment the following line in app.manifest to get Environment.OSVersion.Version to report the right numbers.

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

Elsewhere in your code use

If gbSupported AndAlso Windows.Gaming.UI.GameBar.IsInputRedirected Then
    'do stuff
End If

I resorted to stuffing this into a Timer as i couldn't get the Windows.Gaming.UI.GameBar.IsInputRedirectedChanged event to work reliably.