我正在为我的学校项目制作键盘测试程序。除了 F10 键之外,一切都工作正常。每次我按下它时,都会发出错误噪音,我必须再次单击窗口才能继续。如果按下,它应该形成一个绿色矩形。希望有人可以提供帮助。 谢谢。
case Key.F10:
F10.Fill = Brushes.Green;
break;
<!--F10-->
<Rectangle x:Name="F10" Fill="LightGray" Margin="416,25,359,384" Width="25" Height="25">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="KeyDown"></EventTrigger>
</Rectangle.Triggers>
</Rectangle>
<TextBlock Text="F10" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="419,29,0,0"/>
我尝试使用设置来阻止 F10 成为功能键。它用于调试时的单步执行。我已经安装了 Visual Studio Commander 来尝试摆脱它。一切都没有结果。
您可以在代码隐藏中处理 KeyDown 事件,而不是使用 EventTriggers。 SystemKey 属性将告诉您按下了哪个系统键。
<Window x:Class="KeyEnumn.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:KeyEnumn"
mc:Ignorable="d" PreviewKeyDown="Window_PreviewKeyDown"
Title="MainWindow" Height="650" Width="800">
<Grid>
<TextBox Width="100" Height="39" />
<Rectangle x:Name="rF10" Fill="LightGray" Margin="416,25,359,384" Width="25" Height="25">
</Rectangle>
<TextBlock Text="F10" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="419,29,0,0"/>
</Grid>
</Window>
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.System:
if (e.SystemKey == Key.F10)
{
rF10.Fill = Brushes.Green;
break;
e.Handled = true;
}
break;
}
}