What#39;s the best way to create key events in HTML5 canvas?(在 HTML5 画布中创建关键事件的最佳方式是什么?)
问题描述
请建议为 HTML5 画布创建关键事件的最佳方法.我不喜欢任何图书馆,但如果你认为这是最好的方法,那就去回答吧.提前致谢!
Please suggest the best way to create key events for HTML5 canvas. I don't prefer any library, but if you think that it's the best way then go answer it. Thanks in advance!
推荐答案
这将返回关键代码:
<canvas id="myCanvas" width="200" height="100" style="background:green"></canvas>
<script type="text/javascript">
window.addEventListener('keydown',this.check,false);
function check(e) {
alert(e.keyCode);
}
</script>
如果您想演示基于密钥所做的不同事情:
If you would like a demonstration of different things being done based on key:
function check(e) {
var code = e.keyCode;
//Up arrow pressed
if (code == 38)
alert("You pressed the Up arrow key");
else
alert("You pressed some other key I don't really care about.");
}
或者,如果您有一长串要使用的键,请在开关盒中进行:
Or if you have a long list of keys you'll be using, do it in a switch case:
function check(e) {
var code = e.keyCode;
switch (code) {
case 37: alert("Left"); break; //Left key
case 38: alert("Up"); break; //Up key
case 39: alert("Right"); break; //Right key
case 40: alert("Down"); break; //Down key
default: alert(code); //Everything else
}
}
这篇关于在 HTML5 画布中创建关键事件的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 HTML5 画布中创建关键事件的最佳方式是什么?
基础教程推荐
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
