Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable keyboard events on an input text box in javascript

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" >');

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)

like image 488
user1499220 Avatar asked Sep 06 '13 14:09

user1499220


People also ask

How do I turn off keyboard input?

Click on the keyboard you want to disable, then right-click it to reveal a list of options. 4. Click on "Disable" to disable the keyboard. If you don't have this option, you should click "Uninstall" instead.

How do I turn off keyboard events in CSS?

No, CSS cannot affect the browser's response to the keyboard. JavaScript can, but JavaScript can also be turned off. In other words: you can't do that, and even if you do then you can't count on it. Save this answer.

How do I turn off keyboard input in react?

Material-UI's <TextField> is a wrapper around react's <input> , so all you need to do is pass onKeyDown down via inputProps , which you are already using to pass min , max , and defaultValue . const [value, setValue] = useState(10); return ( <React.

What are keyboard events in JavaScript?

There are three different keyboard events in JavaScript: keydown : Keydown happens when the key is pressed down, and auto repeats if the key is pressed down for long. keypress : This event is fired when an alphabetic, numeric, or punctuation key is pressed down. keyup : Keyup happens when the key is released.


2 Answers

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.

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();
});
like image 127
T.J. Crowder Avatar answered Sep 18 '22 02:09

T.J. Crowder


You could check if the input element has focus: How do I find out which DOM element has the focus?

And if it does, don't apply your image manipulation.

Returning false from the onkeypress handler will ignore the key that was pressed, but if you have focus it sounds like you don't want that anyway.

like image 22
jozxyqk Avatar answered Sep 19 '22 02:09

jozxyqk