[removed] How to get the last two characters typed into a textarea?

后端 未结 2 1431
误落风尘
误落风尘 2021-01-14 15:16

What is the best way to grab the last two characters typed into a textarea box?

I need the last 2 characters typed NOT the last two characters of th

2条回答
  •  -上瘾入骨i
    2021-01-14 15:47

    You need to catch the keypress event on the textarea and then keep a log of keys that were pressed. Note that this is going to catch arrow keys, shift, alt, etc so if you just want characters you need to filter them out.

    Simple example:

    var keyPresses = [];
    textarea.onkeypress = function(ev){
       ev = ev || window.event;
    
       var key = ev.keyCode || ev.which;
       keyPresses.push(key);
    }
    

提交回复
热议问题