I found a couple of keypress scripts but I need a key binding in Chrome and Safari for command key and s (like saving files in desktop applications). Does anybody know about such a script or plugin?
jquery is in place but pure JavaScript solutions are also welcome!
thanks
Well... I wanted something like this soo I wrote my own..
function setAppleSSave(form) {
var lastKey;
$(window).bind('keydown',function(e){
if( lastKey && (lastKey == 91 && e.keyCode == 83) || (lastKey == 83 && e.keyCode == 91) ){
e.preventDefault();
$(form).submit();
return false;
}
lastKey = e.keyCode;
});
}
this works fine for me any recommendations? I'm not a good JavaScripter :)
<script src="jquery.js" type="text/javascript"></script>
<script>
var last_key = 0;
var code_meta_key = 91;
var code_s = 83;
$(document).keydown(function(event){
if (last_key == 91 && event.keyCode == 83) {
setTimeout("console.log('boom')",50);
return false;
}
last_key = (event.keyCode == 91) ? 91 : 0;
});
</script>
For those who came for a lightweight and straightforward way to get the job done, here it is :
$(window).keydown(function (e){
if (e.metaKey && e.keyCode == 83) {
yourFunction();
e.preventDefault();
return false;
}
});
Using
jQuery(document).bind('keydown', 'Ctrl+1',function (evt){
setTimeout("console.log('ctrl+1')",50);return false;
});
is fine.
To determine what keys will fire which event i recommend strongly this w3.org test page.
Related
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
I've looked on the internet for this and all I can find are depreciated functions so before posting please check to make sure that the code you suggest isn't depreciated.
I've found this and tried it:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent
$(document).ready(function () {
var x = new KeyboardEvent("FormatCode", deprectiatedArgument);
});
But after further inspection the KeyboardEventInit is depreciated.
I would like to create an event on pres of the CTRL + K keys.
You have a specific key code for every button on the keyboard.
All of them are here http://keycode.info/.
$(document).keyup(function(e) {
if (e.keyCode === 13) function(); // enter
if (e.keyCode === 27) function(); // esc
});
Here's a vanilla JS solution to detect a CTRL + k keypress event:
UPDATED to also trigger the event.
document.addEventListener("keypress", function(e) {
if ((e.ctrlKey || e.metaKey) && (e.keyCode == 11 || e.keyCode == 75)) {
alert("ctrl+k!");
}
});
document.getElementById("trigger").addEventListener("click", function(){
//trigger a keypress event...
var e = document.createEvent('HTMLEvents');
e.initEvent("keypress", false, true);
e.ctrlKey = true;
e.keyCode = 75;
document.dispatchEvent(e);
});
Press <kbd>ctrl+k</kbd> or
trigger the event
you can use a library called shortcut.js .. here is a link to their source code for downloading:
http://www.openjs.com/scripts/events/keyboard_shortcuts/shortcut.js
then run ur code by making this function:
shortcut.add("Ctrl+K",function() {
alert("Hi there!");
});
and here is the documentation : http://www.openjs.com/scripts/events/keyboard_shortcuts/
hope that can help.
$(document).ready(function () {
var bool = false;
$(document).keydown(function (e) {
if (e.keyCode === 17) {
bool = true;
}
if (bool == true && e.keyCode == 75) {
alert("");
}
});
$(document).keyup(function (e) {
if (e.keyCode === 17) {
bool = false;
}
});
});
This is how me and a friend got it working
I am using the event keypress in backbone. The keyCode for Enter(13) works fine but the keyCode for tab(9) is not working for some odd reason. Please help me figure this out. Thanks.
onEnterSetTitle: function(ev) {
if (ev.keyCode === 9) {
this.$el.find('.set-title-input input').trigger('blur');
}
},
I fixed it by replace keypress with keydown.
if i have to do this then i would use || operator this way:
onEnterSetTitle: function(ev) {
var kc = ev.which || ev.keyCode;
if (kc === 9) {
.........
}
}
I have a script that is supposed to open a section of a web page, and save changes on Ctrl + n and Ctrl + s respectively. I got it working in IE, but it doesn't seem to work in Firefox and Chrome. Any ideas?
My override function.
function prevent(e)
{
try{e.stopPropagation();}catch(ex){}
try{e.preventDefault()}catch(ex){}
try{if (e.preventDefault)
e.preventDefault();
else {
e.cancelBubble = true;
e.returnValue = false;
e.keyCode = 0;
}} catch(ex){}
}
I have seen the same issue. Some browsers will not allow you to capture certain shortcuts. Look at this https://stackoverflow.com/a/7296303/1366887
Some key combinations are resticted in Chrome 4, but not in Chrome 3. Look here: https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-bugs/Ntc1byZXHfU
Here is the Javascript:
$(window).keydown(function(event) {
if(event.ctrlKey && event.keyCode == 84) {
console.log("Hey! Ctrl+T event captured!");
event.preventDefault();
}
if(event.ctrlKey && event.keyCode == 83) {
console.log("Hey! Ctrl+S event captured!");
event.preventDefault();
}
});
I have used this numerous times, and it has worked greatly.
Here is another rescource you can take a look at: http://unixpapa.com/js/key.html
Without Jquery:
onkeydown = function(e){
if(e.ctrlKey && e.keyCode == 'S'.charCodeAt(0)){
e.preventDefault();
//your saving code
}
}
Here is a JSFIDDLE of it working.
For anyone looking for this in the future, the answer for current browsers is the following:
if (event.ctrlKey && event.key === 'k') event.preventDefault()
$(".tbSearchBox").keyup(function (event) {
if (event.keyCode == 13) {
alert("ye");
searchSet = $(this).val();
$(".btnSerachButton").click();
}
});
Im using the above code to detect whether the user has typed something in a search box then hit enter instead of pressing the search button. this works for all browsers apart from IE. IE can read the on keyup event but bypasses the if statement used. Any idea why?
There some incompatibility in ie regarding event and keycode so to make it browser compatible try this
$(".tbSearchBox").keypress(function (event) {
var ev = event || window.event;
var key = ev.keyCode || ev.which || ev.charCode;
if (key == 13) {
ev.preventDefault();
alert("ye");
searchSet = $(this).val();
$(".btnSerachButton").click();
}
});
var code = (event.keyCode ? event.keyCode : event.which);
or maybe even
var code = event.keyCode || event.which;
You should use event.which here to normalise event.keyCode and event.charCode:
if (event.which == 13) ...