3

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?

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
Sripaul
  • 2,227
  • 9
  • 36
  • 60
  • Read this:http://stackoverflow.com/questions/6806271/javascript-check-if-ctrl-button-was-pressed – Vucko Nov 03 '12 at 11:04
  • 1
    1) What would you think if a webpage would restrict you as user? 2) Do you think Browser-Vendors would let a webpage manipulate the browser in such a way? 3) Answer your question yourself. 4) Go to your boss/customer and tell them it's not possible and altogether makes no sense and it's a big security issue. – Christoph Nov 03 '12 at 11:13
  • 1
    @Christoph Not Security but Usability issue. – GolezTrol Nov 03 '12 at 13:22
  • @Christoph: Or in short, what can be used can also be abused/misused. – Andriy M Jan 25 '13 at 06:31

3 Answers3

7

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

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 2
    Note that this does NOT work, in recent versions of either Firefox or Chrome. (Firefox 68 and Chrome 76, to be precise, and only tested on Linux). There is no keydown event triggered when you press tab, while holding ctrl down. I imagine it is a deliberate security thing, to prevent clever spoofing attacks? – Darren Cook Sep 18 '19 at 10:09
  • That sounds likely. Note this is a nearly 7 year old answer. I'll investigate and update with findings – Rory McCrossan Sep 18 '19 at 10:15
1

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.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Restricting users to go to another page is typical old-fashened application behaviour. If you want to build your app in the browser you'll have to work around some of the liberties the user has. Instead of requiring them to finish a task before moving to another tab, make it so that they can re-open and finish the task later. Besides, even if you can capture this keystroke (apparently you can detect it at least), users have other ways to change tabs as well. You can't or shouldn't restrict users like this. – GolezTrol Apr 29 '14 at 09:24
0

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.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • @RoryMcCrossan The tab key is just a regular keycode. You'd detect it as you would any other keypress, by checking the `keyCode` property. – Asad Saeeduddin Nov 03 '12 at 11:10