Skip to content

Instantly share code, notes, and snippets.

@Way
Created October 6, 2014 22:09
Show Gist options
  • Save Way/8e870bca0eefc1a8a5d3 to your computer and use it in GitHub Desktop.
Save Way/8e870bca0eefc1a8a5d3 to your computer and use it in GitHub Desktop.
Ghost Keyboard Writer
/**
* Ghost Keyboard Writer
* Let the keyboard write for yourself.
* Simply use the following command:
* START()
*
* To control the speed of writing, use the first parameter
* to define the delay between to characters in milliseconds.
* START(50)
*/
(function(){
// Into this field will the characters be written
$input = $('#inputfield');
// In the given case the words (character sequences) will be read
// from the given markup:
// <div id="words">
// <div id="row1">
// <span>word1</span>
// <span>word2</span>
// </div>
// </div>
var chars = [];
$('#words > #row1').find('span').each(function(){
var word = $(this).text() + ' ';
chars = chars.concat(word.split(''));
});
var newKeyEvent = function(keyCode, shiftKey) {
var event = $.Event('keyup');
event.which = event.keyCode = keyCode;
if (shiftKey) {
event.shiftKey = true;
}
return event;
};
var charToKeyCode = function(char) {
return char.toUpperCase().charCodeAt();
};
var noUpperCaseList = [228/*ä*/, 246/*ö*/, 252/*ü*/];
function action(speed, pos) {
speed = speed || 100;
pos = pos || 0;
if (chars.length <= pos) {
return;
}
setTimeout(function() {
var char = chars[pos];
var charCode = char.charCodeAt();
var shiftKey = false;
if (charCode > 96 && noUpperCaseList.indexOf(charCode) === -1) {
shiftKey = true;
}
var keyCode = charToKeyCode(char);
var event = newKeyEvent(keyCode, shiftKey);
$input.val($input.val() + char)
$input.trigger(event);
if (!window.STOP) {
action(speed, pos + 1);
}
}, speed);
}
window.START = action;
})(window);
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-git2.js"></script>
<meta charset="utf-8">
<title>Ghost Keyboard Writer</title>
</head>
<body>
<div id="words">
<div id="row1">
<span>oder</span>
<span>Ärger</span>
<span>supeR</span>
</div>
</div>
<input type="text" id="inputfield">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment