Javascript HTML5 捕获 keyCode 并写入 Canvas
我正在编写一个需要模拟文本区域的应用程序。我知道如何处理它的唯一方法是捕获按键事件上的 keyCode。如何获取该 keyCode 并支持 utf-8,将其应用到画布上?
干杯
I'm writing an application in which I need to simulate a textarea. The only way I know how to approach it is to capture the keyCode on a key event. How would one go about taking that keyCode and, supporting utf-8, apply that to the canvas?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是一个坏主意,因为文本区域免费为您提供了很多超出文本输入的内容(脱字符号、选择、剪切、粘贴、拖放、箭头键处理等),但这里有两件事您可以做需要做的:
提供一个
tabindex
属性,以便它可以接收焦点并引发关键事件;keypress
(不是keydown
)处理程序添加到元素以捕获文本输入。
代码:
This seems like a bad idea since there is an awful lot over and above text input that a textarea gives you for free (caret, selection, cut, paste, drag and drop, arrow key handling, etc.), but here's two things you need to do :
<canvas>
atabindex
attribute so that it may receive focus and hence raise key events;keypress
(notkeydown
) handler to the<canvas>
element to capture text input.Code:
使用jquery:
Using jquery:
您看过 Bespin 吗?它不仅仅是文本区域的替换,但它基本上可以满足您的需求。您当然可以查看代码和文档,或者如果它符合您的需求,就使用它。
Have you seen Bespin? It is more than just a textarea replacement, but it basically does what you want. You could certainly look into the code and documentation, or if it fits your needs, just use it.