The below code works fine, but if I click double Ctrl+u then it opens all. How can I disable all?
Ctrl+u, Ctrl+s, right-click, F12 key and more key for hide code?
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;
};
you have to put e.stopImmediatePropagation();
/*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');
e.stopImmediatePropagation();
}
return false;
};
Try to use the e.preventDefault() function. it will stop the browser to do the default actions when in this case a key combination has been pressed.
The key code for the F12 button is 123. To detect the 'contextmenu' event (user clicks right button), you also have to use the preventdefault function to avoid opening the contextmenu. Maybe this will help you:
Live preview: https://jsfiddle.net/cmLf34h3/1/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117) || e.keyCode === 123) { //Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
e.preventDefault();
}
return false;
};
window.oncontextmenu = function (e)
{
alert("You have tested if your right-mousebutton is still working. This alert confirms it's still working, have a nice day!")
e.preventDefault();
return false; // cancel default menu
}
Source for the right-click function: Is right click a Javascript event?
Note: You cannot 100% prevent these actions, there is always a backdoor to bypass this.
I hope this helps
Related
I am trying to disable the browser search functionlity and at the same time i want to focus my own custom search box in website.
here is code for the same.
document.addEventListener("keydown", function(e) {
if (e.keyCode == 70 && e.ctrlKey || e.keyCode === 114) {
document.getElementById("myInput").focus();
}
e.preventDefault();
})
it disable the browser search feature and focus my custom search bar but it does not allow me to type anything in my custom search input.
The preventDefault() should be used only if the relevant key is the ctrl+f:
document.addEventListener("keydown", function(e) {
if (e.keyCode == 70 && e.ctrlKey || e.keyCode === 114) {
document.getElementById("myInput").focus();
e.preventDefault();
}
})
<input id="myInput" />
Otherwise you prevent any keydown that use is doing...
keycode is now deprecated. Use this:
window.addEventListener("keydown", function(e) {
if ( (e.code == "0x0021" && e.key == "Control") || e.code == "0x003D") {
document.getElementById("myInput").focus();
e.preventDefault();
}
})
<input id="myInput" />
I am trying to stop the user from navigating away from the page while they are inputting some text. I am doing this by suppressing the F5 button and the backspace button, which goes back.
Now I notice that I cannot press t. When I check the logs, it's because t is giving me a key event code of 116 which is the same as the F5 button.
How can I get around this?
Here is a code snippet.
function suppressBackspaceAndF5(evt) {
evt = evt || window.event;
var target = evt.target || evt.srcElement;
// when I release "t" - the code is 116, which is the same as the refresh code.
console.log(evt.keyCode);
if ((evt.keyCode == 8 &&
!/input|textarea/i.test(target.nodeName)) ||
evt.keyCode == 116) {
return false;
}
}
document.onkeydown = suppressBackspaceAndF5;
document.onkeypress = suppressBackspaceAndF5;
<input></input>
I found this related Stackoverflow question which seems to contains your answer. Posting here specifically as a reference:
capturing f5 keypress event in javascript using window.event.keyCode in window.onbeforeunload event is always 0 and not 116
Excerpt: by SO user Sim_ba
Dont use e.keyCode == 166 use e.code == 'F5' instead.
function fkey(e){
e = e || window.event;
if( wasPressed ) return;
function fkey(e){
e = e || window.event;
if (e.code === 'F5') {
alert("f5 pressed");
wasPressed = true;
}else {
alert("Window closed");
}
}
This is because the 't' and 'F5' both use the keycode number 116. [...]
Go vote Sim_ba up as well! :)
Is there any way to make a keyboard shortcut that will click on a button on a webpage.
Like I would like to have a hotkey : Ctrl+S to automatically click on Search on Google.
I've tried this : Shortcut Manager plugin but I'm not sure how to assign it.
This might help:
See this jsFiddle
$(window).keypress(function(e) {
var keycode = (e.keyCode ? e.keyCode : e.which);
var rr = $('#report').html();
$('#report').html(rr + '<br />' + keycode);
if (e.ctrlKey) alert('Control pressed');
//if (!(keycode == 115 && e.ctrlKey) && !(keycode == 19)) return true;
if (!(keycode == 83 && e.ctrlKey) && !(keycode == 17)) return true;
alert("Ctrl-S pressed");
$('#gsearch').trigger('click'); //or just .click() also works
e.preventDefault();
return false;
});
Note that webkit browsers will not trap ctrl, alt, shift, etc keys. See this article for info
this is what I got so far
http://jsfiddle.net/qEKfg/
It's two buttons that activate on click and look like keyboard keys.
I'm trying to make it where they will only activate (animate) on a keyboard press of the related keys (CTRL and D)
This will make an 'animated buttons' effect for bookmarking my website, because CTRL+D is the hotkey to bookmark a page.
But I don't know how to set it up to work with keyboard keys in html or jQuery
if some could help I would be really REALLY grateful
The following should work for you. However, note that due to the window losing focus, I've added in a timer to release the on-screen 'buttons' after 5 seconds, as the window losing focus at that specific time prevents the keyup event from firing.
$(document).ready(function() {
var keys = [];
$(window).on('keydown keyup', function(e) {
if (e.type == "keydown") {
if (e.keyCode == 17 || e.keyCode == 91) {
$("a.a_demo_two:contains('CTRL')").addClass("active");
keys[0] = e.keyCode;
}
else if (e.keyCode == 68) {
$("a.a_demo_two:contains('D')").addClass("active");
keys[1] = 68;
};
if ((keys[0] == 17 || e.keyCode == 91) && keys[1] == 68) {
setTimeout(function() {
$('a.a_demo_two').removeClass("active");
}, 5000);
}
}
else {
if (e.keyCode == 17 || e.keyCode == 91) {
$("a.a_demo_two:contains('CTRL')").removeClass("active");
}
else if (e.keyCode == 68) {
$("a.a_demo_two:contains('D')").removeClass("active");
}
keys = [];
}
});
});
DEMO
Basically you just put handler on keydown and keyup events and trigger whatever you want.
Something like that
$('body').on('keydown', function(e) {
console.log(e)
if (e.ctrlKey) $('.a_demo_two').trigger('mousedown')
if (e.keyCode === 100) $('.a_demo_two').trigger('mousedown')
});
$('body').on('keyup', function(e) {
console.log(e)
if (e.ctrlKey) $('.a_demo_two').trigger('mouseup')
if (e.keyCode === 100) $('.a_demo_two').trigger('mouseup')
});
I use some hotkeys on my website, but when the user is inside the search form or inside comment. I want to disable them.
What the best for me to do it? Thanks
Example of my hotkey:
$(document).keydown(function(e)
{
if (e.which == 40 || e.which == 74) // next post
{
return scroll('next');
}
if (e.which == 38 || e.which == 75) // prev post
{
return scroll('prev');
}
});
You can check for the event.target element. If that element is from type INPUT you might want to omit the handler code. Could look like
$(document).keydown(function(e)
{
if( e.target.nodeName !== 'INPUT' ) {
if (e.which == 40 || e.which == 74) // next post
{
return scroll('next');
}
if (e.which == 38 || e.which == 75) // prev post
{
return scroll('prev');
}
}
});
You could check if e.target.nodeName === INPUT (the event is triggered inside an input field) and act accordingly