HTML canvas文本输入和退格 [英] HTML canvas text entry and backspace

查看:184
本文介绍了HTML canvas文本输入和退格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个canvas元素,我想通过允许用户输入文本和删除它进行交互。

I have a canvas element which I would like to make interactive by allowing users to enter text and remove it. Adding text works as expected, however deletion using the backspace key triggers the browser's back action.

如何停止此操作?

干杯,
A。

Cheers, A.

推荐答案

使用 c $ c>事件。将处理程序添加到文档中(不好,因为它会禁用页面其余部分的预期浏览器行为,而不仅仅是< canvas> 元素) c $ c>< canvas> 元素a tabindex并向其添加 keypress 处理程序。

Use the keydown event. Add the handler either to the document (bad, because it disables expected browser behaviour on the rest of the page, not just the <canvas> element) or give your <canvas> element a tabindex and add the keypress handler to it (better).

function preventBackspaceHandler(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 8) {
        return false;
    }
}

document.onkeydown = preventBackspaceHandler;

这篇关于HTML canvas文本输入和退格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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