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.