question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent onkeydown event with condition.

See original GitHub issue

Is 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:open
  • Created 4 months ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Flachdachscommented, May 9, 2023

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.

0reactions
Planit-Scheduler-Solutionscommented, Jul 13, 2023

Thanks, works great for me. (I apply as in the example).

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found