Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串转换为键_C#_Wpf_Input - Fatal编程技术网

C# 将字符串转换为键

C# 将字符串转换为键,c#,wpf,input,C#,Wpf,Input,我正在尝试将一个简单的字符串转换为一个键,我找到了一些解决方案,但大多数都是针对winforms的,或者它们没有显示完整的代码,所以我不理解 这就是我想要实现的基本目标 namespace KeyDown { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window {

我正在尝试将一个简单的字符串转换为一个键,我找到了一些解决方案,但大多数都是针对winforms的,或者它们没有显示完整的代码,所以我不理解

这就是我想要实现的基本目标

namespace KeyDown
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string CustomKey = "B";
        public MainWindow()
        {
            InitializeComponent();
            activeArea.Focus();
        }

        private void activeArea_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.CustomKey)
            {
                MessageBox.Show("Key Pressed");
            }
        }
    }
}
namespace键关闭
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共字符串CustomKey=“B”;
公共主窗口()
{
初始化组件();
activeArea.Focus();
}
私有void activeArea_KeyDown(对象发送方,KeyEventArgs e)
{
if(e.Key==Key.CustomKey)
{
MessageBox.Show(“按键”);
}
}
}
}

您可以在WPF中使用
System.Windows.Input.Key
枚举:

using System.Windows.Input;

...

public Key CustomKey = Key.B;

// or this if you really want to convert the string representation
public Key CustomKey = (Key)Enum.Parse(typeof(Key), "B");


private void activeArea_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == CustomKey)
    {
        MessageBox.Show("Key Pressed");
    }
}