Keyboard shortcut not working - javascript

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.

Related

e is undefined, correct syntax for a recursive handler function

How to make a correct recursive call? In this code the console says me "e is undefined".
It's a event listener for a button ul in a unordered list.
This is my code:
$("button#ul").on("click", function(event){
var button = $(this);
$(button).toggleClass("active");
if ($(button).hasClass('active')){
//event listener
$(document).on("keydown", **checkExit** = function(e) {
if (exit(e) == true){
$(button).removeClass('active');
$(this).off("keydown");
}
**checkExit();**
});
function exit(event){
// alert("prevKeyCode: "+prevKeyCode);
var code = event.keyCode || event.which;
var doubleEnter = (code == 13 && prevKeyCode == code);
if (event.type == "click"){
return true;
}
else if (event.type == 'keydown'){
if (code == 27 || doubleEnter) {
prevKeyCode = null;
return true;
}
else if (code == 13) {
prevKeyCode = 13;
}
else {
prevKeyCode = code;
}
return false;
}
}
I think I should define outside the function checkExit...
You need to pass the event as a parameter when calling checkExit again:
$(document).on("keydown", checkExit = function(e) {
if (exit(e) == true) {
$('button').removeClass('active');
$(this).off("keydown"); //spegne il gestore corrente
}
checkExit(e);
});

Is there a way in JavaScript to change Enter to Shift + Enter

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/>

Disable Ctrl key in IE 8

I want to disable the ctrl key in the IE browser.I had tried some solution using javascript but nothing is working can someone please help me to find out the solution
document.onkeydown = function () {
if (event.keyCode == 17) alert('Ctrl Key is disabled');
};
document.onkeydown = function(e) {
if (e.altKey && (e.keyCode === 36)) {//Alt+home blocked.
return false;
}
if (e.altKey && (e.keyCode === 70)) {//Alt+f blocked.
return false;
}
};
function hookKeyboardEvents(e) {
// get key code
var key_code = (window.event) ? event.keyCode : e.which;
// case :if it is IE event
if (window.event)
{
if (!event.shiftKey && !event.ctrlKey) {
window.event.returnValue = null;
event.keyCode = 0;
}
}
// case: if it is firefox event
else
e.preventDefault();
}
window.document.onkeydown = hookKeyboardEvents;
function Disable_Control_C() {
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
if (event.ctrlKey && (keystroke == 'c' || keystroke == 'v' || keystroke == 'p' || keystroke == 's' || keystroke == 'u')) {
alert("this function is disabled");
event.returnValue = false; // disable Ctrl+C
}
}
<body onkeydown="javascript:Disable_Control_C()">
this is what i do it to run in the IE...

How to prevent Chrome open history after press CTRL+H

I mean is there any way to prevent the default accesskey in Chrome.
var text = document.getElementById("text");
text.onkeyup = function(e) {
if(e.ctrlKey && e.keyCode == 72) {
// do something...
alert("You wont see me cause Chrome will open history manager");
}
}
<textarea id="text"></textarea>
This should work. You need Keydown Event.
var text = document.getElementById("text");
text.addEventListener("keydown", function(e) {
console.log(e.keyCode);
if (e.keyCode == 72 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
alert('Stopped');
}
}, false);
<textarea id="text"></textarea>
I've added isCtrlDown variable and keyup with keydown event, to achieve what you're looking for because I didn't see isKeyDown kind of function in Key as discussed here.
var isCtrlDown = false;
var text = document.getElementById("text");
text.onkeydown = function(e){
if(e.keyCode == 17){
isCtrlDown = true;
}
if(isCtrlDown && e.keyCode == 72){
// do something...
console.log("You wont see me cause Chrome will open history manager");
}
e.preventDefault();
}
text.onkeyup = function(e){
if(e.keyCode == 17){
isCtrlDown = false;
}
e.preventDefault();
}

Number in textfield should be autoformatted only after user clicks outside the textbox?

I have a problem. Basically, what happens in my case is that the numbers in my textbox are autoformatted as I type. I don't want this to happen. What I want is that the numbers should be autoformatted only when the user clicks outside the textbox.
In my input tag I have :
onkeyup="format(event, this);"
My javascript function is :
function format(e, obj) {
if (e.keyCode == 36) {
press1(obj);
}
if (e.keyCode == 13) {
return false;
}
if ((e.keyCode <= 34) || (e.keyCode >= 46 && e.keyCode < 58) || (e.keyCode >= 96 && e.keyCode <= 105)) { // //alert(e.keyCode);
obj.value = CommaFormatted(obj.value);
} else {
if (e && e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
} else {
e.cancelBubble = true;
e.returnValue = false;
}
return false;
}
}
where the press1 function is:
function press1(textControlID) {
var text = textControlID;
if (text.getAttribute("maxlength") == text.value.length) {
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
return true;
}
if (text != null && text.value.length > 0) {
if (text.createTextRange) {
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
} else if (text.setSelectionRange) {
var textLength = text.value.length;
text.setSelectionRange(textLength, textLength);
}
}
}
I really hope this could be solved. Please!
You could change onkeyup to onblur, which is the event that gets fired when the control loses focus - clicking out of it.
The onkeyup event fires with every keypress.

Categories