禁用 Chrome 的文本输入撤销/重做 (CTRL+Z/CTRL+Y) [英] Disable Chrome's text input undo/redo (CTRL+Z / CTRL+Y)

查看:53
本文介绍了禁用 Chrome 的文本输入撤销/重做 (CTRL+Z/CTRL+Y)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 Web 应用程序,但遇到了一个问题.您可能知道或不知道,chrome 有一个功能可以让 (尤其是文本输入)撤消"当您点击 CTRL+Z 和重做"时的功能;使用 CTRL+Y 这在普通网站中似乎是一个很好的功能,但目前我正在制作一个也使用这些键盘快捷键的图像编辑器(CTRL+Z & CTRL+Y).

i'm currently developing a web application and i encounter a problem. As you might know or not, chrome has a feature that gives <input> (especially text inputs) an "undo" function when you hit CTRL+Z and "redo" with CTRL+Y it may seem like a good feature in normal websites, but currently i'm making an image editor that also uses those keyboard shortcuts (CTRL+Z & CTRL+Y).

在我的应用程序中,我也有一个文本输入,所以当我更改文本输入的内容然后点击 CTRL+Z 以撤消我的图像编辑器中的更改时,它会在文本编辑器中撤消更改!

in my app i also have a text input, so when i change the text input's content and then hit CTRL+Z to undo the changes in my image editor, it would undo the change in the text editor instead!

这里有一个 Codepen 可以演示效果
(说明在代码笔)

here is a Codepen that would demonstrate the effect
(instruction is in the Codepen)

总而言之,我想删除 chrome 中的撤消/重做功能,我该怎么做?

So in conclusion i want to remove the undo/redo function in chrome how can i do that?

推荐答案

var ctrlDown = false;
var ctrlKey = 17, vKey = 86, cKey = 67, zKey = 90;

document.body.onkeydown = function(e) {
  if (e.keyCode == 17 || e.keyCode == 91) {
    ctrlDown = true;
  }
  if ((ctrlDown && e.keyCode == zKey) || (ctrlDown && e.keyCode == vKey) || (ctrlDown && e.keyCode == cKey)) {
    e.preventDefault();
    return false;
  }
}
document.body.onkeyup = function(e) {
  if (e.keyCode == 17 || e.keyCode == 91) {
    ctrlDown = false;
  };
};

<body>
    <input type='text' value='test' />
</body>

这行得通吗?

此处 窃取,可在 此处 作为@KadeHafen 的评论.

Stolen from here, found on here as a comment by @KadeHafen.

这篇关于禁用 Chrome 的文本输入撤销/重做 (CTRL+Z/CTRL+Y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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