我正在尝试在 C# wpf 中获取击键,但它不起作用。我的代码错了吗?

I am trying to get keystroke in C# wpf but it's not working. Is my code wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace clipper
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void HandleKeyDownEvent(object sender, KeyEventArgs e)
        {
            MessageBox.Show("It came here");

            if (e.Key == Key.LeftCtrl && e.Key == Key.C)
            {
                MessageBox.Show("You have pressed control + c");
            }
        }
    }
}

这是我的代码。我尝试用谷歌搜索 keyPress 事件的解决方案,并在此处实现了它。但是由于我是 C# 的新手,所以我不确定出了什么问题。请指导我。

您应该将 Key_Up 与以下代码一起使用

private void textBox_KeyUp(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.C) && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
        MessageBox.Show("You have pressed control + c");
    }
}

您必须将 KeyPreview 更改为 true 你能试试 e.Modifiers event 吗?可能会有帮助..

private void Form1_Load(object sender, EventArgs e)
{
    KeyPreview = true;
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Modifiers == (Keys.Shift | Keys.Control))
    {
        MessageBox.Show(".");
    }
}