Hotkey combinations don't work if done fast - javascript

I'm trying to make a hotkey for a web app, for example Ctrl + z performs the undo function.
It seems that when I press the keys fast (as I'm used to from using desktop apps a lot), it doesn't register. The single key press registers, but it misses the combination for some reason.
From what I understand, you have to keep track of which buttons are held down via keypress events, which is what I've done below.
Try the code below. Hitting Z outputs Z. Hitting CTRL then Z slowly outputs CTRL + Z. Hitting CTRL then Z quickly outputs Z. When I perform the action at the same speed in say Notepad for windows, it works flawlessly almost every time.
https://codepen.io/samkeddy/pen/YQjgdZ?editors=1010#0
var ctrlPressed=false, altPressed=false;
window.addEventListener("keydown", function (e) {
hotkey = e;
if (e.keyCode == 17) ctrlPressed = true;
if (e.keyCode == 18) altPressed = true;
e.preventDefault();
});
window.addEventListener("keyup", function (e) {
hotkey = window.event;
if (e.keyCode == 17) ctrlPressed = false;
if (e.keyCode == 18) altPressed = false;
if (e.keyCode == 90){
if (altPressed && ctrlPressed && e.keyCode == 90)
addText('ALT + CTRL + Z');
else if (ctrlPressed && e.keyCode == 90)
addText('CTRL + Z');
else
addText('Z');
}
});
//meaningless, just adds text to doc so you can see it
function addText(text) {
var theDiv = document.getElementById("output");
theDiv.appendChild(document.createElement("br"));
var content = document.createTextNode(text);
theDiv.appendChild(content);
}

Use e.ctrlKey to check if ctrl is down instead of variables, and just check for the hotkey combination on keydown, don't do anything on key up.
https://codepen.io/samkeddy/pen/gRdBpq
function KeyPress(e) {
var evtobj = window.event? event : e
if (evtobj.keyCode == 90 && evtobj.ctrlKey) addText("CTRL + Z");
else if (evtobj.keyCode == 90) addText("Z");
}
document.onkeydown = KeyPress;

Related

Tabs default action is not stopped event though i use e.preventDefault()

I am showing a text area in a modal when I pressed tab it moves to the next input I wrote code to stop this but I didn't work for me.(when I pressed tab the execution even not coming to the acceptTabsSpace function)
$(document).on("keyup", "#collection-text-input", acceptTabsSpace);
function acceptTabsSpace(e){
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
e.preventDefault();
return false;
}
}
Add the listener to trigger on keydown instead:
$(document).on("keydown", "#collection-text-input", acceptTabsSpace);
function acceptTabsSpace(e){
var keyCode = e.keyCode || e.which;
if (keyCode === 9) {
e.preventDefault();
return false;
}
}
Use keydown instead key up and for tabspaces press tab twice.
$(document).on("keydown", "#collection-text-input", acceptTabsSpace);
function acceptTabsSpace(e) {
var keyCode = e.keyCode || e.which;
if (e.keyCode === 9) {
e.preventDefault();
this.value = this.value.substring(0, this.selectionStart) + "\t" +
this.value.substring(this.selectionEnd);
this.selectionEnd = s + 1;
}
}

Disable Chrome's text input undo/redo (CTRL+Z / CTRL+Y)

i'm currently developing a web application and i encounter a problem.
As you might know or not, chrome has a feature that gives <input> (especially text inputs) an "undo" function when you hit CTRL+Z and "redo" with CTRL+Y it may seem like a good feature in normal websites, but currently i'm making an image editor that also uses those keyboard shortcuts (CTRL+Z & CTRL+Y).
in my app i also have a text input, so when i change the text input's content and then hit CTRL+Z to undo the changes in my image editor, it would undo the change in the text editor instead!
here is a Codepen that would demonstrate the effect(instruction is in the Codepen)
So in conclusion i want to remove the undo/redo function in chrome how can i do that?
var ctrlDown = false;
var ctrlKey = 17, vKey = 86, cKey = 67, zKey = 90;
document.body.onkeydown = function(e) {
if (e.keyCode == 17 || e.keyCode == 91) {
ctrlDown = true;
}
if ((ctrlDown && e.keyCode == zKey) || (ctrlDown && e.keyCode == vKey) || (ctrlDown && e.keyCode == cKey)) {
e.preventDefault();
return false;
}
}
document.body.onkeyup = function(e) {
if (e.keyCode == 17 || e.keyCode == 91) {
ctrlDown = false;
};
};
<body>
<input type='text' value='test' />
</body>
Does this work?
Stolen from here, found on here as a comment by #KadeHafen.
You could use onkeydown event, and preventDefault.. eg.
document.onkeydown = function(e) {
if (e.ctrlKey && e.key === 'z') {
e.preventDefault();
alert("CTRL + Z!");
}
}

Adding keypress Events that use an existing function

