简体   繁体   English

如何捕获Javascript textarea中的事件以记录所有键入的文本

[英]How to capture events in Javascript textarea for recording all typed text

I am sorry to post such a general question with little code but all googling has been unsuccessful. 我很抱歉用很少的代码发布这样一般的问题,但所有的谷歌搜索都没有成功。

I want to record the process of a document being created entirely, I am making this "document editor" 我想记录完全创建的文档的过程,我正在制作这个“文档编辑器”

If someone types Hello world and then backspaces the last 5 chars and then types Joe I want the result to be Hello Joe displayed to the user but in my array I want the following stored: 如果有人键入Hello world然后退格最后5个字符然后键入Joe我希望结果是Hello Joe显示给用户但在我的数组中我希望存储以下内容:

record[0] = 'H';
record[1] = 'e';
record[2] = 'l';
record[3] = 'l';
record[4] = 'o';
record[5] = ' ';
record[6] = 'w';
record[7] = 'o';
record[8] = 'r';
record[9] = 'l';
record[10] = 'd';
record[11] = 'Backspace';
record[12] = 'Backspace';
record[13] = 'Backspace';
record[14] = 'Backspace';
record[15] = 'Backspace';
record[16] = 'J';
record[17] = 'o';
record[18] = 'e';

My Code so far: 我的代码到目前为止:

function recordTextArea()
{
  // Record all text

  var record = [];

}

I have searched for ways to capture all this information but I just can't make it out. 我已经搜索了捕获所有这些信息的方法,但我无法解决这些问题。

I want the program to be able to record this so that I can play back the document. 我希望程序能够记录这个,以便我可以播放文档。

I am trying to create a pure Javascript solution. 我正在尝试创建一个纯Javascript解决方案。

EDIT: All I need help with is capturing keypresses and their values in a textarea. 编辑:我需要帮助的是在textarea中捕获按键及其值。

In plain JavaScript what you want is this: 在纯JavaScript中你想要的是这个:

var record = [];

document.getElementById('my_txt').onkeyup = function(e){
    record.push(String.fromCharCode(e.keyCode));
};

Live demo 现场演示

But this captures all key codes (including non-printable keys). 但这会捕获所有密钥代码(包括不可打印的密钥)。 Additionally it captures characters without knowing the case of the character (you'd have to check the state of shift and capslock keys separately). 此外,它在不知道角色情况的情况下捕获角色(您必须分别检查移位和封锁键的状态)。 One more problem is that this code is vulnerable to undo/redo operations. 另一个问题是此代码易受撤消/重做操作的影响。

Keeping this in mind you'd be better off reading the textarea content and saving the difference as previous state like this: 记住这一点,你最好还是阅读textarea内容并保存差异,就像之前的状态一样:

theTextArea.onkeyup = function(e){
    var value = new String(theTextArea.value);
    if ( record.length ) {
        var prev = record[record.length - 1];
        var diff = value.length - prev.length;
        var newEntry = {
            length: value.length
        };
        if ( diff < 0 ) {
            newEntry.txtDiff = diff;
        }
        else {
            newEntry.txtDiff = value.substring(value.length - diff);
        }
        record.push(newEntry);       
    }
    else {
        record.push({
            txtDiff: value,
            length: value.length
        });
    }
};

The data structure in the later example alows you to freely manipulate revisions of the text in the textarea. 后面的示例中的数据结构允许您自由地操作textarea中文本的修订。 See it live here . 在这里看到它

EDIT: 编辑:

Additionally you can implement an stop typing event instead of keyup . 此外,您可以实现停止键入事件而不是keyup This will decrease both: memory and cpu usage. 这将减少:内存和CPU使用率。 Since this is out of the scope of this question, you can read about it elswhere on SO, for example here . 由于这超出了这个问题的范围,你可以在SO上阅读它,例如这里

Using jQuery, you can just do: 使用jQuery,你可以这样做:

$( function() {
  var record = [];
  $( "#myText" ).on( "keypress", function( evt ) {
    record.push( String.fromCharCode( evt.which ) );
  });
});
var txtarea = document.getElementById('txtarea'),
    record = [];

txtarea.addEventListener('keydown', recordTextArea);

function recordTextArea(e){
    record.push(String.fromCharCode(e.which));
}

Simple as mentioned @Cuberto 简单如上所述@Cuberto

var record = [];
document.getElementById("mytextBox").onkeypress=
   function(event){
   record.push( String.fromCharCode( event.which ) );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM