Javascript - Disable Key Down selectively - javascript

My application has a image palette. I am using key board shortcuts to move next and previous images. The following is the code:
window.addEventListener('keydown', function(e) {
if (e.which == 37 || e.which == 65) {
beforeAfterImages(1);
gtfval();
}
if(e.which == 39 || e.which == 68) {
beforeAfterImages(2);
gtfval();
}
})
In the same page, I have a button, that opens a modal window. The modal window has fields for text input. During text input When I press a or d the image palette behind the modal keeps moving. Can I disable the keydown functions, when I open the modal window. I tried the following:
$('#myModal').click(function() {
window.addEventListener('keydown', function(e) {
if (e.which == 37 || e.which == 65) {
return false;
}
if(e.which == 39 || e.which == 68) {
return false;
}
})
});

When you open the modal, you can edit a variable, like var isModalOpen. Then check that everytime the user presses the keys:
window.addEventListener('keydown', function(e) {
if(modalIsOpen) return; // this
if (e.which == 37 || e.which == 65) {
beforeAfterImages(1);
gtfval();
}
if(e.which == 39 || e.which == 68) {
beforeAfterImages(2);
gtfval();
}
})

Related

Disable print screen option in IE,but the code works fine with google chrome

Print screen option has been disabled in Google Chrome with the below js code.
document.onkeydown = keydown;
document.onkeyup = keyup;
function keydown(e) {
console.log("key down triggered");
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
if (e.keyCode == 44 || e.keyCode == "44" || e.which == 44 || e.which == "44") {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.ctrlKey && (e.key == "P" || e.key == "C" || e.key == "A" || e.key == "p"||e.key == "c" || e.key == "a" || e.charCode == 16 || e.charCode == 112 ||e.keyCode == 80) || (e.keyCode == 44) || (e.keyCode == 123)) {
//alert("Inspect element & Print &cut/copy option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.keyCode > 111 && e.keyCode < 124) {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.key == "F11" || e.key == "f11") {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
}
function keyup(e) {
debugger;
console.log("key up triggered");
if (e.keyCode == 44 || e.keyCode == "44" || e.which == 44 || e.which =="44") {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.keyCode > 111 && e.keyCode < 124) {
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.key == "F11" || e.key == "f11") {
//alert("Function option is restricted");
e.cancelBubble = true;
e.preventDefault();
e.stopImmediatePropagation();
}
The above code works fine in Google Chrome.key code 44 for Print screen option is used.In IE all the function keys have been restricted,but the keyup and keydown function is not getting triggered,when in IE browser alone.
What is the alternate way to handle print screen option in IE Browser using jquery.?
And additionally the above code fails to prevent print screen when the user opens a bootbox alert pop up inside a web application.
Suggest a solution for the above 2 scenarios.
Don't.
No matter how clever your code, you can't stop me from focussing a different window on my second monitor and hitting Print Screen there to get a copy of whatever's on-screen that you're trying to stop me from screenshotting.
Or I could just open the browser console (even if you block F12 / Ctrl+Shift+I, I can still get there via the browser's menus) and type document.onkeydown = document.onkeyup = null; and break your entire guard.
It's not often that I "answer" a question by completely shutting it down, but you are wasting your time.

Apply custom validation to contact form 7

I am new to wordpress developement.I wabt to apply my own validation to contatc form 7.Adding same in Header.php file but getting no result.
adding this for mobile number validation in header.php file
<script type="text/javascript">
$("#field-mobile").keydown(function(event) { // Allow only backspace and delete
if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9) { // let it happen, don't do anything
} else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
</script>
That is so simple, the script does not work since your code is run before your input text is exist on DOM.
You should add $(document).ready() function then put your block of codes in that function.
I guess this is should work now:
$(document).ready(function() {
$('#field-mobile').bind('keypress', function(e) {
if (e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 9) { // let it happen, don't do anything
} else {
console.log('else block')
if ((e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
}
});
});

Detect keycode ALT+L via Javascript

I try to detect if a user presses F12 or ALT + L.
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123 || (event.keyCode == 18 && event.keyCode == 76)) {
//do anything
return false;
}
}
What am I doing wrong?
event.keyCode contains only one value. You can use event.altKey do detect if the alt key is pressed.
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123 || (event.keyCode === 76 && event.altKey)) {
//do something
return false;
}
}
The standard way is to create a bool to detect if the 'alt' key is currently held down and then a separate if to detect if that is true and if the L key as just been pressed - see the fiddle:
http://jsfiddle.net/L4cb9/1
var held = false;
...
else if (event.keyCode == 18) {held = true;}
if (held == true && event.keyCode == 76) {
alert();
}
...
document.onkeyup = function(event) {
if (event.keyCode == 18) {held = false;}
}
This is applicable to holding any combination of keys - you can create an array for multiple key holds greater than two:
held = [];
...
if (event.keyCode == i) {held[i] = true;}
...
and so on

How to disable Ctrl+U using Javascript

I just want to disable Ctrl+U and Ctrl+C event. The primary purpose of doing this is, preventing users from downloading any image or copying content from my website easily i.e. pressing Ctrl+U for viewing my webpage's source code or pressing Ctrl+C for copying content directly from my webpage.
Currently, I am using this piece of code but it disables my entire keyboard
<script>
/*function check(e)
{
alert(e.keyCode);
}*/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117)) {//Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
}
return false;
};
</script>
Your problem is the return statement.
Though I would suggest you use addEventListener and the like, this is a working copy of your code:
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
alert('not allowed');
return false;
} else {
return true;
}
};
This is finally what I got to disable Ctrl+U:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
return false;
} else {
return true;
}
};
$(document).keypress("u",function(e) {
if(e.ctrlKey)
{
return false;
}
else
{
return true;
}
});
</script>
Its simple just use the following code it will disable only Ctrl+U while Ctrl+C, Ctrl+V, Ctrl+S etc will works fine:
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 85 )) {
return false;
}
};
</script>
try check this link from jsfiddle.
js
shortcut.add("Ctrl+U",function(){
alert('Sorry\nNo CTRL+U is allowed. Be creative!')
}),
it will simple show and error when you try to hit Ctrl+U on your keybord
But check the link, there is alot of code
To disable right click
document.addEventListener('contextmenu', event => event.preventDefault());
To disable F12 options
document.onkeypress = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
return false;
}
}
document.onmousedown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
return false;
}
}
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
return false;
}
}
To Disable ctrl+c, ctrl+u
jQuery(document).ready(function($){
$(document).keydown(function(event) {
var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
if (event.ctrlKey && (pressedKey == "c" || pressedKey == "u")) {
alert('Sorry, This Functionality Has Been Disabled!');
//disable key press porcessing
return false;
}
});
});
This will Disable Ctrl + U and Right-click (make sure it's at the top of all your code) :
<!-- Disable CTRL U and Right Click -->
<script>
document.onkeydown = function(e) {
if (e.ctrlKey && e.keyCode === 85) {
return false;
}
};
</script>
<body oncontextmenu="return false;"></body>
<!-- disable CTRL U and Right Click -->
you can use the below script
<script>
/*function check(e)
{
alert(e.keyCode);
}*/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117 || e.keycode === 17 || e.keycode === 85)) {//ctrl+u Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
}
return false;
};
</script>
Its simple just use the following code it will disable only Ctrl+U while Ctrl+C, Ctrl+V, Ctrl+S etc will works fine: but it will disable your page source too.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript"> $(function () { $(this).bind("contextmenu", function (e) { e.preventDefault(); }); }); </script>
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 85 )) {
return false;
}
};
</script>

jQuery detect keyboard press then focus to textbox

I have jQuery function that detect if some key in keyboard pressed. So far that worked, example if I press "P" then it will focus to textbox.
$(document).keyup(function(e)
{
if (e.keyCode == 51 || e.keyCode == 52 || e.keyCode == 80 || e.keyCode == 65)
{
$("#code_read_box").focus();
}
});
Now I want when I press "P", "P" will set too to textbox.
<input type="text" id="code_read_box" value=""/>
Any advice ?
DEMO here
Use String.fromCharCode:
String.fromCharCode(e.keyCode)
Demo: http://jsfiddle.net/hw2g5/
Your code:
$(document).keyup(function(e) {
if (e.keyCode == 51 || e.keyCode == 52 || e.keyCode == 80 || e.keyCode == 65) {
$("#code_read_box").focus();
$("#code_read_box").val(String.fromCharCode(e.keyCode));
}
});
If you want to append values, so:
$("#code_read_box").val($("#code_read_box").val() + String.fromCharCode(e.keyCode));
Solved.
$(document).keyup(function(e) {
var key = String.fromCharCode(e.keyCode);
if (key.match(/[34PA]/) && !$('input').is(':focus')) {
$('input').focus();
$('input').val($('input').val() + key);
}
});

Categories

Resources