<bdo id='2SPOt'></bdo><ul id='2SPOt'></ul>
  • <tfoot id='2SPOt'></tfoot>

    1. <i id='2SPOt'><tr id='2SPOt'><dt id='2SPOt'><q id='2SPOt'><span id='2SPOt'><b id='2SPOt'><form id='2SPOt'><ins id='2SPOt'></ins><ul id='2SPOt'></ul><sub id='2SPOt'></sub></form><legend id='2SPOt'></legend><bdo id='2SPOt'><pre id='2SPOt'><center id='2SPOt'></center></pre></bdo></b><th id='2SPOt'></th></span></q></dt></tr></i><div id='2SPOt'><tfoot id='2SPOt'></tfoot><dl id='2SPOt'><fieldset id='2SPOt'></fieldset></dl></div>
      <legend id='2SPOt'><style id='2SPOt'><dir id='2SPOt'><q id='2SPOt'></q></dir></style></legend>

      1. <small id='2SPOt'></small><noframes id='2SPOt'>

        在javascript中检测按键的最简单方法

        时间:2023-09-07

                <tbody id='JD0Lm'></tbody>
              <legend id='JD0Lm'><style id='JD0Lm'><dir id='JD0Lm'><q id='JD0Lm'></q></dir></style></legend>

              • <bdo id='JD0Lm'></bdo><ul id='JD0Lm'></ul>
                  <tfoot id='JD0Lm'></tfoot>

                  <small id='JD0Lm'></small><noframes id='JD0Lm'>

                  <i id='JD0Lm'><tr id='JD0Lm'><dt id='JD0Lm'><q id='JD0Lm'><span id='JD0Lm'><b id='JD0Lm'><form id='JD0Lm'><ins id='JD0Lm'></ins><ul id='JD0Lm'></ul><sub id='JD0Lm'></sub></form><legend id='JD0Lm'></legend><bdo id='JD0Lm'><pre id='JD0Lm'><center id='JD0Lm'></center></pre></bdo></b><th id='JD0Lm'></th></span></q></dt></tr></i><div id='JD0Lm'><tfoot id='JD0Lm'></tfoot><dl id='JD0Lm'><fieldset id='JD0Lm'></fieldset></dl></div>
                  本文介绍了在javascript中检测按键的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 javascript 游戏的想法(我将使用 EaselJS 制作它),我必须检测按键.在互联网上环顾四周后,我看到了很多建议(使用 window.onkeypress,使用 jQuery 等),但几乎每个选项都有反驳.你们有什么建议?使用 jQuery 听起来很容易,但我几乎没有使用该库的经验(而且我也不是 javascript 的资深人士)所以我宁愿避免使用它.如果 jQuery 是最好的选择,有人可以举一个很好的例子(解释会很棒)吗?

                  I have an idea for a game in javascript (I'm going to make it with EaselJS) and I'll have to detect keypresses. After looking around on the internet I've seen a lot of suggestions (use window.onkeypress, use jQuery, etc.) but for almost every option there's a counterargument. What do you guys suggest? Using jQuery for this sounds easy enough but I have virtually no experience with that library (and I'm not particulary a veteran at javascript either) so I'd rather avoid it. If jQuery is the best option, can someone give a good example (with explanation would be awesome) of how to do this?

                  我想这被问了很多,但我找不到任何明确的答案.提前致谢!

                  I guess this gets asked a lot but I couldn't find any clear answers. Thanks in advance!

                  推荐答案

                  用纯Javascript,最简单的是:

                  With plain Javascript, the simplest is:

                  document.onkeypress = function (e) {
                      e = e || window.event;
                      // use e.keyCode
                  };
                  

                  但是有了这个,你只能为事件绑定一个处理程序.

                  But with this, you can only bind one handler for the event.

                  此外,您可以使用以下方法将多个处理程序潜在地绑定到同一事件:

                  In addition, you could use the following to be able to potentially bind multiple handlers to the same event:

                  addEvent(document, "keypress", function (e) {
                      e = e || window.event;
                      // use e.keyCode
                  });
                  
                  function addEvent(element, eventName, callback) {
                      if (element.addEventListener) {
                          element.addEventListener(eventName, callback, false);
                      } else if (element.attachEvent) {
                          element.attachEvent("on" + eventName, callback);
                      } else {
                          element["on" + eventName] = callback;
                      }
                  }
                  

                  在任何一种情况下,keyCode 在浏览器中都不一致,因此需要检查和找出更多内容.注意 e = e ||window.event - 这是 Internet Explorer 的正常问题,将事件放在 window.event 中,而不是将其传递给回调.

                  In either case, keyCode isn't consistent across browsers, so there's more to check for and figure out. Notice the e = e || window.event - that's a normal problem with Internet Explorer, putting the event in window.event instead of passing it to the callback.

                  参考资料:

                  • https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/keypress
                  • https://developer.mozilla.org/en-US/docs/DOM/EventTarget.addEventListener

                  使用 jQuery:

                  $(document).on("keypress", function (e) {
                      // use e.which
                  });
                  

                  参考:

                  • http://api.jquery.com/on/

                  除了 jQuery 是一个大型"库之外,jQuery 确实有助于解决浏览器之间的不一致问题,尤其是窗口事件……这是不可否认的.希望很明显,我为您的示例提供的 jQuery 代码更优雅、更短,但以一致的方式完成了您想要的工作.您应该能够相信 e (事件)和 e.which (键码,用于知道按下了哪个键)是准确的.在纯 Javascript 中,除非您执行 jQuery 库内部所做的所有事情,否则很难知道.

                  Other than jQuery being a "large" library, jQuery really helps with inconsistencies between browsers, especially with window events...and that can't be denied. Hopefully it's obvious that the jQuery code I provided for your example is much more elegant and shorter, yet accomplishes what you want in a consistent way. You should be able to trust that e (the event) and e.which (the key code, for knowing which key was pressed) are accurate. In plain Javascript, it's a little harder to know unless you do everything that the jQuery library internally does.

                  注意有一个 keydown 事件,它不同于 keypress.您可以在此处了解有关它们的更多信息:onKeyPress Vs.onKeyUp 和 onKeyDown

                  Note there is a keydown event, that is different than keypress. You can learn more about them here: onKeyPress Vs. onKeyUp and onKeyDown

                  至于建议使用什么,如果您准备学习该框架,我肯定会建议使用 jQuery.同时,我想说你应该学习 Javascript 的语法、方法、特性,以及如何与 DOM 交互.一旦你理解了它是如何工作的以及发生了什么,你应该更容易使用 jQuery.对我来说,jQuery 让事情变得更加一致和简洁.最后,它是 Javascript,并包装了语言.

                  As for suggesting what to use, I would definitely suggest using jQuery if you're up for learning the framework. At the same time, I would say that you should learn Javascript's syntax, methods, features, and how to interact with the DOM. Once you understand how it works and what's happening, you should be more comfortable working with jQuery. To me, jQuery makes things more consistent and is more concise. In the end, it's Javascript, and wraps the language.

                  另一个非常有用的 jQuery 例子是 AJAX.浏览器与 AJAX 请求的处理方式不一致,因此 jQuery 抽象了这一点,因此您不必担心.

                  Another example of jQuery being very useful is with AJAX. Browsers are inconsistent with how AJAX requests are handled, so jQuery abstracts that so you don't have to worry.

                  以下几点可能有助于做出决定:

                  Here's something that might help decide:

                  • http://www.jscripters.com/jquery-disadvantages-and-advantages/

                  这篇关于在javascript中检测按键的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:你如何判断大写锁定是否在使用 JavaScript? 下一篇:用 jQuery 模拟按键

                  相关文章

                  最新文章

                • <i id='rFdAt'><tr id='rFdAt'><dt id='rFdAt'><q id='rFdAt'><span id='rFdAt'><b id='rFdAt'><form id='rFdAt'><ins id='rFdAt'></ins><ul id='rFdAt'></ul><sub id='rFdAt'></sub></form><legend id='rFdAt'></legend><bdo id='rFdAt'><pre id='rFdAt'><center id='rFdAt'></center></pre></bdo></b><th id='rFdAt'></th></span></q></dt></tr></i><div id='rFdAt'><tfoot id='rFdAt'></tfoot><dl id='rFdAt'><fieldset id='rFdAt'></fieldset></dl></div>
                  • <bdo id='rFdAt'></bdo><ul id='rFdAt'></ul>
                    <legend id='rFdAt'><style id='rFdAt'><dir id='rFdAt'><q id='rFdAt'></q></dir></style></legend>

                      <small id='rFdAt'></small><noframes id='rFdAt'>

                      <tfoot id='rFdAt'></tfoot>