Lette.Wpf.AppCommands 1.0.3

dotnet add package Lette.Wpf.AppCommands --version 1.0.3
NuGet\Install-Package Lette.Wpf.AppCommands -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lette.Wpf.AppCommands" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Lette.Wpf.AppCommands --version 1.0.3
#r "nuget: Lette.Wpf.AppCommands, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Lette.Wpf.AppCommands as a Cake Addin
#addin nuget:?package=Lette.Wpf.AppCommands&version=1.0.3

// Install Lette.Wpf.AppCommands as a Cake Tool
#tool nuget:?package=Lette.Wpf.AppCommands&version=1.0.3

Lette.Wpf.AppCommands

MVVM-friendly attached behavior for Window, where you can bind WPF ICommands to system-defined application commands.

BIG NOTE: These are not the same as the WPF ApplicationCommands.

Motivation

I wanted my WPF app to react to the "back" button on my mouse. Creating a custom MouseBinding and reacting to XButton1 was fairly easy.

However, most mice with extra buttons can be customized through its accompanying software. Hence, the XButton1 isn't always the "back" button.

This package catches the underlying system-defined application commands, and is therefore well-suited for handling BrowserBackwards and BrowserForward messages that may be triggered from any button on the mouse. (Or on any other type of specialized hardware, for that matter!)

Usage

Add this XAML to your MainWindow (of type Window):

<l:Window.AppCommandBindings>
    <AppCommandBinding AppCommand="BrowserForward" Command="{Binding ForwardCommand}" />
    <AppCommandBinding AppCommand="BrowserBackward" Command="{Binding BackwardCommand}" />
</l:Window.AppCommandBindings>

This will invoke the ICommand in the Command property (with an optional CommandParameter value) whenever your MainWindow receives a corresponding application command.

How it works (or should work)

These bindings listen for WM_APPCOMMAND messages with an lparam containing an APPCOMMAND_* corresponding to the AppCommand value, as they come in on the MainWindow message loop (WndProc).

See the docs on WM_APPCOMMAND for more info on the available commands.

Note that these messages are usually broadcast by specialized hardware, ie. configurable extra buttons on your mouse or media keys on your keyboard.

Many mice with two extra buttons have those two buttons wired up for back and forward navigation out of the box.

Known shortcomings

Works only when your application is active.

The current version of this software will only detect these messages when your application is active.

Pressing the "back" button on your mouse while not hovering over your application will most likely activate whatever window is below the mouse cursor, hence deactivating yours.

Some messages never reach your app

Even if your app is active, some messages gets handled earlier by other software and are stopped from propagating.

Exactly why (shell hooks?) this happens, and how to deal with it, is unclear to me at the moment.

Disclaimer

Technically, all defined APPCOMMAND_* values are supported, but YMMV.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net5.0-windows7.0 is compatible.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 776 10/28/2021