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
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?
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.
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?
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.
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.