0

I have a keydown event. I can take out the keyCode from that. I wish to first prevent the default using event.preventDefault and then after some conditional check, print back the character corresponding to the keycode to where the cursor actually is.

I guess i can retreive the correct character using

var character = String.fromCharCode(e.charCode);

but how do i print it back to the iframe body also considering the fact that i may required to insert this character to already present text without disturbing the cursor position

Bhumi Singhal
  • 8,063
  • 10
  • 50
  • 76
  • I think, You should do `preventDefault` only on those special conditions you have. Using preventDefault and then inserting the text later would complicate the issue. There is no reliable way to get the exact character from they keyCode as it varies for different browser and different keyboard type. Check this http://stackoverflow.com/a/13127566/297641 answer for a workaround. – Selvakumar Arumugam Nov 29 '12 at 19:20

1 Answers1

0

You should do preventDefault only on those special conditions you have. Using preventDefault and then inserting the character later would simply complicate the issue(get caret position, inserting the character and then set the caret position).

I don't think there is a reliable way to get the exact character from they keyCode as it varies for different browser and different keyboard type.

Check this https://stackoverflow.com/a/13127566/297641 answer for a workaround.

Community
  • 1
  • 1
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • You may be able to reliably get the exact character if you get the current value of the field and then compare it to the value within a setTimeout with a 0ms delay. You would just have to also take into account the possiblity of copy/paste, backspace, arrow keys, escape, tab, etc. – Kevin B Nov 29 '12 at 19:33
  • @KevinB How do we get the current value `onkeydown` from the `e.which` **reliably**? – Selvakumar Arumugam Nov 29 '12 at 19:35
  • @KevinB I am not sure.. I was trying to understand the logic you mentioned on the first comment _get the exact character if you get the current value of the field and then compare it to the value within a setTimeout with a 0ms delay_ .. I think You can do that `onkeypress`, but not on `onkeydown` – Selvakumar Arumugam Nov 29 '12 at 19:38
  • you should be able to see my deleted answer, basically you wold get the value before the keypress outside of the setTimeout, and then the altered value due to the keypress inside of the setTimeout. you could compare those two values to find differences. For example, if the `a` key was pressed, the value would have a new `a` character somewhere in the value. – Kevin B Nov 29 '12 at 19:40
  • For reference, what i'm doing with keydown is exactly what jQuery UI autocomplete does to make it feel so fast. If they waited for keypress, there would be a larger delay in getting results. – Kevin B Nov 29 '12 at 19:42
  • @KevinB Well, that is the whole point.. `keypress` and `keydown`. How do you prevent the `keydown` if the comparison logic is in the `setTimeout`.. by that time the entry is already made.. You can very well replace with old value.. but the caret positioning is little complicated... but I agree it is doable. – Selvakumar Arumugam Nov 29 '12 at 19:42
  • you wouldn't necessarily have to prevent it, just undo it. – Kevin B Nov 29 '12 at 19:43
  • @Vega - My original idea was to use preventDefault only when the condition was satisfied. But i have a lot of code before which i can decide on this and by the time the code reaches event.preventDefault() it does not execute/ – Bhumi Singhal Nov 29 '12 at 19:57
  • @BhumiSinghal I don't think so.. It should preventDefault as long as it is executed, irrespective of where it is inside the handler. – Selvakumar Arumugam Nov 29 '12 at 20:10
  • @Vega - please look at my question [link](http://stackoverflow.com/questions/13630785/preventdefault-and-stoppropagation-not-working-for-keypress). I am a little clueless here. – Bhumi Singhal Nov 30 '12 at 05:15