KeyDown event repeats unexpectedly when simultaneously pressing 2 keys (See simplified example)

WWW 1 Reputation point
2022-01-16T04:50:33.553+00:00

I'm using C# & WPF. When using a KeyDown event handler to monitor for keyboard input, everything works fine.
It all works great when pressing and holding 1 key, or even 2.
The problem occurs when I press 2 keys at the same time and hold them. When I do this, the KeyDown event for both keys is repeatedly triggered, and the IsRepeat value is false. It's repeatedly triggering new events when you simultaneously press and hold 2 keys.

Why does this happen, and how can I make it not happen?
Below is a .net core WPF example where I monitor for KeyDown events and look for A or S keys. I can press and hold A and I can press and hold S and I only my code will only generate a non-repeat event once for each. I can even press them quickly in succession. If I press them and hold them at the same exact time, new non-repeating KeyDown events are generated repeatedly.

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Input;

namespace TwoSimultaneousKeyDowns
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            //If A or S is pressed
            if (e.Key.ToString() == "A" || e.Key.ToString() == "S")
            {
                //Checks if it is a repeat event (repeats happen when you hold the button down)
                if (!e.IsRepeat)
                {
                    txtBox1.Text += e.Key.ToString() + " was pressed.\r\n";
                }
            }
        }
    }
}

MainWindow.xaml:

<Window x:Class="TwoSimultaneousKeyDowns.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TwoSimultaneousKeyDowns"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" KeyDown="OnKeyDownHandler">
    <Grid Focusable="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBox x:Name="txtBox1" Grid.Column="1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Height="400" Width="300"/>
    </Grid>
</Window>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,668 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,051 Reputation points Microsoft Vendor
    2022-01-17T06:25:27.397+00:00

    If you want to press the A and S keys at the same time to trigger the event only once for a period of time, you could try to use the following code. If I misunderstood, please let me know.
    MainWindow.xaml:

    <Window x:Class="KeyDownRepeat.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:KeyDownRepeat"  
            mc:Ignorable="d" KeyDown="OnKeyDownHandler" KeyUp="OnKeyUpHandler"  
            Title="MainWindow" Height="450" Width="800">  
        <Grid Focusable="True">  
            <Grid.ColumnDefinitions>  
                <ColumnDefinition />  
            </Grid.ColumnDefinitions>  
            <TextBox x:Name="txtBox1" Grid.Column="1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Height="400" Width="300"/>  
        </Grid>  
    </Window>  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        bool isKeyRepeating = false;  
    
        private void OnKeyDownHandler(object sender, KeyEventArgs e)  
        {  
          if (isKeyRepeating)  
          {  
            return;  
          }  
          else  
          {  
            isKeyRepeating=true;  
            if (e.Key.ToString() == "A" || e.Key.ToString() == "S")  
            {  
              if (!e.IsRepeat)  
              {  
                txtBox1.Text += e.Key.ToString() + " was pressed.\r\n";  
              }  
            }  
          }  
        }  
        private void OnKeyUpHandler(object sender, KeyEventArgs e)  
        {  
          isKeyRepeating = false;  
        }  
      }  
    

    The result:
    165536-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments