开发者

Keyboard keys Capturing

I`m developing this application that need to read every key stroke while the application is focused.

I can read all keys except for 3 keys: Space bar, Enter and backspace.

This is a code snippet form my project:

XAML File:

<TextBox Height="108" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="354" AcceptsReturn开发者_如何学编程="True" AcceptsTab="True" KeyDown="textBox1_KeyDown" IsReadOnly="False" IsEnabled="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />

CS File:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return || e.Key == Key.Space)
        MessageBox.Show("" + e.Key);
}


Instead of KeyDown use PreviewKeyDown and your done -

<TextBox Height="108" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="354" AcceptsReturn="True" AcceptsTab="True" PreviewKeyDown="textBox1_KeyDown" IsReadOnly="False" IsEnabled="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />


See this Link WPF: OnKeyDown() not being called for space key in control derived from WPF TextBox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