简体   繁体   中英

Swap Enter Key for Tab Key

So I've got this javascript:

<script type="text/javascript">
    function OnKeyPress() {
        var charCode = window.event.keyCode;
        if (charCode == 13) {                
            window.event.keyCode = 9;
        }
    } document.onkeypress = OnKeyPress;
</script>

The idea is to catch an enter key press, and switch it to tab key press. And it half works - it catches the enter key. But it doesn't make it register as a tab key. I've tried using other keycodes as well (18 for alt) to confirm I wasn't just not seeing the tab happen.

Can anyone see what the problem is? Working in ASP.NET fwiw.

Thanks!

You can't change the keyCode and have it trigger that event instead. It's just a captured value at that point. You might be able to obtain the effect by calling a function that simulates the desired key press event and canceling the current event instead.

You'll have to create the event handler for it.

Take a look:

How to trigger event in JavaScript?

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