Thought
Thought

Reputation: 5846

Keydown Event fires twice

On a Windows store App, I have this simple TextBox

<TextBox Name="TextBoxUser" HorizontalAlignment="Right" Width="147" Margin="20,0,0,0" KeyDown="TextBox_KeyDown" /

That has a KeyDown Event associated with it.

        private async void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.Enter)
        {
           Debug.WriteLine("LA");
        }
    }

And the output of this function is:


LA
LA

although I press Enter only once, it prints 2 times. Any reason for that or am I doing something wrong?

Upvotes: 13

Views: 22295

Answers (7)

Ryan D.
Ryan D.

Reputation: 57

I had this same issue in Xamarin.Android where the keypress event fired twice.

In my case it is because the KeyDown and KeyUp are two separate actions which both call the same event. I only respond to one action as below:

    private void KeyPress(object sender, View.KeyEventArgs e)
    {
        if (e.KeyCode == Keycode.Enter)
        {
            if (e.Event.Action == KeyEventActions.Down)
            {
                Event();
            }
        }
    }

Upvotes: 0

Hafiz Temuri
Hafiz Temuri

Reputation: 4112

This might be too late, but e.preventDefault() worked for me. Especially in React - TypeScript.

(e: KeyboardEvent) => {
    e.preventDefault();
    console.log(e);
};

Upvotes: 1

SS_Jin
SS_Jin

Reputation: 61

i was seeing this issue in javascript, where keydown event handler is fired twice, and was able to fix by using .off().on()

$('.text_area').off().on('keydown', function (e) {

// do something

)};

what it does is that, it switches off the extra events and the takes the latest keydown action and executes the handler for it

for me only this solution worked well thanks

Upvotes: -1

brunda reddy
brunda reddy

Reputation: 1

void onKeyPressEvent(object sender, KeyEventArgs e)
{
  if(e.Handled)
  return;

  {
    //
    // the block of codes to be executed on the key press
    // should added here.
    //
  }
  e.Handled = true;
}

Upvotes: 0

MAXE
MAXE

Reputation: 5122

I finally realized that there is probably a bug in the KeyDown event.

When I set, as @daniellepelley said,

e.Handled = true;

the event still propagates: other buttons will intercept it, also if they shouldn't.

In my code, I just replaced KeyDown event with KeyUp event and everything works fine (always setting Handled to True value)!

Upvotes: 4

asitis
asitis

Reputation: 3031

This is a known bug in Windows RT.You can handle it by checking the Key RepeatCount `

if (e.KeyStatus.RepeatCount == 1)
{
   //Execute code
}

Upvotes: 6

daniellepelley
daniellepelley

Reputation: 1979

This should only fire the event once, so if it is firing twice I would check a couple of things.

Check that you aren't handling the key down event on a parent control. This could be a panel or the containing window. Events will bubble down through the visual tree. For example a key down on a textbox will also be a keydown on the window containing the textbox.

To stop this happening you can mark the event as handled as below;

e.Handled = true;

The other thing to check is that you aren't subscribing to the event twice. The XAML will do the same as;

TextBoxUser.KeyDown += TextBox_KeyDown

so check that you don't have this in your code behind.

You can check the sender and e.OriginalSource property to see where the event is being fired from.

Upvotes: 17

Related Questions