preventDefault on keydown

how it all goes to hell...

by Nikolay Petrov

HTML

<input type="text" />

CSS

/*
See https://github.com/Mottie/Keyboard/issues/106
*/

JavaScript

var $input = $('input')

$input.on('keyup keydown keypress', function (e) {
		
    console.log('e.charCode', e.charCode, e.which);
    
    var key = String.fromCharCode( e.charCode || e.which );
    
    if (e.type === 'keydown') {
    
        e.preventDefault();

        // add the string from keyCode
        // to show how inaccurate this value is on keydown/keyup
        $input.val( $input.val() + key );
    }
    
});