简体   繁体   中英

detecting ctrl+tab key press in javascript/jQuery

I have a requirement wherein I must restrict the user viewing my web page. Users should not be allowed to press Ctrl + Tab before finishing a task in the page.

Is there a way to detect Ctrl + Tab keypress in javascript/jQuery?

This code will detect CTRL+Tab:

$(document).keydown(function(e) {
    if (e.ctrlKey && e.which == 9) {
        alert("CTRL + TAB Pressed")
    }
})

Note however that CTRL+Tab functionality is controlled by the browser and you cannot stop it as this fiddle will demonstrate if you have more than one tab open:

Example fiddle

No, you cannot. That is, you can detect the keystroke, but not block the behaviour of toggling tabs. And fortunate too, because websites that do this would make browsing a real pain.

And it would defeat the purpose of a web browser. If you need restrictions like that, don't build a website, but a desktop application. But rather, if you build a browser application, make it smart enough to save its state, so users can come back to a task and finish it later if they have switched to a different tab first.

Check the ctrlKey property on the event in your handler.

$(document).on('keypress','*',function(e){alert(e.ctrlKey);})

http://jsfiddle.net/kDdzj/

You can detect it, but you can't stop it.

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