Can I prevent keyup event after a keydown in javascript?











up vote
-4
down vote

favorite












Are keydown-keyup js events linked? Can I control an event during keydown to prevent the next keyup event of the same key?



$(document).keydown(function (e) {
if(some_condition)
e.preventDefault(); //I don't want keyup after this
}).keyup(function (e) {
if(keydown_was_prevent_default) {
alert("Exit without keyup!");
return false;
}
});









share|improve this question




















  • 2




    Just remove the keyup event?
    – cmprogram
    Nov 19 at 17:21










  • I edited my question to better explain, I have to control the same gesture/key, when I push down a button, in some circumstances, I want to prevent the next keyup (of the same key!). This is why I want to know if the two events keyup/down are linked.
    – Tobia
    Nov 20 at 6:16















up vote
-4
down vote

favorite












Are keydown-keyup js events linked? Can I control an event during keydown to prevent the next keyup event of the same key?



$(document).keydown(function (e) {
if(some_condition)
e.preventDefault(); //I don't want keyup after this
}).keyup(function (e) {
if(keydown_was_prevent_default) {
alert("Exit without keyup!");
return false;
}
});









share|improve this question




















  • 2




    Just remove the keyup event?
    – cmprogram
    Nov 19 at 17:21










  • I edited my question to better explain, I have to control the same gesture/key, when I push down a button, in some circumstances, I want to prevent the next keyup (of the same key!). This is why I want to know if the two events keyup/down are linked.
    – Tobia
    Nov 20 at 6:16













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











Are keydown-keyup js events linked? Can I control an event during keydown to prevent the next keyup event of the same key?



$(document).keydown(function (e) {
if(some_condition)
e.preventDefault(); //I don't want keyup after this
}).keyup(function (e) {
if(keydown_was_prevent_default) {
alert("Exit without keyup!");
return false;
}
});









share|improve this question















Are keydown-keyup js events linked? Can I control an event during keydown to prevent the next keyup event of the same key?



$(document).keydown(function (e) {
if(some_condition)
e.preventDefault(); //I don't want keyup after this
}).keyup(function (e) {
if(keydown_was_prevent_default) {
alert("Exit without keyup!");
return false;
}
});






javascript jquery keyevent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 6:13

























asked Nov 19 at 17:19









Tobia

3,0381964135




3,0381964135








  • 2




    Just remove the keyup event?
    – cmprogram
    Nov 19 at 17:21










  • I edited my question to better explain, I have to control the same gesture/key, when I push down a button, in some circumstances, I want to prevent the next keyup (of the same key!). This is why I want to know if the two events keyup/down are linked.
    – Tobia
    Nov 20 at 6:16














  • 2




    Just remove the keyup event?
    – cmprogram
    Nov 19 at 17:21










  • I edited my question to better explain, I have to control the same gesture/key, when I push down a button, in some circumstances, I want to prevent the next keyup (of the same key!). This is why I want to know if the two events keyup/down are linked.
    – Tobia
    Nov 20 at 6:16








2




2




Just remove the keyup event?
– cmprogram
Nov 19 at 17:21




Just remove the keyup event?
– cmprogram
Nov 19 at 17:21












I edited my question to better explain, I have to control the same gesture/key, when I push down a button, in some circumstances, I want to prevent the next keyup (of the same key!). This is why I want to know if the two events keyup/down are linked.
– Tobia
Nov 20 at 6:16




I edited my question to better explain, I have to control the same gesture/key, when I push down a button, in some circumstances, I want to prevent the next keyup (of the same key!). This is why I want to know if the two events keyup/down are linked.
– Tobia
Nov 20 at 6:16












1 Answer
1






active

oldest

votes

















up vote
1
down vote













They aren't connected.



You could pass state between them using a variable.



(function () { // IIFE to avoid making `block_keys` a global

let block_keys = false;

$(document).on("keydown", event => {
if (some_condition()) {
block_keys = true;
} else {
block_keys = false;
}
});

$(document).on("keyup", event => {
if (block_keys) {
event.preventDefault();
}
});

}());


