Prevent onkeydown event with condition.
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
HI I need be possible to block the browser CTRL+F default event, and run some my custom logic instead. My Code:
<div @onkeydown="(args) => KeyHandle(args)"
@onkeydown:preventDefault="PreventKeyDown">
Some elements...
</div>
Razor File:
CS File:
public bool PreventKeyDown { get; set; } = false;
private void KeyHandle(KeyboardEventArgs keyboardEvent)
{
PreventKeyDown = false;
if (!keyboardEvent.Repeat)
{
if (keyboardEvent.CtrlKey && keyboardEvent.Code == "KeyF")
{
PreventKeyDown = true;
Some logic...
}
}
}
This block is not working but if I not use the prevent with parameter like this:
@onkeydown="(args) => KeyHandle(args)"
@onkeydown:preventDefault="true">
Some elements...
</div>```
It works and block the event but in this way blocks all event and I want to block only the CTRL+F.
How can I solve that issue(prevent only CTRL+F event)?
### Expected Behavior
If you press CTLS+F search should not become visible.
### Steps To Reproduce
Enter the code as in the description and press CTRL+F.
### Exceptions (if any)
_No response_
### .NET Version
6.0.401
### Anything else?
_No response_
Issue Analytics
- State:
- Created 4 months ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Disabling keypress event listener when a condition is met
1 Answer 1 ... Use document.removeEventListener("keydown", moveCharacter); since you defined moveCharacter and not navigationController.
Read more >How to prevent .keydown from firing endlessly when key is ...
I would create a flag or boolean variable called isKeyDown and set it to true when the key is down and false when...
Read more >onkeydown Event
Description. The onkeydown event occurs when the user presses a key on the keyboard. ... The onkeypress event is deprecated. It is not...
Read more >How do I conditionally PreventDefault inside the input ...
You can use the “preventDefault” attribute to prevent certain actions(events). In the following example, input keypress event checks whether the key value ...
Read more >Keyboard: keydown and keyup
The keydown events happens when a key is pressed down, and then keyup ... Preventing the default action on keydown can cancel most...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
My goal is not to just block, but to react on certain keys and prevent the default behavior. In particular changing to the previous or next tab of a tab panel using the cursor keys, but prevent that the browser scrolls on cursor up and down keys.
Thanks, works great for me. (I apply as in the example).