Capturing keys with Javascript - Now with Shift
May 3rd, 2006
The intent was the same as the original Capturing Keys - stop signals from the backspace key and enter key from reaching the browser.
The additional challenge was detecting if the shift key was currently being used.
Now, the modier keys of Ctrl, Alt, and Shift don’t send their own key codes, they have special references within the event signal. Of course, these slightly … vary. MSIE 6 breaks Firefox, and vice versa - and though it might be possible to have a javascript detect decide between the function assignments, PHP will do the job with fewer questions.
<?php if( strpos($_SERVER['HTTP_USER_AGENT'],”MSIE”) ) { ?>
document.onkeypress = function(ev) {
var pK = ev ? ev.which : window.event.keyCode;
if (pK != 13) {return pK;}
if ( client_state.value == “chat” ){
if (event.shiftKey==1){sendprivate();}
else {sendpublic();}
}
else {sendlogin();}
return false;
}
document.onkeydown = function(ev) {
var pK = ev ? ev.which : window.event.keyCode;
if (pK == 8 ){
if(typing.value != “message” && client_state.value == “chat”){
dummy = document.getElementById(”message”);
dummy.focus();
dummy.value = dummy.value.substr(0, dummy.value.length -1);
typing.value = “message”;
return false;
}
else return pK;
}
}
<?php } else { ?>
document.onkeypress = function(event) {
var pK = event ? event.which : window.event.keyCode;
if (pK != 13 && pK != 8 ) {return pK;}
if (pK==8){
if(typing.value != “message” && client_state.value == “chat”){
dummy = document.getElementById(”message”);
dummy.focus();
dummy.value = dummy.value.substr(0, dummy.value.length -1);
typing.value = “message”;
return false;
}
else return pK;
}
else if ( client_state.value == “chat” ){
if (event.shiftKey==1){sendprivate();}
else {sendpublic();}
}
else {sendlogin();}
return false;
}
<?php } ?>
Posted in JavaScript |