如何在javascript中禁用输入文本框上的键盘事件

问题描述:

我有一张图片,我用键盘对它进行一些操作(翻译,缩放......)。在图像上方,我有一个输入文本框,显示数据集中的图像编号。
当光标在文本框上时,如何禁用图像处理? (即禁用我分配给每个键盘的行为)。我还想能够编辑文本框(例如移动到图像编号xx)。从我在这里看到的 IPad禁止输入的关键事件,我认为解决方案将是一些东西像这样:

I have an image, and I use keyboards to apply some manipulations on it (translation, zoom ..). Above the image, I have an input text box which shows the number of the image in the data set. How can I disable the image manipulation when the cursor is on the text box? (ie disable the behavior that I assigned to each keyboard). I would also like to still be able to edit the text box(to move to image number xx for example). From what I read here IPad Disable keyevent on input, I think the solution would be something like this:

 // input text field
   var currentPosDisplay = $('<input type=text id="currentPos"  onkeypress="return  disableManipulation();" value="1" style="display:inline" >');

但是我不知道如何实现disableManipulation()以便当我按下键盘时(在文本框内),只发生默认行为(而不是图像处理)

But I don't know how to implement disableManipulation() so that when I press on a key board (inside the text box), only the default behavior occurs (and not the image manipulation)

听起来你想要让用户更改输入中的值,但是您不希望在文档(或其他某个容器)级别看到这些事件,您已经挂钩用于图像处理的键盘事件。

It sounds like you want to let the user change the value in the input, but you don't want those events to be seen at the document (or some other container) level, where you've hooked the keyboard events for image manipulation.

如果是这种情况,你可以用事件#stopPropagation

If that's the case, you can do that with Event#stopPropagation:

var currentPosDisplay = $('<input type=text id="currentPos" value="1" style="display:inline" >');
currentPosDisplay.on("keypress keydown keyup", function(e) {
    e.stopPropagation();
});