Skip to content

XPF: Add docs for Ctrl+Click menus on macOS #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions xpf/platforms/macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ However there are problems with this mapping:
2. `ModifierKeys.Windows` is actually [not set in `Keyboard.Modifiers` in WPF](https://github.com/dotnet/wpf/blob/6634719e22053aab8e5e0db37618170494aea334/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs#L207-L219)
3. Common controls such as text boxes are expected to have different keyboard shortcuts in macOS, such as "Move the insertion point to the beginning of the previous word" being Option+Left Arrow on macOS instead of Control+Left Arrow

## Automatic macOS Key Mapping
### Automatic macOS Key Mapping

To fix many of these problems, one can call the `XpfKeyboard.MapMacOSKeys()` method on startup. This would usually be done in the same place as [the XPF WinAPI shim setup](../third-party-libraries); that is, in the constructor of your `App` class or `Program.Main`:

Expand Down Expand Up @@ -45,6 +45,27 @@ Calling this method on macOS:
- Option+Left Arrow-> Ctrl+Left Arrow
- Option+Left Arrow -> Ctrl+Left Arrow

## Automatic macOS Keyboard Mapping
### macOS Custom Keyboard Mapping

For more flexible key mapping you can [add custom key mappings](../advanced/key-mapping).
For more flexible key mapping you can [add custom key mappings](../advanced/key-mapping).

## Context Menus <MinVersion version="1.6" />

On macOS, context menus can be opened by Ctrl+Clicking as well as by right clicking. You can enable this feature by setting `XpfMouse.ShowContextMenuOnMacOSCtrlClick` on startup. This would usually be done in the same place as [the XPF WinAPI shim setup](../third-party-libraries); that is, in the constructor of your `App` class or `Program.Main`:

```csharp
using System.Windows;
using Atlantis;

namespace XpfKeyboardMappingExample;

public partial class App : Application
{
public App()
{
XpfMouse.ShowContextMenuOnMacOSCtrlClick = true;
}
}
```

Once this feature is enabled, it can be disabled on a per-control basis by handling the [`ContextMenuOpening` event](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/how-to-handle-the-contextmenuopening-event#suppressing-any-existing-context-menu-and-displaying-no-context-menu) and checking the value of [`Keyboard.Modifiers`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.keyboard.modifiers) and/or [`Mouse.LeftButton`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.mouse.leftbutton) to determine how the context menu is being opened.