If you were to do this on an element I would probably use data() to store the state so it would be associated with the element directly.






share|improve this answer





















  • I want to block the same keyup, not every other keyup.
    – Tobia
    Nov 20 at 6:17










  • @Tobia — And that is what this does. If you set block_keys when the key goes down, it is going to be in that state for when the key is released … not when it is released, pressed down again, then released again.
    – Quentin
    Nov 20 at 8:17










  • What happens if i fired these events with this order: keydown(Akey), keydown(Bkey), keyUp(Bkey), keyUp(Akey)? If Akey has "some_condition" it locks also the B-keyup event.
    – Tobia
    Nov 20 at 8:41












  • I explain here with this fiddle why this solution doesn't work: jsfiddle.net/a0ux7dqf (the condition used is just an example)
    – Tobia
    Nov 20 at 8:50












  • Just namespace the variable: jsfiddle.net/fb3j0s46
    – geoidesic
    Nov 22 at 16:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379701%2fcan-i-prevent-keyup-event-after-a-keydown-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













They aren't connected.



You could pass state between them using a variable.



(function () { // IIFE to avoid making `block_keys` a global

let block_keys = false;

$(document).on("keydown", event => {
if (some_condition()) {
block_keys = true;
} else {
block_keys = false;
}
});

$(document).on("keyup", event => {
if (block_keys) {
event.preventDefault();
}
});

}());


If you were to do this on an element I would probably use data() to store the state so it would be associated with the element directly.






share|improve this answer





















  • I want to block the same keyup, not every other keyup.
    – Tobia
    Nov 20 at 6:17










  • @Tobia — And that is what this does. If you set block_keys when the key goes down, it is going to be in that state for when the key is released … not when it is released, pressed down again, then released again.
    – Quentin
    Nov 20 at 8:17










  • What happens if i fired these events with this order: keydown(Akey), keydown(Bkey), keyUp(Bkey), keyUp(Akey)? If Akey has "some_condition" it locks also the B-keyup event.
    – Tobia
    Nov 20 at 8:41












  • I explain here with this fiddle why this solution doesn't work: jsfiddle.net/a0ux7dqf (the condition used is just an example)
    – Tobia
    Nov 20 at 8:50












  • Just namespace the variable: jsfiddle.net/fb3j0s46
    – geoidesic
    Nov 22 at 16:49















up vote
1
down vote













They aren't connected.



You could pass state between them using a variable.



(function () { // IIFE to avoid making `block_keys` a global

let block_keys = false;

$(document).on("keydown", event => {
if (some_condition()) {
block_keys = true;
} else {
block_keys = false;
}
});

$(document).on("keyup", event => {
if (block_keys) {
event.preventDefault();
}
});

}());


If you were to do this on an element I would probably use data() to store the state so it would be associated with the element directly.






share|improve this answer





















  • I want to block the same keyup, not every other keyup.
    – Tobia
    Nov 20 at 6:17










  • @Tobia — And that is what this does. If you set block_keys when the key goes down, it is going to be in that state for when the key is released … not when it is released, pressed down again, then released again.
    – Quentin
    Nov 20 at 8:17










  • What happens if i fired these events with this order: keydown(Akey), keydown(Bkey), keyUp(Bkey), keyUp(Akey)? If Akey has "some_condition" it locks also the B-keyup event.
    – Tobia
    Nov 20 at 8:41












  • I explain here with this fiddle why this solution doesn't work: jsfiddle.net/a0ux7dqf (the condition used is just an example)
    – Tobia
    Nov 20 at 8:50












  • Just namespace the variable: jsfiddle.net/fb3j0s46
    – geoidesic
    Nov 22 at 16:49













up vote
1
down vote










up vote
1
down vote









They aren't connected.



You could pass state between them using a variable.



(function () { // IIFE to avoid making `block_keys` a global

let block_keys = false;

$(document).on("keydown", event => {
if (some_condition()) {
block_keys = true;
} else {
block_keys = false;
}
});

$(document).on("keyup", event => {
if (block_keys) {
event.preventDefault();
}
});

}());


