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