beeware / beeware/toga

Escape key used as toga.Command shortcut does not trigger callback

Open
#2,526 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug macOS
Dominant language
Python
Stars
5.4k
Forks
829
Avg merge
15h 39m
Merged PRs (30d)
40

Description

Describe the bug

The Escape key does not work on macOS when specified as a shortcut to a toga.Command. The keystroke is merely ignored.

Steps to reproduce

Put the following code in a python file:

from toga import Group
from toga import Key, Icon
from toga.style import Pack
from toga.style.pack import COLUMN

def key_press_event(e):
    print(e)
    app.exit()

def build(app):
    main_window = toga.MainWindow(title='ExitMenuTest', size=(300, 200))

    box1 = toga.Box(style=Pack(direction=COLUMN))

    mylabel = toga.Label('Press "Escape" or "x" on the keyboard to exit\n')
    box1.add(mylabel)

    cmd1 = toga.Command(
        key_press_event,
        'Exit program',
        group=Group.FILE,
        shortcut=Key.ESCAPE,
        section=0
        )


    cmd2 = toga.Command(
        key_press_event,
        'Exit program1',
        group=Group.FILE,
        shortcut=Key.X,
        section=0
        )

    app.commands.add(cmd1,cmd2)

    return(box1)

if __name__ == '__main__':
    app = toga.App('ExitMenuTest', 'org.example.ExitMenuTest', startup=build, icon=toga.Icon.DEFAULT_ICON)
    app.main_loop()

Run the python file.

Pressing "X" on the keyboard when the app is open will quit the app, and hitting escape will not.

Expected behavior

The associated callback is run.

Screenshots
image
Environment
  • Operating System: macOS 14.4.1 on Apple Silicon (23E224)
  • Python version: 3.12.3
  • Software versions:
    • Toga: 0.42
Logs

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the macOS behavior with the provided Python example, then trace how toga.Command handles Key.ESCAPE versus Key.X shortcuts. Done means pressing Escape invokes the associated callback just as pressing X does.

Written by the indexing model from the issue text.

Assessment

Tech stack
macos, python
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.