如何捕获网络上的击键? [英] How do I capture keystrokes on the web?

查看:96
本文介绍了如何捕获网络上的击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP,JS或HTML(或类似的东西),我将如何捕获关键字?例如,如果用户按下ctrl + f或甚至可能只是f,则会发生某种功能。



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

;脚本>
var element = document.getElementById('capture');
element.onkeypress = function(e){
var ev = e ||事件;
if(ev.keyCode == 70){
alert(hello);
}
}
< / script>
< div id =capture>
您好,测试123
< / div>

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



以下是一切,但我无法实现它:

 < ; link rel =iconhref =favicon.icotype =image / x-icon> 
< style>
* {
保证金:0px
}

div {
height:250px;
width:630px;
overflow:hidden;
vertical-align:top;
职位:亲属;
背景颜色:#999;
}
iframe {
position:absolute;剩余
:-50px;
top:-130px;
}
< / style>
< script>
document.getElementsByTagName('body')[0] .onkeyup = function(e){
var ev = e ||事件; (ev.keyCode == 70&& ev.ctrlKey){//控制+ f
alert(hello);
if
}
}
< / script>
< div id =capture>
您好,测试123<! - < iframe src =http://www.pandora.com/scrolling =nowidth =1000height =515frameborder =0> ;< / iframe中> - >
< / div>

+++编辑+++



感谢雅各布,我认为我已经修好了,但是当我在FF和IE中尝试它时(当前使用的是Chrome,它确实有效),它不起作用。这个脚本只是一个个人页面,只有我会看到,所以它不是最大的交易,但为了将来的参考,我只想知道为什么这不适用于IE或FF。

解决方案

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

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

要了解所需的特定代码,请参阅此文章: http://www.webonweboff.com/tips/js/event_key_codes.aspx



jsFiddle示例


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.

解决方案

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

这篇关于如何捕获网络上的击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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