检测哪个键被按下 - 这是一个好方法吗? [英] Detecting which key is pressed - is this a good way?

查看:105
本文介绍了检测哪个键被按下 - 这是一个好方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我在互联网上发现了一个简单的代码...找不到它的特殊网站,但代码是在2005年制作的。所以,我想知道这是否是一种很好的方法:

这是在HTML中:

 < body onload = document.body.focus(); onkeyup =return keypressed(event)> 

当按键启动时,它会在JavaScript中调用此函数:

  function keypressed(e){
var intKey =(window.Event)? e.which:e.keyCode;
if(intKey == 13){// 13 =输入密钥
//如果点击了键,会发生什么
}
//在这里,您可以添加if你想要的关键笔画句子! (与第一个相同)

返回true;
}

我的意思是代码是7岁(也许更多!)我认为这可能有点过时!



我的问题:


  1. 有没有更好的方法来做到这一点,或者这仍然是一个好方法? (我不想为这段代码使用jQuery,因为我不想只为一个函数使用它)

  2. 我真的需要这个代码: onload =document.body.focus();? (我没有编写这段代码,所以我不知道。)

    您不需要加载处理程序,该页面在加载后已经集中,并且/或者如果有人在任何位置按键入在页面上它也将被聚焦。

    如果你需要键值,处理程序取决于你想用它做什么。你显示的代码是一个输入处理程序。如果您希望文本输入字段在输入时输入,则将处理程序添加到该字段效率更高。

    此外,最好从脚本中做到这一点( unnobtrusive javascript 用法)。一个 keypress 处理程序就足够了,否则 enter 值将成为输入值的一部分。例如:

      [someTextInput] .onkeypress = function(e){
    e = e ||事件; ((e.which || e.keyCode)=== 13){
    // do something
    }
    }
    >

    或者您可以使用 addEventListener / attachEvent 见这里)。


    I found a simple code on the internet a while ago... can't find the spesific site for it, but the code was made in 2005. So, I wondered if this was a good way of doing this:

    This is in the HTML:

    <body onload="document.body.focus();" onkeyup="return keypressed(event)">
    

    When the key is up it calls this function in JavaScript:

    function keypressed(e) {
        var intKey = (window.Event) ? e.which : e.keyCode;
        if (intKey == 13) { // 13 = enter key
            //something happens if the key is clicked
        }
            // here, you can add as many if sentences for key strokes as you want! (same as first)
    
    return true;
    }
    

    What I mean is that the code is 7 years old (maybe more!) and I thought it might be a little or very outdated!

    My questions:

    1. Is there a better way of doing this or is this still a good way? (I don't want to use jQuery for this code, because I don't want to use it just for one function)

    2. Do I really need this code: onload="document.body.focus();"? (I have not programmed this code, so I don't know.)

    解决方案

    You don't need the load handler, the page will be already focused after it loads, and/or if someone presses enter anywhere on the page it will be focused too.

    If you need the keyup handler depends on what you'd like to do with it. The code you show is an enter handler. If you want a text input field to act on typing enter, it's more efficient to add the handler to that field.

    Furthermore, it's better to do that from within your scripting (a part of unnobtrusive javascript usage). A keypress handler would be sufficient, because otherwise the enter value would become part of the input value. Something like:

    [someTextInput].onkeypress = function(e){
      e = e || event;
      if ( (e.which || e.keyCode) === 13 ){
         // do things
      }
    }
    

    or you can use addEventListener/attachEvent (See here).

    这篇关于检测哪个键被按下 - 这是一个好方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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