Skip to content
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

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

Open
cicigee opened this issue Apr 23, 2024 · 1 comment
Open

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

cicigee opened this issue Apr 23, 2024 · 1 comment
Labels
bug A crash or error in behavior. macOS The issue relates to Apple macOS support.

Comments

@cicigee
Copy link

cicigee commented Apr 23, 2024

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

@cicigee cicigee added the bug A crash or error in behavior. label Apr 23, 2024
@freakboy3742
Copy link
Member

Thanks for the report - thanks to your sample code, I've been able to reproduce this.

After a little investigation, it appears that this is a macOS limitation/feature. If you search this document for "Escape", there's a mention that:

The Escape key is another default key for a keyboard interface control in a window; it immediately aborts a modal loop.

There's a couple of other mentions of how to work around this, but they're fairly low level modifications to the keyboard handling process.

On that basis, my immediate suggestion is that a bare Escape would be best to avoid as a keyboard shortcut, because of the overlap with existing OS keyboard handlers. MOD_1 + ESCAPE works; as do the other modifiers on ESCAPE, and most of the other "bare" keys on your keyboard.

At the very least this general problem is worth noting in the documentation - ideally we'd provide a list of keys that aren't good candidates for keyboard shortcuts. That could arguably be surfaced as a platform-level warning/error as well.

@freakboy3742 freakboy3742 added the macOS The issue relates to Apple macOS support. label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. macOS The issue relates to Apple macOS support.
Projects
None yet
Development

No branches or pull requests

2 participants