如何捕获网络上的击键?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,做到这一点的唯一方法是通过 JavaScript,您可以这样做:
要找出您想要的特定关键代码,请参阅这篇文章: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:
To find out the specific key code you want, see this article: http://www.webonweboff.com/tips/js/event_key_codes.aspx
jsFiddle example
您正在寻找与按键相关的 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.
您最好捕获窗口上的所有按键,而不是像提到的其他答案那样捕获特定元素上的击键。
所以使用原生 javascript:
使用 JQuery:
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:
using JQuery:
使用 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: