JavaScript的浏览器捕捉快捷键(Ctrl + T / N / W) [英] javascript capture browser shortcuts (ctrl+t/n/w)

查看:392
本文介绍了JavaScript的浏览器捕捉快捷键(Ctrl + T / N / W)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能捕捉到这些快捷方式?


  • <大骨节病>控制 + <大骨节病> N

  • <大骨节病>控制 + <大骨节病> T

  • <大骨节病>控制 + <大骨节病>是W

我试过,但它不工作:

  $(窗口).keydown(函数(事件){
  的console.log(event.key code);
  。事件preventDefault();
});

当我preSS <大骨节病> T 它显示了 84 在控制台中,但如果我preSS <大骨节病>控制 + <大骨节病> T 这说明不了什么,并打开一个新的标签。

我想捕捉这些快捷方式和prevent任何浏览器的行动。


解决方案

捕获<大骨节病>控制在Javascript中的键盘事件

样品code:

  $(窗口).keydown(函数(事件){
  如果(event.ctrlKey&安培;&安培; event.key code == 84){
    的console.log(嘿按Ctrl + T事件所捕捉!);
    。事件preventDefault();
  }
  如果(event.ctrlKey&安培;&安培; event.key code == 83){
    的console.log(嘿按Ctrl + S事件所捕捉!);
    。事件preventDefault();
  }
});


火狐

<子>(6.0.1测试)

在Firefox中这两个事件侦听器的工作原理。如果preSS <大骨节病>控制 <大骨节病> T 或<大骨节病>控制 <大骨节病>取值 keycombinations,您将获得在控制台上这两个消息,和浏览器不会打开一个标签,也没有要求保存。

据intresting,如果你使用警报而不是的console.log 事件。preventDefault()不工作,并打开一个新的标签,或者要求保存。也许这个bug需要得到修复。


Chrome3

在Chrome 3的它就像在Firefox。


Chrome4浏览器

<子>(测试)


  

在Chrome4浏览器,某些控制键组合已预留浏览器
  使用仅,不能再由客户端JavaScript被截获
  在网页中。结果,
  这些限制没有在Chrome3存在,并与这两个不一致
  Firefox3的/ 3.5和IE7 / 8(在Windows上)。


在Chrome 4的工作原理类似地到Firefox,除了一些键盘组合:


  • <大骨节病>控制 <大骨节病> N


  • <大骨节病>控制 <大骨节病>移 <大骨节病> N


  • <大骨节病>控制 <大骨节病> T


  • <大骨节病>控制 <大骨节病>移 <大骨节病> T


  • <大骨节病>控制 <大骨节病>是W


  • <大骨节病>控制 <大骨节病>移 <大骨节病>是W


这些组合不能得到的Javascript捕获的嵌入插件可以捕获这些。例如,如果你专注于YouTube视频和preSS <大骨节病>控制 <大骨节病> T ,浏览器将不会打开一个新的标签。


IE7 / 8

它像Firefox或者Chrome3。


IE9

<子>(测试)

IE9是一个败家子​​了,因为忽略了最低允许JavaScript捕获任何<大骨节病>控制 <大骨节病>?键盘事件。我与许多键盘组合(R,T,P,S,N,T)和既不工作进行测试。还嵌入应用程序无法捕获事件。测试了YouTube视频。


感谢 @Lime 为伟大的链接

Is it possible to capture these shortcuts?

  • Ctrl+N
  • Ctrl+T
  • Ctrl+W

I tried this but it doesn't work:

$(window).keydown(function(event) {
  console.log(event.keyCode);
  event.preventDefault();
});

When I press T it shows 84 in the console, but if I press Ctrl+T it shows nothing, and opens a new tab.

I would like to capture these shortcuts and prevent any browser action.

解决方案

Capturing Ctrl keyboard events in Javascript

Sample code:

$(window).keydown(function(event) {
  if(event.ctrlKey && event.keyCode == 84) { 
    console.log("Hey! Ctrl+T event captured!");
    event.preventDefault(); 
  }
  if(event.ctrlKey && event.keyCode == 83) { 
    console.log("Hey! Ctrl+S event captured!");
    event.preventDefault(); 
  }
});


Firefox

(6.0.1 tested)

In Firefox both event listener works. If you press CtrlT or CtrlS keycombinations, you will get both message on the console, and the browser wont open a tab, nor ask for save.

It is intresting that if you use alert instead of console.log the event.preventDefault() not works, and opens a new tab or asks for save. Maybe this bug needs to get fixed.


Chrome3

In Chrome 3 it works like in Firefox.


Chrome4

(tested)

In Chrome4, certain control key combinations have been reserved for browser usage only and can no longer be intercepted by the client side JavaScript in the web page.
These restrictions did not exist in Chrome3 and are inconsistent with both Firefox3/3.5 and IE7/8 (on Windows).

In Chrome 4 it works similary to Firefox, except some keyboard combination:

  • CtrlN

  • CtrlShiftN

  • CtrlT

  • CtrlShiftT

  • CtrlW

  • CtrlShiftW

These combinations cannot get captured by Javascript, but embed plugins can capture these. For example if you focus in a Youtube video and press CtrlT, the browser won't open a new tab.


IE7/8

It works like in Firefox or Chrome3.


IE9

(tested)

IE9 is a black sheep again, because it dosen't allow javascript to capture any Ctrl? keyboard event. I tested with many keyboard combination (R,T,P,S,N,T) and neither worked. Also embed applications can't capture the event. Tested with Youtube videos.


Thanks to @Lime for the great link.

这篇关于JavaScript的浏览器捕捉快捷键(Ctrl + T / N / W)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