I have used a function which is activated on click events. I want to do the same using keypress events.
function addToText(target) {
var exp = target.target;
//alert(exp.value);
if (newExp) {
//clearText();
document.getElementById("expression").value = exp.value;
newExp=false;
}
else
document.getElementById("expression").value = document.getElementById("expression").value + exp.value;
}
This is the function used. How do I modify it to use for keypress events also. Currently, it does not work initially(for keypress events). But after clicking once, then any keypress returns the same number that was previously clicked.
Full code here:http://codepen.io/jpninanjohn/pen/JXVpYb?editors=1010
Here's is your final solution, I test if charcode is between 48 and 57, what it means, numbers between 0 and 9.
document.addEventListener('keypress', function(e){
if (e.which >= 48 && e.which <= 57)
document.getElementById("expression").value+= String.fromCharCode(e.which);
});
You can use this function-
window.onkeyup = function(e) {
var key = e.keyCode ? e.keyCode : e.which;
document.getElementById("expression").value += key-48; //for getting the number
alert(key);
if (key == 38) {
//whatever
}else if (key == 40) {
// whatever
}
}
source
And you need to add + to the =
document.getElementById("expression").value+=
instead of -
document.getElementById("expression").value=
You have to add a different function for keyPress event, because the keypress event get the value of pressed key differently, not target.target
function addToTextKeypress(event) {
var exp = String.fromCharCode(event.keyCode);
//alert(exp);
if (newExp) {
//clearText();
document.getElementById("expression").value = exp;
newExp=false;
}
else
document.getElementById("expression").value = document.getElementById("expression").value + exp;
}
Later you need to remove exp.value since it has the direct value

How can i disable the ctrl + a using javascript?

<script type="text/javascript">
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}
document.onkeydown=function(e)
{
if(e.which == 17)
isCtrl=true;
if((e.which == 85) || (e.which == 67) && isCtrl == true)
{
// alert(‘Keyboard shortcuts are cool!’);
return false;
}
}
</script>
Hi all , I using the code to disable the right click and also the ctrl+c and ctrl+u how to disable the ctrl a in the following code. Any help would be great.
Thanks,
vicky
You shouldn't try to do this, let me tell you why. I'm assuming you want to disable ctrl + c because you don't want the user to be able to copy content from your site, well have you thought about the fact that there are a dozen of other ways to copy your content?
Download html file and copy in their favorite text editor
Inspect element and copy content from there
Use mouse to right click -> copy
And for my good friend #glenatron:
Network sniffer like Fiddler between the browser and the network card
Screenshots, Taking a photograph of the monitor
... The list goes on and on.
Also, trying to stop users from normal functionality will only bother and annoy them; most likely causing them to leave your site and never return.
FInd the below code for detect ctrl + a,ctrl + A,ctrl + c,ctrl + C, ctrl + u,ctrl + U with your code editing.
<script type="text/javascript">
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}
document.onkeydown=function(e)
{
if(e.which == 17)
isCtrl=true;
if(((e.which == 85) || (e.which == 117) || (e.which == 65) || (e.which == 97) || (e.which == 67) || (e.which == 99)) && isCtrl == true)
{
// alert(‘Keyboard shortcuts are cool!’);
return false;
}
}
you can get value for key from below link
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html
Enjoy...!! :)
How can i disable the ctrl + a
I had the same question but for a different reason,
I had multiple textPath elements in my DOM and because of some weird bug, whenever I pressed ctrl + a they all change position, to fix that I added:
body{
...
user-select:none
}
I guess this also "technically" disables Ctrl + a

How to Trigger an event with keys(ctrl +)(jQuery)

How can I trigger an event with jQuery if I press the Ctrl key plus the ++ key(zoom in).
key = Ctrl ++
Try this
$(window).keypress(function(e){
if((e.which == 61 && e.ctrlKey) || (e.which == 43 && e.ctrlKey)){
//Ctrl + "+" is pressed, 61 is for =/+ anr 43 is for Numpad + key
}
});
An example of binding to Ctrl+I. Note that you can't override default browser behavior, so many of the Ctrl+(letter) shortcuts are reserved (Ctrl+T = new tab, Ctrl+N = new Window, Ctrl+P = Print etc...)
$(window).keydown(function(e){
if(e.which == 17)
$(window).bind('keydown.ctrlI', function(e){
if(e.which == 73){
e.preventDefault();
alert('CTRL+I');
}
});
});
$(window).keyup(function(e){
if(e.which == 17)
$(window).unbind('keydown.ctrlI');
});
// the element at which you are firing the event
var div = $('#foo');
// the event handler
div.bind('paint', function() {
$(this).addClass('painted');
});
$(window).keydown(function(e) {
// if CTRL + + was pressed
if ( e.ctrlKey && e.which === 187 ) {
// trigger the event
div.trigger('paint');
}
});
Live demo: http://jsfiddle.net/NMYJW/

Categories