Question about rule 'Mouse events should have corresponding keyboard events'

SonarQube 7.7 Community, SonarHTML 3.2 (build1615)

<button class="btn btn-outline-danger" (click)="cancel()">Cancel</button>

Recently upgraded to 7.7 and now seeing the bug Web:MouseEventWithoutKeyboardEquivalentCheck reported for some of our Angular 5 modules. For example the code above resulted in the bug “Add a ‘onKeyPress’ attribute to this tag.” and reviewing, I find the suggested compliant solution

<a onClick="doSomething();" onKeyPress="doSomething();" ...> 

Is this rule valid for the given use case? Most (if not all) browsers already trigger the click event if the user hits the space-bar or enter key when focus is on a button. Further the suggested solution, if blindly followed would result in very unexpected behavior as the buttons are triggered when tabbing or arrowing around the page.

And finally, even if valid for vanilla JavaScript, is it still valid for an Angular implementation?

Hi Nick,

You are right that <button> elements should not raise any issue for the onclick attribute. Using onclick for this element is in fact recommended by WCAG2.

For anchors there is a small difference in that only the enter key triggers the onclick, not the space key. W3C recommends to remain consistent by adding an onkeypress handler for anchors. (See Example 1: Using Space to activate a button). But as WCAG2 actually recommends to just use onclick I will ask our designer and accessibility specialist what she thinks of this use case (@Laura).

As a side note, onkeypress is not triggered for arrows and tab keys, but you are right that the example should rather show that the function uses the keyboard event to filter other keys. The rule should also accept other attributes such as onkeydown and onkeyup because onkeypress is deprecated.

I will create a ticket to change the rule as soon as I receive the answer for the anchor use case.

Thank you for your feedback.

Cheers,

Nicolas

I agree, I did over-generalize. Thanks for the quick reply. Will watch for updates.

N-

Hi Nick,

I created the following ticket. The new rule description is available here.

We expect to implement it soon.

Thank you for your patience.

Why is this rule mandatory, in my case i dont need any onKey event but due to this rule my sonar is failing , is there any way to skip it without adding onKey event?

1 Like

Hi, did you get any resolution for this?

1 Like

For a month or so I have been looking for this rule, it was activated and now many elements are marked with the same thing.

Mouse events should have corresponding keyboard events (Web:MouseEventWithoutKeyboardEquivalentCheck)

Is there a way to deactivate that rule or a logical explanation because it was activated for security or something because

The link Example 1: Using Space to activate a button goes to a wiki, which is not an official source of recommendations related to WCAG.

In addition, quoting that example in a discussion about <a onClick="doSomething();" onKeyPress="doSomething();" ...> is inappropriate because that page is about WAI-ARIA controls and the code example is no such control (it has no role attribute).

Finally, pure links respond to Enter, not the the Space bar; there is no requirement nor a recommendation in WCAG to make links (as opposed to buttons) respond to the spacebar.

meanwhile, 5 years later…

I think you missed where the snippet onKeyPress="doSomething() was a direct quote from the “suggested solution” found in the original issue description. I was pointing out that the suggestion as written at that time was not a good recommendation because you typically need to filter keyboard events by key and the example suggested using the same method as was assigned to the click event.

This rule doesn’t recognize the use of (keyup.enter) in Angular. I have to manually mark it as a false positive.

1 Like