简体   繁体   中英

JavaScript onkeydown in a link doesnot work with JAWS

I have an HTML page where the code is

<!DOCTYPE html>
<html>
<body>
<script>
function myFunction(event) {
    if (event.keyCode == 13) {
        console.log(5 + 6);
        return false;
      }
}
</script>
<a href="#"  id="descRef" onkeydown='myFunction(event)'><b>Search Description222</b></a>
</body>
</html>

It works and the function is triggered. But when I enable JAWS professional it does not trigger the function onkeypressdown

You have to understand the difference between a screen reader's "browse mode" vs "forms mode". In "browse mode", the screen reader user can "browse" the DOM by using screen reader shortcut keys. For example, "H" will move the screen reader focus to the next heading, "B" will move to the next button, "L" will move to the next list, "T" will move to the next table, etc. These are all single character shortcut keys. No modifier (such as ctrl or alt ) has to be pressed.

When the screen reader focus moves to an element that would like to have keyboard events, such as an input field, then there are options in the screen reader that "browse mode" can automatically change to "forms mode" (so that you can type stuff into a form field). JAWS will change modes automatically, but you can change your settings so that the mode must be changed manually. I suspect you have "automatic" set since it's the default.

However, this automatic change only happens for certain elements, such as an <input> . It does not happen for links because you don't normally type anything on a link (except ENTER).

So, to get keyboard events on a link, you must manually switch to "forms mode". For JAWS (it's different for NVDA), this can be done with Ins + Z . You'll hear "use virtual pc cursor OFF" when it changes. Ins + Z to toggle it back on (you'll hear "use virtual pc cursor ON").

After pressing Ins + Z , you should be able to type a letter while the focus is on the link and your event handler should run.

I found this answer: Make onKeyDown trigger an HTML button's onClick event . Try to use Jquery syntax for passing a keyboard value in the a tag. look at the next code:

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<script>

jQuery(function() {
    jQuery(window).keypress(function(e){
         if (e.keyCode === 13 && $('a:hover').length == true) {
               console.log(5 + 6);
                return false;
         }
    });
})
</script>
<a href="#"  id="descRef"><b>Search Description222</b></a>

</body>
</html>

When Space or Enter is pressed while a link has focus, JAWS catches the keystroke, prevents the default behaviour and simulates a click instead. That's why KeyDown isn't working.

The expected behaviour for links is to load a new page or move the focus to a new place on the same page. Can you change the link to a button? You can get JAWS to respect the KeyDown event on buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM