detecting ctrl+tab key press in javascript/jQuery

↘锁芯ラ 提交于 2019-12-22 12:33:08

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/13208512/detecting-ctrltab-key-press-in-javascript-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!