0

Are there other ways to write text with keycodes apart from these two methods? However, the bad part by the following methods is that they cannot handle shortcuts like shift + a...

1. Method

Use a json-object filled with keycodes and characters, rebuild then the text while iterating over them and compare with typed keycodes.

example (only for demonstration purposes):

var chars = {
  65: 'a',
  66: 'b',
  67: 'c',
  68: 'd',
  69: 'e'
  etc..
},
keys = {};

...push some keycodes to `keys`-array...
e.g: keys[(+new Date())] = 65;
     keys[(+new Date())] = 66;
     keys[(+new Date())] = 67;

...fill the output-string
var mark = null,
    timeout = 0,
    str = "";

    for(var t in keys) {
        if(mark) {
            timeout = tS - mark;
        } else {
            timeout = 0;
            mark = tS;
        }

        setTimeout(function(){
            str += keys[t];
        }, timeout);
    }

...return the string:
elem.text(str);

2. Method

Create an empty/shadow input and fill the values from it into a string (I'll not explain this method further, you can read here about it: https://stackoverflow.com/a/8072440/1250044

3. Method

Not a real way but it should be noted:

var str = String.fromCharCode(e.which);

A good way

I think the best way to do it it will be to trigger the keyevent like so:

$(document).trigger({type: 'keydown', which: x});

The disadvantage is, that this will not trigger a real keydown, so writing this way is not possible.

Community
  • 1
  • 1
yckart
  • 32,460
  • 9
  • 122
  • 129
  • Those are pretty much the best ways out there. I'd suggest going for method #2, since it's the most reliable with capital letters etc. – Cerbrus Jan 07 '13 at 13:30
  • @Cerbrus ...me too, however the problem is that it does not result in good performance when the textarea's value gets bigger. – yckart Jan 07 '13 at 13:34
  • What exactly do you mean with "bad performance"? What's happening? – Cerbrus Jan 07 '13 at 13:35
  • @Cerbus *it takes much storage because when the textarea's value gets bigger, it gets duplicated more and more until it becomes a huge 200MB file after a while of typing...* – yckart Jan 07 '13 at 13:38

0 Answers0