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

keyboard, space bar or enter cannot trigger a button clicking #20

Open
mo-han opened this issue Jun 16, 2021 · 5 comments
Open

keyboard, space bar or enter cannot trigger a button clicking #20

mo-han opened this issue Jun 16, 2021 · 5 comments

Comments

@mo-han
Copy link

mo-han commented Jun 16, 2021

pressing space bar when focusing on a button will trigger an animation of button click, but the button's on_click is not called, have to use mouse to do a "real click"

while pressing enter cannot even generate a button clicked animation

p.s. this project has very promising future, but current version still very incomplete, i would hope to be more skillful to contribute to this project but right now i'm a newbie especially in Qt

@fding
Copy link
Member

fding commented Jun 17, 2021

Hi, thank you for your feedback and suggestions. Please keep on filing feature requests so that I have a better idea of what people want or need in their projects.

I'll take a look at keyboard bindings and figure out the best way to incorporate them.

@jamesdbrock
Copy link
Member

I've confirmed this that Space and Enter do not fire QPushButton.clicked(), which is strange because the docs say they do.

https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QPushButton.html#detailed-description

A push button emits the signal clicked() when it is activated by the mouse, the Spacebar or by a keyboard shortcut.

@jamesdbrock
Copy link
Member

jamesdbrock commented Jul 4, 2023

@jamesdbrock
Copy link
Member

jamesdbrock commented Jul 4, 2023

I tried to solve this by making the on_click prop use the clicked() signal but it didn't work because two events would get raised for a single mouse click.

So I tried this other way and this works, but it uses a different on_trigger prop.

    @register_props
    def __init__(self,
            layout: str = "row",
            on_trigger: tp.Callable[[QKeyEvent], None] | tp.Callable[[QMouseEvent], None] | None = None,
            **kwargs):
        super().__init__(layout, **kwargs)
    def _set_on_trigger(self, underlying, on_trigger):
        if on_trigger is not None:
            def on_click(ev:QMouseEvent):
                on_trigger(ev)
            def on_key(ev:QKeyEvent):
                if ev.text() == " " or ev.text() == "\r":
                    on_trigger(ev)
            self._set_on_click(underlying, on_click)
            self._set_on_key_up(underlying, on_key)

    def _qt_update_commands(self, children, newprops, newstate):
        if self.underlying is None:
            self._initialize()
        commands = super()._qt_update_commands(children, newprops, newstate)
        for prop in newprops:
            if prop == "on_trigger":
                commands.append((self._set_on_trigger, self.underlying, newprops.on_trigger))
                commands.append((self.underlying.setCursor, Qt.PointingHandCursor))
        return commands

@jamesdbrock
Copy link
Member

The ButtonView component which has an on_trigger prop that handles both key press and mouse clicks is now published as part of Edifice. https://pyedifice.github.io/stubs/edifice.components.button_view.ButtonView.html

We should think about how we want to generally handle this problem with the Base Button too.

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

No branches or pull requests

3 participants