Skip to content

Add a Key.CONTROL modifier for the physical Control key - #4884

Merged
freakboy3742 merged 3 commits into
beeware:mainfrom
kattni:add-ctrl-key
Nov 10, 2026
Merged

freakboy3742 merged 3 commits into
beeware:mainfrom
kattni:add-ctrl-key

Conversation

@kattni

@kattni kattni commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Toga's generic modifiers (MOD_1, MOD_2, MOD_3) follow each platform's precedence, so there's no way to ask for the Control key specifically: it's MOD_3 on macOS and MOD_1 on Linux and Windows. This adds Key.CONTROL for the physical Control key. Core only defines the name; each backend maps it to its own native Control modifier:

Backend Key.CONTROL maps to Same key as
Cocoa NSEventModifierFlagControl MOD_3
GTK <Control> (not <Primary>, which is Command on macOS) MOD_1
Winforms Keys.Control MOD_1
Qt ControlModifier MOD_1
Android <Ctrl> MOD_1

iOS, Textual and Web don't support keyboard shortcuts, so they have nothing to map.

When a native key event is converted back into a Toga key, Control is still reported as the generic modifier (MOD_1 or MOD_3), so existing results don't change. Qt and Winforms used one modifier table for both directions, so they get a separate shortcut-only table; otherwise a Control key event would report both MOD_1 and CONTROL. On Winforms, a shortcut that combines MOD_1 and CONTROL shows "Ctrl" only once in the menu label.

Tests: a Key.CONTROL + "a" case in the testbed key round-trip test, which expects MOD_3 on macOS and MOD_1 elsewhere. The GTK probe now parses <Control>. Run locally on the macOS testbed (fails without the Cocoa mapping, passes with it); GTK, Qt and Winforms are exercised by CI.

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool

Assisted-by: Claude Code (Claude Opus 5.5)

kattni and others added 3 commits November 9, 2026 22:55
Core defines Key.CONTROL; each backend maps it to its native Control
modifier. On macOS that's the same key as MOD_3; on Linux and Windows
it's the same key as MOD_1. Key events still report the generic MOD_n
modifier.

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good; one minor tweak ensuring that the probe contains all the platform-specific definitions, rather than needing a current_platform check in the test code.

Comment thread testbed/tests/test_keys.py Outdated
from toga.keys import Key

# The generic modifier that the backend's physical Control key is reported as.
CONTROL = Key.MOD_3 if toga.platform.current_platform == "macOS" else Key.MOD_1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The definition of the mapping should be on the probe, rather than relying on current_platform.

@freakboy3742
freakboy3742 merged commit 86189af into beeware:main Nov 10, 2026
180 of 182 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants