var value = ''; $('input').on('keypress', function(e) { if (value === e.key) { console.log('SAME'); e.preventDefault(); return; } value = e.key; }); $('input').on('keyup', function(e) { value = ''; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='text' />
We get a variable in which we write the key that we pressed. If the key is clamped (re-call keypress / keydown without calling keyup), then block. As it turned out, you better hang the event on keypress. he does not respond to special characters
"Any letters, numbers generate keypress. Control keys, such as Ctrl, Shift, F1, F2 ... - keypress does not generate." tyk