33 lines
1.0 KiB
C#
33 lines
1.0 KiB
C#
using System.Windows;
|
|
using System.Windows.Input;
|
|
|
|
namespace WPF路由事件_6_获取键盘状态;
|
|
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void InputTextBox_OnPreviewKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
var keyboardDeviceModifiers = e.KeyboardDevice.Modifiers & ModifierKeys.Control;
|
|
if (keyboardDeviceModifiers == ModifierKeys.Control) InputTextBox.Text = "好了";
|
|
|
|
// 判断是否按下了按键
|
|
if (e.KeyboardDevice.IsKeyDown(Key.Z)) InputTextBox.Text = "按下了 Z 键";
|
|
|
|
// 判断是否按键释放
|
|
if (e.KeyboardDevice.IsKeyUp(Key.LeftShift)) InputTextBox.Text = "左边的 LeftShift";
|
|
|
|
// 大写键是否开启
|
|
if (e.KeyboardDevice.IsKeyToggled(Key.CapsLock)) InputTextBox.Text = "大写键是否开启或关闭";
|
|
|
|
var keyStates = e.KeyboardDevice.GetKeyStates(Key.A);
|
|
Console.WriteLine(keyStates);
|
|
}
|
|
} |