If you were to do this on an element I would probably use data() to store the state so it would be associated with the element directly.






share|improve this answer












They aren't connected.



You could pass state between them using a variable.



(function () { // IIFE to avoid making `block_keys` a global

let block_keys = false;

$(document).on("keydown", event => {
if (some_condition()) {
block_keys = true;
} else {
block_keys = false;
}
});

$(document).on("keyup", event => {
if (block_keys) {
event.preventDefault();
}
});

}());


If you were to do this on an element I would probably use data() to store the state so it would be associated with the element directly.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 17:25









Quentin

636k718591027




636k718591027












  • I want to block the same keyup, not every other keyup.
    – Tobia
    Nov 20 at 6:17










  • @Tobia — And that is what this does. If you set block_keys when the key goes down, it is going to be in that state for when the key is released … not when it is released, pressed down again, then released again.
    – Quentin
    Nov 20 at 8:17










  • What happens if i fired these events with this order: keydown(Akey), keydown(Bkey), keyUp(Bkey), keyUp(Akey)? If Akey has "some_condition" it locks also the B-keyup event.
    – Tobia
    Nov 20 at 8:41












  • I explain here with this fiddle why this solution doesn't work: jsfiddle.net/a0ux7dqf (the condition used is just an example)
    – Tobia
    Nov 20 at 8:50












  • Just namespace the variable: jsfiddle.net/fb3j0s46
    – geoidesic
    Nov 22 at 16:49


















  • I want to block the same keyup, not every other keyup.
    – Tobia
    Nov 20 at 6:17










  • @Tobia — And that is what this does. If you set block_keys when the key goes down, it is going to be in that state for when the key is released … not when it is released, pressed down again, then released again.
    – Quentin
    Nov 20 at 8:17










  • What happens if i fired these events with this order: keydown(Akey), keydown(Bkey), keyUp(Bkey), keyUp(Akey)? If Akey has "some_condition" it locks also the B-keyup event.
    – Tobia
    Nov 20 at 8:41












  • I explain here with this fiddle why this solution doesn't work: jsfiddle.net/a0ux7dqf (the condition used is just an example)
    – Tobia
    Nov 20 at 8:50












  • Just namespace the variable: jsfiddle.net/fb3j0s46
    – geoidesic
    Nov 22 at 16:49
















I want to block the same keyup, not every other keyup.
– Tobia
Nov 20 at 6:17




I want to block the same keyup, not every other keyup.
– Tobia
Nov 20 at 6:17












@Tobia — And that is what this does. If you set block_keys when the key goes down, it is going to be in that state for when the key is released … not when it is released, pressed down again, then released again.
– Quentin
Nov 20 at 8:17




@Tobia — And that is what this does. If you set block_keys when the key goes down, it is going to be in that state for when the key is released … not when it is released, pressed down again, then released again.
– Quentin
Nov 20 at 8:17












What happens if i fired these events with this order: keydown(Akey), keydown(Bkey), keyUp(Bkey), keyUp(Akey)? If Akey has "some_condition" it locks also the B-keyup event.
– Tobia
Nov 20 at 8:41






What happens if i fired these events with this order: keydown(Akey), keydown(Bkey), keyUp(Bkey), keyUp(Akey)? If Akey has "some_condition" it locks also the B-keyup event.
– Tobia
Nov 20 at 8:41














I explain here with this fiddle why this solution doesn't work: jsfiddle.net/a0ux7dqf (the condition used is just an example)
– Tobia
Nov 20 at 8:50






I explain here with this fiddle why this solution doesn't work: jsfiddle.net/a0ux7dqf (the condition used is just an example)
– Tobia
Nov 20 at 8:50














Just namespace the variable: jsfiddle.net/fb3j0s46
– geoidesic
Nov 22 at 16:49




Just namespace the variable: jsfiddle.net/fb3j0s46
– geoidesic
Nov 22 at 16:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379701%2fcan-i-prevent-keyup-event-after-a-keydown-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft