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

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

        <legend id='IDM3R'><style id='IDM3R'><dir id='IDM3R'><q id='IDM3R'></q></dir></style></legend>
        <tfoot id='IDM3R'></tfoot>

        addEventListener 用于 Canvas 上的 keydown

        addEventListener for keydown on Canvas(addEventListener 用于 Canvas 上的 keydown)
        <tfoot id='sFO3e'></tfoot>

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

          <legend id='sFO3e'><style id='sFO3e'><dir id='sFO3e'><q id='sFO3e'></q></dir></style></legend>
              <bdo id='sFO3e'></bdo><ul id='sFO3e'></ul>

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

                    <tbody id='sFO3e'></tbody>
                  本文介绍了addEventListener 用于 Canvas 上的 keydown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试制作一个响应键盘和鼠标输入的画布应用程序.我有这个代码:

                  I am trying to make a canvas app that responds to keyboard and mouse input. I have this code:

                  canvas = document.getElementById('canvas');
                  canvas.addEventListener('mousedown', function(event) {
                      alert('mousedown');
                          }, false);
                  canvas.addEventListener('keydown', function(event) {
                      alert('keydown');
                          }, false);
                  

                  每当我单击鼠标时都会出现mousedown"警报,但从未出现过keydown"警报.相同的代码在 JS Bin 上运行良好:http://jsbin.com/uteha3/66/

                  The 'mousedown' alert comes up whenever I click the mouse, but the 'keydown' alert never comes up. The same code works fine on JS Bin: http://jsbin.com/uteha3/66/

                  为什么它不能在我的页面上运行?画布不能识别键盘输入吗?

                  Why isn't it working on my page? Does canvas not recognize keyboard input?

                  推荐答案

                  编辑 - 这个答案一个解决方案,但更简单和正确的方法是设置画布元素上的 tabindex 属性(由 hobberwickey 建议).

                  Edit - This answer is a solution, but a much simpler and proper approach would be setting the tabindex attribute on the canvas element (as suggested by hobberwickey).

                  您无法聚焦画布元素.解决这个问题的一个简单方法是让你自己"关注.

                  var lastDownTarget, canvas;
                  window.onload = function() {
                      canvas = document.getElementById('canvas');
                  
                      document.addEventListener('mousedown', function(event) {
                          lastDownTarget = event.target;
                          alert('mousedown');
                      }, false);
                  
                      document.addEventListener('keydown', function(event) {
                          if(lastDownTarget == canvas) {
                              alert('keydown');
                          }
                      }, false);
                  }
                  

                  JSFIDDLE

                  这篇关于addEventListener 用于 Canvas 上的 keydown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)

                  • <legend id='nTApc'><style id='nTApc'><dir id='nTApc'><q id='nTApc'></q></dir></style></legend>

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

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

                          <tfoot id='nTApc'></tfoot>
                            <bdo id='nTApc'></bdo><ul id='nTApc'></ul>

                              <tbody id='nTApc'></tbody>