I want to do the following in javascript :
If "B" is pressed > Press "C" automatically
How to achieve this condition ? e.keyCode == 66 is B
$(document).ready(function() {
$(document).bind("keydown", function(e) {
if (e.keyCode == 66) {
}
});
});
$(document).ready(function() {
$(document).on("keypress", e => {
if (e.which == 66) {
e.preventDefault();
var f = $.Event("keypress", {which: 13});
$(document).trigger(f);
}
});
});
Related
This is what I tried and obviously failed:
ed.on('keyup', function(e){
console.log(e.keyCode)
if(e.keyCode == 13 && !e.shiftKey){
e.shiftKey = true;
e.keyCode = 13;
$(this).trigger(e);
}
else if (e.keyCode == 13 && e.shiftKey){
e.shiftKey = false;
e.keyCode = 13;
$(this).trigger(e);
}
});
Is there a way to do this cause based on what I seen I think I'm on the right track and most likely just not triggering it early or something similar.
Additionally, I tried using 'keypress' instead and had no luck with that.
document.onkeydown = function onkeydown(e) {
if (e.keyCode == 13 && e.shiftKey==false) {
e.preventDefault();
document.execCommand("insertLineBreak");
}
}
When enter is pressed without shift, trigger keydown with enter and shift!
$('input').on('keydown', function(event) {
if (event.keyCode == 13 && !event.shiftKey) {
$(this).trigger(jQuery.Event("keydown", {
keyCode: 13, // ENTER
shiftKey: true
}));
} else if (event.keyCode == 13 && event.shiftKey) {
console.log('shift + enter');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input/>
I'm using the code below :
$(document).keypress(function (e) {
if (e.which === 68 || e.which === 100) {
doStuff();
}
});
What i want is to capture the key 'D' as a shortcut to a function in my page. Problem is , this page has inputs and textarea controls and when i'm'typing on this controls, the 'doStuff()' function is called.
How to avoid it ? I want the key 'D' capture only if the user is not typing anything in editable controls.
Thanks !
Alternatively to e.target, you could test if the current active element is an input or textarea:
$(document).keypress(function (e) {
var focussedTag = document.activeElement && document.activeElement.nodeName;
if( focussedTag === 'INPUT' || focussedTag === 'TEXTAREA' ) {
return;
}
if (e.which === 68 || e.which === 100) {
doStuff();
}
} );
Fiddle: https://jsfiddle.net/e1qtapo6/
$(document).on('keypress', ':not(:input)', function (e) {
if (e.which === 68 || e.which === 100) {
doStuff();
}
});
Add another check before executing the 'doStuff' function. Check if the user has any input element in focus, and if true then only execute the statement.
Something like this:
$(document).keypress(function (e) {
if ( (e.which === 68 || e.which === 100) && (!$(input).is(":focus")) ) {
doStuff();
}
});
Another one -
$(document).keypress(function (e) {
if (e.which === 68 || e.which === 100) {
var _node = e.target.nodeName;
if( _node !== "INPUT" && _node !== "TEXTAREA" ){
doStuff();
}
}
});
I have a textarea validation code that only submits when the textarea is not empty. The below code works perfectly but how can I prevent the content in the textarea from submitting multiple times pressing return on the keyboard multiple times in succession?
document.getElementById('input').addEventListener('keyup', function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13 && $.trim($(this).val())) {
e.preventDefault();
send();
}
});
window.formWasSubmitted = false;
document.getElementById('input').addEventListener('keyup', function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13 && $.trim($(this).val()) && !window.formWasSubmitted) {
e.preventDefault();
window.formWasSubmitted = true;
send();
}
});
I want to write next condition:
document.onkeydown = function(e) {
e = e || event;
e.preventDefault();
if(e.ctrlKey&&e.keyCode==120) {
alert('Hello');
} else {
alert('Bye');
}
But when we begin pressing Ctrl key, we see message 'Bye'. How to disable Ctrl key if it pressed alone ?
Just make it an else-if where you check if the ctrl-key is pressed. Bye will now be displayed if any other key is pressed.
document.onkeydown = function(e) {
e = e || event;
e.preventDefault();
if(e.ctrlKey && e.keyCode==120) {
alert('Hello');
} else if( !e.ctrlKey ) {
alert('Bye');
}
}
I am using HandsOnTable jquery plugin, and I'm trying to handle a specific key combination (Alt+v) shortcut. But it is not working for some reason, here is my code and jsfiddle:
$(document).ready(function () {
var isCtrl = false;
var isShift = false;
var isAlt = false;
// action on key up
$(document).keyup(function (e) {
if (e.which == 17) {
isCtrl = false;
}
if (e.which == 16) {
isShift = false;
}
if (e.which == 18) {
isAlt = false;
}
});
// action on key down
$(document).keydown(function (e) {
if (e.which == 17) {
isCtrl = true;
}
if (e.which == 16) {
isShift = true;
}
if (e.which == 18) {
isAlt = true;
}
if (e.which == 86 && isAlt) //alt+v
{
console.log("alt+v detected");
e.preventDefault();
e.stopImmediatePropagation();
return false;
}
});
});
I'm using chromium and it looks like all the keydown events do not fire. I found out there is a beforeKeyDown callback, which can be used to "modify keybindings". Using that seems to work:
$('#example').handsontable({
data: data,
minSpareRows: 1,
colHeaders: true,
contextMenu: true,
beforeKeyDown: function (e) {
if (e.altKey === true && e.which === 86) {
console.log("alt-v");
e.stopImmediatePropagation();
e.preventDefault();
}
}
});
http://jsfiddle.net/JdzR3/
Since you're using jQuery, you can use the altkey property on the event.
$(document).on('keydown', function(e){
var key = e.which || e.keyCode;
if(e.altKey === true && key === 86){
console.log("Alt+v");
}
});
See https://developer.mozilla.org/en-US/docs/Web/API/event.altKey for more about that and other key events.