Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 369
Sripaul Avatar asked Oct 12 '25 21:10

Sripaul


2 Answers

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

like image 136
Rory McCrossan Avatar answered Oct 14 '25 10:10

Rory McCrossan


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.

like image 34
GolezTrol Avatar answered Oct 14 '25 11:10

GolezTrol