I recommend to use the function below getCharCode
to redeem the event code and then perform the treatment. The reason you have created this function is, in some browsers rescues the value of the event code with keyCode
, in others with which
.
So I’ve created a wrapper for both of you, so you’ll have a better browser compatibility. To view the code of each key, click on the field and press the keys.
I put a console.log
to present the results. With the code in hand, just listen to events as our colleagues informed or even follow the example of this code.
function getCharCode(e) {
e = (e) ? e : window.event, charCode = null;
try {
charCode = (e.which) ? e.which : e.keyCode;
return charCode;
} catch (err) {
return charCode;
}
}
$(function() {
$('input').on('keyup', function(e) {
console.log("Tecla pressionada %s (%s)", e.key, getCharCode(e));
});
});
input {
height: 100px;
font-size: 40px;
line-height: 100px;
max-width: 100%;
width: 100%;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="" placeholder="Clique aqui e pressione as teclas" />
Try asssim $(Document). keyup(Function(e) { if (e. keycode == 13) $('. save'). click(); // enter if (e. keycode === 27) $('. Cancel'). click(); // Esc });
– PauloHDSousa
@Paulohdsousa worked out! if you want you can put as answer
– Maurício Krüger