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;
}
});
javascript jquery keyevent
add a comment |
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;
}
});
javascript jquery keyevent
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
add a comment |
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;
}
});
javascript jquery keyevent
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
javascript jquery keyevent
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
add a comment |
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
add a comment |
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.
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 setblock_keyswhen 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
add a comment |
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.
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 setblock_keyswhen 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
add a comment |
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.
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 setblock_keyswhen 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
add a comment |
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.
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.
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 setblock_keyswhen 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
add a comment |
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 setblock_keyswhen 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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