Skip to content

Instantly share code, notes, and snippets.

@jkdba
Created August 18, 2023 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkdba/db450898f0ad9b6ebf4280c91c003fb7 to your computer and use it in GitHub Desktop.
Save jkdba/db450898f0ad9b6ebf4280c91c003fb7 to your computer and use it in GitHub Desktop.
.Net Maui Windows & WinUI3 System/Global Hotkey
using MauiApp1.Data;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Platform;
using System.Runtime.InteropServices;
using MauiApp1.Shared;
namespace MauiApp1
{
public static class MauiProgram
{
//private const uint WM_HOTKEY = 0x0312;
//private Windows.Win32.UI.WindowsAndMessaging.WNDPROC origPrc;
//private Windows.Win32.UI.WindowsAndMessaging.WNDPROC hotKeyPrc;
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureLifecycleEvents(events =>
{
#if WINDOWS
events.AddWindows(windows => windows
.OnClosed((window, args) =>
{
// unregister hot key
var h = window.GetWindowHandle();
UnregisterHotKey(h, 1);
LogEvent(nameof(WindowsLifecycle.OnClosed));
})
.OnWindowCreated((window) =>
{
// register hot key ctrl+win+.
var h = window.GetWindowHandle();
var success = RegisterHotKey(h, 1, 0x0008 | 0x0002, 0xBE);
LogEvent(nameof(WindowsLifecycle.OnWindowCreated));
})
.OnPlatformMessage((window, args) =>
{
// on hotkey received from os
if (args.MessageId == 0x0312 && args.WParam.ToInt32() == 1)
{
// toggle app window visibility
var h = window.GetWindowHandle();
if(window.Visible)
{
ShowWindow(h, 6);
}
else
{
SetActiveWindow(h);
ShowWindow(h, 9);
//window.GetAppWindow().Title = "oh no";
//SetForegroundWindow(h);
//SetFocus(h);
//SetActiveWindow(h);
}
}
}));
#endif
static bool LogEvent(string eventName, string type = null)
{
System.Diagnostics.Debug.WriteLine($"Lifecycle event: {eventName}{(type == null ? string.Empty : $" ({type})")}");
return true;
}
})
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
builder.Services.AddMauiBlazorWebView();
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
#endif
builder.Services.AddSingleton<WeatherForecastService>();
return builder.Build();
}
}
}
@jkdba
Copy link
Author

jkdba commented Aug 18, 2023

learned quite a bit from this blog post then found a way to not directly have to create a new window procedure to handle the os messaging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment