Reputation: 8070
I'm using the event like onkeyup and onkeydown function. In that I tried to disable the printscr alone using Javascript. I Googled in that alt key ASCII is 18 and Printscr is 44.
Here is the Code that I tried to disable:
document.onkeyup = function(e)
{
if (e.which === 44)
{
alert('alt key pressed');
return false;
}
};
var isAlt = false;
document.onkeydown = function(e)
{
if (e.which === 18) {
isAlt === true;
if (((e.which === 44)) && isAlt === true)
{
alert('tested printscreen');
return false;
}
}
};
In that keyup alert is working, but return false is not firing. Also for the alt+printscr is not getting the alert also. What shall I do? Is anything where I made a mistake?
Upvotes: 1
Views: 3321
Reputation: 4399
Look at browser specific documentation for stopping events:
e.stopPropagation()
Also, in key up, the text box may have been populated with multiple characters from the key repeat. In key down, you need to store the value. In key up, restore that value if you get the 44 key. And, you need to place the key handlers on window, not document.
(function() {
var savedValue;
window.onkeyup = function(e) {
if (e.altKey && e.which === 44) {
alert('alt key pressed');
if (undefined !== savedValue) {
e.target.value = savedValue;
savedValue = undefined;
}
return;
}
};
window.onkeydown = function(e) {
if (undefined === savedValue) {
savedValue = e.target.value;
}
if (e.altKey && e.which === 44) {
alert("tested printscr");
}
};
})();
Upvotes: 1