Skip to content

bug: Keyboard events cannot prevent system events. #6346

@zangxx66

Description

@zangxx66

Duplicate Check

Describe the bug

In macOS, pressing Command + Q will immediately terminate the program and cannot trigger a custom keyboard event.

Code sample

Code
import flet as ft


def main(page: ft.Page):
    async def handle_exit(_: ft.Event[ft.TextButton]):
        await page.window.close()

    def show_exit_confirm():
        page.show_dialog(
            ft.AlertDialog(
                title=ft.Text("info"),
                content=ft.Text("Do you want to quit?"),
                actions=[
                    ft.TextButton("Cancel", on_click=lambda e: page.pop_dialog()),
                    ft.TextButton("Quit", on_click=handle_exit),
                ],
            )
        )

    def on_keyboard(e: ft.KeyboardEvent):
        if e.meta and e.key == "Q":
            show_exit_confirm()

    page.on_keyboard_event = on_keyboard
    page.add(
        ft.Text("hello workd")
    )


ft.run(main)

To reproduce

1.uv run flet run main.py run code
2.pressing Command + Q
3.program immediately exits

Expected behavior

I created a dialog box for exit confirmation. I hope that when the exit shortcut key is triggered, an exit confirmation dialog will be displayed first instead of directly terminating the program.

Screenshots / Videos

Captures

[Upload media here]

Operating System

macOS

Operating system details

macOS 15

Flet version

0.82.2

Regression

I'm not sure / I don't know

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions