Skip to content

Click shortcut of focused vaadin button triggers with key modifiers #14190

Closed
vaadin/web-components
#6404

Description

@19rainer98

Description

In our vaadin application we are used to having several buttons within a form. Additionally we obviously have a button which triggers the save functionality. For the save button we registered a click shortcut consisting of the enter key and the control key. Now the problem: If one of the several buttons is focused and the user hits the control key + the enter key, first the action of the focused button triggers and afterwards the form gets saved.

Expected outcome

Only the save action shoul be triggered

Minimal reproducible example

public class MainView extends HorizontalLayout {

private TextField name;
private Button sayHello;
private Button helloFolks;

public MainView() {
name = new TextField("Your name");
sayHello = new Button("Say hello");
sayHello.addClickListener(e -> {
Notification.show("Hello " + name.getValue());
});

   helloFolks = new Button("Hello folks");
   helloFolks.addClickListener(e -> {
       Notification.show("Hello folks");
   });
   helloFolks.addClickShortcut(Key.ENTER, KeyModifier.CONTROL);

   setMargin(true);
   setVerticalComponentAlignment(Alignment.END, name, sayHello, helloFolks);

   add(name, sayHello, helloFolks);

}
}

Steps to reproduce

  1. Dowload latest Vaadin starter
  2. Copy the code into the MainView.java
  3. Focus the sayHello Button
  4. Hit the control key + the enter key on your keyboard

Environment

Vaadin version(s): 23.1.2

Browsers

Issue is not browser related

Activity

  1. sissbruecker commented on Jul 20, 2022

    @sissbruecker
    Member

    This is likely a Flow issue.

    Possibly related: #14187, #8037

  2. web-padawan commented on Jul 20, 2022

    @web-padawan
    Member

    Yes, this is an issue in Flow. The web component has some logic to fire click on Enter or Space keys.
    This is expected, also it's how native <button> works, so we are not going to change this.

    I'm going to transfer this to Flow repository, as it seems slightly different from #14187

  3. mcollovati commented on Jul 22, 2022

    @mcollovati
    Collaborator

    It seems the issue relates to #14187
    Flow is receiving two events: click from the focused button and the keydown.
    Since firing click event on Enter or Space is the default behavior for button, I don't know at the moment how we can prevent the click listener to be invoked.
    Need further investigation

  4. probert94 commented on Mar 13, 2023

    @probert94

    I am facing the same issue in a Vaadin application.

    This is expected, also it's how native works, so we are not going to change this.

    I just tested it on an empty webpage with the following code:

    <html>
      <body>
        <button>Test</button>
        <script>
          document.querySelector("button").addEventListener("click", e => console.warn(e));
        </script>
      </body>
    </html>
    

    If I click the button or focus it and press enter, the event gets printed, if I press ctrl+enter it doesn't.
    So in my opinion, this is not a Flow issue but should be fixed in the webcomponent. The clicklistener should only be triggered if enter is pressed without modifiers.

  5. added
    BFPBugfix priority, also known as Warranty
    on Aug 22, 2023
  6. mshabarov commented on Aug 22, 2023

    @mshabarov
    Contributor

    The issue was triaged and currently added to the backlog priority queue for further investigation.

  7. TatuLund commented on Aug 24, 2023

    @TatuLund
    Contributor

    Workarounds:

    @Route("shortcut")
    public class ShortcutView extends Div {
    
        boolean button2Clicked;
    
        public ShortcutView() {
            Button button1 = new Button("1");
            Button button2 = new Button("2");
            button1.addClickListener(e -> {
                Notification.show("1 clicked");
            });
            button2.addClickListener(e -> {
                button2Clicked = true;
                button2Click();
            });
            DomListenerRegistration keyReg = button1.getElement()
                    .addEventListener("keydown", event -> {
                        if (event.getEventData().getBoolean("event.shiftKey")) {
                            button1.setEnabled(false);
                            button2.focus();
                        }
                    });
            keyReg.addEventData("event.keyCode");
            keyReg.addEventData("event.shiftKey");
            keyReg.setFilter("event.keyCode == 13");
            Shortcuts.addShortcutListener(this, e -> {
                if (!button2Clicked) {
                    button2Click();
                }
                button1.setEnabled(true);
            }, Key.ENTER, KeyModifier.SHIFT).listenOn(this);
            add(button1, button2);
        }
    
        public void button2Click() {
            Notification.show("2 clicked");
        }
    }
    
  8. TatuLund commented on Aug 24, 2023

    @TatuLund
    Contributor

    So in my opinion, this is not a Flow issue but should be fixed in the webcomponent. The clicklistener should only be triggered if enter is pressed without modifiers.

    @probert94 I tend to agree, the workarounds are not needed when using NativeButton instead of Button. So if we use that as reference.

    @Route("shortcut2")
    public class Shortcut2View extends Div {
    
        public Shortcut2View() {
            NativeButton button1 = new NativeButton("1");
            NativeButton button2 = new NativeButton("2");
            button1.addClickListener(e -> {
                Notification.show("1 clicked");
            });
            button2.addClickListener(e -> {
                Notification.show("2 clicked");
            });
            Shortcuts.addShortcutListener(this, e -> {
                button2.getElement().executeJs("this.click();");
            }, Key.ENTER, KeyModifier.SHIFT).listenOn(this);
            add(button1, button2);
        }
    
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions