I needed to implement a "trim" on whatever was pasted (remove all leading and trailing whitespace) while still allowing the use of the spacebar.
For Ctrl+V, Shift+Insert and mouse right-click Paste, here is what I found worked in FF, IE11 and Chrome as of 2017-04-22:
$(document).ready(function() {
var lastKeyCode = 0;
$('input[type="text"]').bind('keydown', function(e) {
lastKeyCode = e.keyCode;
});
// Bind on the input having changed. As long as the previous character
// was not a space, BS or Del, trim the input.
$('input[type="text"]').bind('input', function(e) {
if(lastKeyCode != 32 && lastKeyCode != 8 && lastKeyCode != 46) {
$(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
}
});
});
Two caveats:
If there is already text when the paste occurs, trimming occurs on the entire result, not just what it being pasted.
If the user types space or BS or Del and then pastes, trimming will not occur.