문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top