4

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
EZLearner
  • 1,614
  • 16
  • 25

2 Answers2

6

For 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.

jaeheung
  • 1,208
  • 6
  • 7
  • That's a complete answer to my question. – EZLearner Sep 10 '13 at 06:39
  • 1
    This answer is not entirely correct - unfortunately Chrome on Windows still has an overwrite mode in contenteditable elements. In form elements (input, textarea) there is no overwrite mode. Tested on Windows 7 with Crome 41. https://jsfiddle.net/sw077y41/ – aPokeIntheEye Mar 18 '15 at 15:27
  • For IE11, document.queryCommandValue("OverWrite"); is always giving me false – Alex Mi Apr 20 '17 at 08:12
3

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.net/Xp3Jh/

I'm certain there are more in-depth checks you can do, but hopefully this helps!

Community
  • 1
  • 1
jterry
  • 6,209
  • 2
  • 30
  • 36
  • 1
    That is not what I was looking for, though I try to get the value on any event of my choice, but this is a good detailed answer nonetheless. This code will not work on long key presses, but the point is clear. – EZLearner Aug 14 '13 at 22:39