Can you distinguish between the left CTRL key and right CTRL key using keycodes in .keypress()?
This code will fire an alert if I hit either Ctrl key:
$('#text').bind('keypress', function(e) {
if(e.keyCode==17)
{
alert开发者_JS百科("Boo ya");
}
});
Any way to only fire the alert if only the left Ctrl key is pressed?
You can't, at least using the keyCode. It'll be 17 for both keys. I don't know of any other method to distinguish between the two, and in my opinion, it's unlikely that there is one.
I'm aware this question is quite old but nowadays it seems to be possible KeyboardEvent.code:
document.getElementById('i').addEventListener("keyup", function(e) {
e.target.value = e.code;
});
<input id="i" value="try keyboard here">
You should get ShiftLeft
or ShiftRight
by pressing either Shift key.
精彩评论