如何捕获网络上的击键?

发布于 2024-10-08 20:52:43 字数 1493 浏览 4 评论 0原文

使用 PHP、JS 或 HTML(或类似的东西)如何捕获按键?例如,如果用户按下 ctrl+f 甚至只是 f,就会发生某个功能。

++++++++++++++++++++++++++编辑+++++++++++++++++++++ 好吧,这是否正确,因为我无法让它工作。我为我的笨拙道歉,这是一个简单的问题,对于 jQuery 来说是个新手,并且仍在学习越来越多的关于 JS 的知识。

<script>    
var element = document.getElementById('capture');
    element.onkeypress = function(e) { 
       var ev = e || event;
       if(ev.keyCode == 70) {
          alert("hello");
       }
    }
</script>
<div id="capture">
Hello, Testing 123
</div>

++++++++++++++++++编辑++++++++++++++++++++

这里是一切,但我无法让它工作:

<link rel="icon" href="favicon.ico" type="image/x-icon">
<style>
* {
margin: 0px
}  

div {
    height: 250px;
    width: 630px;
    overflow: hidden;
    vertical-align: top;
    position: relative;
    background-color: #999;
}
  iframe {
    position: absolute;
    left: -50px;
    top: -130px;
  }
</style>
<script>
document.getElementsByTagName('body')[0].onkeyup = function(e) { 
   var ev = e || event;
   if(ev.keyCode == 70 && ev.ctrlKey) { //control+f
      alert("hello");
   }
}
</script>
<div id="capture">
Hello, Testing 123<!--<iframe src="http://www.pandora.com/" scrolling="no" width="1000" height="515"frameborder="0"></iframe>-->
</div>

+++编辑+++

感谢雅各布,我以为我已经修复了它,但是当我在 FF 和 IE 中尝试过(目前使用 chrome,确实有效),但没有成功。这个脚本只是用于一个只有我会看到的个人页面,所以这不是最重要的,但为了将来的参考,我只是想知道为什么这在 IE 或 FF 中不起作用。

Using PHP, JS, or HTML (or something similar) how would I capture keystokes? Such as if the user presses ctrl+f or maybe even just f, a certain function will happen.

++++++++++++++++++++++++EDIT+++++++++++++++++++
Ok, so is this correct, because I can't get it to work. And I apologize for my n00bness is this is an easy question, new to jQuery and still learning more and more about JS.

<script>    
var element = document.getElementById('capture');
    element.onkeypress = function(e) { 
       var ev = e || event;
       if(ev.keyCode == 70) {
          alert("hello");
       }
    }
</script>
<div id="capture">
Hello, Testing 123
</div>

++++++++++++++++EDIT++++++++++++++++++

Here is everything, but I can't get it to work:

<link rel="icon" href="favicon.ico" type="image/x-icon">
<style>
* {
margin: 0px
}  

div {
    height: 250px;
    width: 630px;
    overflow: hidden;
    vertical-align: top;
    position: relative;
    background-color: #999;
}
  iframe {
    position: absolute;
    left: -50px;
    top: -130px;
  }
</style>
<script>
document.getElementsByTagName('body')[0].onkeyup = function(e) { 
   var ev = e || event;
   if(ev.keyCode == 70 && ev.ctrlKey) { //control+f
      alert("hello");
   }
}
</script>
<div id="capture">
Hello, Testing 123<!--<iframe src="http://www.pandora.com/" scrolling="no" width="1000" height="515"frameborder="0"></iframe>-->
</div>

+++EDIT+++

Thanks to Jacob, I had thought that I had it fixed, but when I tried it in FF and IE (currently using chrome, which did work) it did not work. This script is just going to be for a personal page that only I will see, so it is not the biggest deal, but for future reference, I would just like to know why this is not working in either IE or FF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千年*琉璃梦 2024-10-15 20:52:43

当然,做到这一点的唯一方法是通过 JavaScript,您可以这样做:

window.onload = function() {
   document.getElementsByTagName('body')[0].onkeyup = function(e) { 
      var ev = e || event;
      if(ev.keyCode == 70) {//&& ev.ctrlKey) {
         //do something...
      }
   }
};

要找出您想要的特定关键代码,请参阅这篇文章:http://www.webonweboff.com/tips/js/event_key_codes.aspx

jsFiddle 示例

Sure, the only way to do this would be through JavaScript, and you'd do so like this:

window.onload = function() {
   document.getElementsByTagName('body')[0].onkeyup = function(e) { 
      var ev = e || event;
      if(ev.keyCode == 70) {//&& ev.ctrlKey) {
         //do something...
      }
   }
};

To find out the specific key code you want, see this article: http://www.webonweboff.com/tips/js/event_key_codes.aspx

jsFiddle example

梦与时光遇 2024-10-15 20:52:43

您正在寻找与按键相关的 JavaScript 事件。这里有一些烦人的浏览器不兼容问题,因此您最好使用像 jQuery 这样的 JS 库,您可以在其中使用 jQuery keypress() 方法,但您可以从 javascript onkeypress 事件中获取您想要的数据。

You're looking for the javascript events associated with key presses. There are some annoying browser incompatibilities here, so you'll be best off using a JS library like jQuery, where you can use the jQuery keypress() method, but you can get the data you want from the javascript onkeypress event.

智商已欠费 2024-10-15 20:52:43

您最好捕获窗口上的所有按键,而不是像提到的其他答案那样捕获特定元素上的击键。

所以使用原生 javascript:

window.onload = function (){
 eventHandler = function (e){
    if (e.keyCode == 70 && e.ctrlKey)
    {
      //do stuff
      //console.log(e);
    }
  }

  window.addEventListener('keydown', eventHandler, false);
}

使用 JQuery:

$(document).ready(function(){
  $(window).keydown(function (e) {
    if (e.keyCode == 70 && e.ctrlKey)
    {
        //do stuff
    }

  });
});

You are better off capturing all keys on the window rather than capturing key strokes on a specific element like other answers referred to.

so using native javascript:

window.onload = function (){
 eventHandler = function (e){
    if (e.keyCode == 70 && e.ctrlKey)
    {
      //do stuff
      //console.log(e);
    }
  }

  window.addEventListener('keydown', eventHandler, false);
}

using JQuery:

$(document).ready(function(){
  $(window).keydown(function (e) {
    if (e.keyCode == 70 && e.ctrlKey)
    {
        //do stuff
    }

  });
});
屋顶上的小猫咪 2024-10-15 20:52:43

使用 jQuery:

您可以使用 jQuery Keydown

关于捕获不同关键事件的好文章:

在 jQuery 中处理事件

编辑:

JavaScript

这里有一些很好的文章,可以在 javascript 中使用很好的演示来完成此操作:

Using jQuery:

You can do it using jQuery Keydown

Nice article on capturing different key events:

Working with Events in jQuery

EDIT:

JavaScript

Here are nice articles to do this in javascript with nice DEMO:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文