admin管理员组文章数量:1323688
I'm trying to calculate the value of an input field on various events regardless of the event caught.
I'm taking into account the events keypress keyup keydown cut paste
events (Do I forget anything?).
I'm trying to determine if the text-input INSERT
mode is on (default - on), or off. I cannot determine the mode by capturing a key event of that key, though I don't know what was the initial state.
Does anyone know a way? Preferably - a cross browser solution that includes IE8+, Chrome and Firefox.
Any solution, or in the words of Barney Stinson, "scam, con, hustle, hoodwink, gambit, flim flam, stratagem, and bamboozle" that could help me would be appreciated.
I'm trying to calculate the value of an input field on various events regardless of the event caught.
I'm taking into account the events keypress keyup keydown cut paste
events (Do I forget anything?).
I'm trying to determine if the text-input INSERT
mode is on (default - on), or off. I cannot determine the mode by capturing a key event of that key, though I don't know what was the initial state.
Does anyone know a way? Preferably - a cross browser solution that includes IE8+, Chrome and Firefox.
Any solution, or in the words of Barney Stinson, "scam, con, hustle, hoodwink, gambit, flim flam, stratagem, and bamboozle" that could help me would be appreciated.
Share edited Nov 6, 2020 at 8:00 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 14, 2013 at 21:22 EZLearnerEZLearner 1,83417 silver badges28 bronze badges 9- Wouldn't it be easier to just read the entire value each time? – Mike Christensen Commented Aug 14, 2013 at 21:24
-
With all those events, except for
keyup
, the input's value does not refresh until after the event is handled. – EZLearner Commented Aug 14, 2013 at 21:25 - Ah gotcha. Yea, I'm pretty sure the DOM doesn't expose insert mode state anywhere. – Mike Christensen Commented Aug 14, 2013 at 21:27
- I couldn't find any reference, and that's why I'm asking here :) – EZLearner Commented Aug 14, 2013 at 21:35
- 1 @EXSlaver Sorry not yet no - saw this, which only works in IE but it's for setting insert mode - haven't found an equivalent for checking the current state. – Steve Chambers Commented Aug 20, 2013 at 18:55
2 Answers
Reset to default 6 +100For IE only:
document.queryCommandValue("OverWrite");
As far as I know, Firefox and Chrome don't have overwrite mode. The insert key doesn't work in those browsers, and they are always in insert mode.
Moreover, Mac keyboard doesn't have a insert key from the beginning. So Safari also is always in insert mode.
Although I don't think you can directly access the state of the user's Insert
status, you can figure out how long the strings are when the user is editing the field.
Consider this, tied with a simple <input id="input" />
:
var i = document.getElementById('input');
document.onkeydown = function(e) {
var c = String.fromCharCode(event.keyCode);
if(null !== c.match(/\w/)) {
l = i.value.length;
}
}
document.onkeyup = function(e) {
var c = String.fromCharCode(event.keyCode);
if(null !== c.match(/\w/)) {
if(l === i.value.length) {
console.log("Keyboard insert might be off - length hasn't changed");
}
}
}
Note that there is a rudimentary check to try and isolate only letters and numbers (with the match
against the keyCode
(borrowed from here), as you don't want to perform the check against a shift key being pressed, for instance.
Basic POC here: http://jsfiddle/Xp3Jh/
I'm certain there are more in-depth checks you can do, but hopefully this helps!
本文标签: javascriptCheck if INSERT mode is ON when typing in an inputStack Overflow
版权声明:本文标题:javascript - Check if INSERT mode is ON when typing in an input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742138874a2422491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论