i have attached this function to the key pressed event with jquery but in the eventObject it returns the same char code for capitals and lower case characters entered
function keyPressed(delegates,eventdata){
var keyType = new KeyType();
var keycode = eventdata.which;
if((keycode === 0)&&(eventdata.keyCode === 46)){
keycode=46;
}
switch(keycode)
{
case keyType.enter:
break;
case keyType.backspace:
break;
case keyType.del:
break;
case keyType.space:
break;
default:
keycode = 1;
}
console.log(eventdata.charCode);
console.log(eventdata.which);
console.log(eventdata.keyCode);
//if a delegate is present call it
var delegate = delegates[keycode];
if(delegate !== null){
delegate(eventdata);
}
}
When using jQuery to detect keyboard/mouse events, you should always use
event.whichto get the normalized user input cross-browser.Slightly off-topic illustrative history, couple months ago I made an userscript in Vanilla JS which runs perfectly fine in FF and Chrome, and just now I had a report that it wasn’t working in Opera. I quickly debugged it on Opera and of course, the
event.keyCodewas returning0when it should be the key’s value.So yes, whenever you can use jQuery’s
event.whichto handle user-input, use it to save a lot of headaches.keydownandkeypressare different events and as such may report different values for different events — which may differ further into having akeyCodeand/orcharCodeproperties in different browsers.event.whichminimizes those discrepancies.