WPF中的KeyPress事件等效于什么?

27

我在WPA中有以下代码,现在尝试将其转换到WPF。我试过使用KeyDown代替Keypress并更改了例如

(e.keyChar == '-') to (e.key == e.Subtract):
  1. 它的工作方式不同了
  2. 我在e.key中找不到等号

第一段代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        foreach (TextBox tb in this.Controls.OfType<TextBox>())
        {
            tb.Enter += textBox_Enter;
        }
    }

    void textBox_Enter(object sender, EventArgs e)
    {
        focusedTextbox = (TextBox)sender;
    }

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == '+')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
            e.Handled = true;
        }
        else if (e.KeyChar == '-')
        {

            if (focusedTextbox != null)
            {
                if (focusedTextbox.Text == "")
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                    operand1.Real = getOperand.Real;
                    operand1.Imag = getOperand.Imag;
                    flag1 = 2;
                }
            }

        }
        else if (e.KeyChar == '*')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
            e.Handled = true;
        }
        else if (e.KeyChar == '/')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
            e.Handled = true;
        }
        else if (e.KeyChar == '=')
        {
            e.Handled = true;
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    if (operand2.Magnitude == 0)
                    {
                        textBox1.Clear();
                        textBox2.Clear();
                        MessageBox.Show("Cannot divide by a number whose magnitude is zero");
                        operand1 = new Complex();
                        operand2 = new Complex();
                        listBox1.ClearSelected();

                    }
                    else
                    operand1 = operand1 / operand2;
                    break;
            }
            string s = operand1.ToString();
            if (flag == 1)
            {
                string[] s1 = s.Split(' ');

                if (s1[1] == "-")
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = "-" + s1[3];
                }
                else
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = s1[3];
                }
            }
            else if (flag == 2)
            {
                string[] s1 = s.Split('@');
                textBox1.Text = s1[0].Trim();
                textBox2.Text = s1[1].Trim();
            }

            listBox1.Items.Add(operand1);
        }

    }

第二段代码:

private void win_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Add)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
            e.Handled = true;

        }
        else if (e.Key == Key.Subtract)
        {

            if (textBox2.Text == "")
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                operand1.Real = getOperand.Real;
                operand1.Imag = getOperand.Imag;
                flag1 = 2;
            }

        }
        else if (e.Key == Key.Multiply)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
            e.Handled = true;
        }
        else if (e.Key == Key.Divide)
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
            e.Handled = true;
        }
        else if (e.Key == Key.Enter)
        {
            e.Handled = true;
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    if (operand2.Magnitude == 0)
                    {
                        textBox1.Clear();
                        textBox2.Clear();
                        MessageBox.Show("Cannot divide by a number whose magnitude is zero");
                        operand1 = new Complex();
                        operand2 = new Complex();
                        listBox1.UnselectAll();
                    }
                    else
                        operand1 = operand1 / operand2;
                    break;
            }
            string s = operand1.ToString();
            if (flag == 1)
            {
                string[] s1 = s.Split(' ');

                if (s1[1] == "-")
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = "-" + s1[3];
                }
                else
                {
                    textBox1.Text = s1[0];
                    textBox2.Text = s1[3];
                }
            }
            else if (flag == 2)
            {
                string[] s1 = s.Split('@');
                textBox1.Text = s1[0].Trim();
                textBox2.Text = s1[1].Trim();
            }


            listBox1.Items.Add(operand1);
        }
    }
3个回答

34

虽然非常相似,但你需要将 e.Key 与 Key 枚举进行比较。

在某个地方注册你的事件处理程序(例如构造函数或 window_loaded):

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
}

然后在事件处理程序中:

void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Subtract)
    {
        // Do something
    }
}

1
我刚刚发现什么都不起作用了,我的意思是我有两个Textchanged事件和KeyDown事件,只有第一个Textchanged事件能够工作,其他的完全没有被执行。 - joseph
1
Window_Loaded是在设计器中双击WPF窗口时为您生成的方法。它在WPF窗口加载完成后运行。看起来您重新输入了该方法,而不是仅在已存在的方法中注册事件处理程序。尝试创建一个全新的WPF应用程序,并在其中使我的示例正常工作。 - Will Faithfull
现在我正在使用具有相同代码的textbox1_keyDown,我做了以下操作: private void textBox1_KeyDown(object sender, KeyEventArgs e) { // MessageBox.Show("msg"); if (e.Key == Key.Add) { //MessageBox.Show("msg2 "); operand1.Real = getOperand.Real; operand1.Imag = getOperand.Imag; flag1 = 1; e.Handled = true; {....} 我可以看到每当我按任何键时都会出现msg1,但是如果我按“+”键,即使关闭了msg1的消息框,我也看不到msg2。 - joseph
问题可能仍然存在,如果(e.Key == Key.Add)------它对所有符号都是一样的,而不仅仅是+。 - joseph
你正在使用 "textBox1" 的 KeyDown 事件,而不是 "MainWindow" 的 KeyDown 事件。请注意,在上面的示例中,我将事件处理程序注册到了 "this.KeyDown"。 - Will Faithfull
我找到了问题所在...我的电脑上没有键盘..所以当我按"+"或"Shift和-"时,key.Add无法读取按键..有什么建议可以表示这些键在key枚举中的表示方式吗? - joseph

16

谢谢,正是我所需要的。您可以获得任何可打印的字符串形式的键。这避免了执行一个依赖于区域的巨大开关语句。 - AndresRohrAtlasInformatik

7
<TextBox x:Name="txtbx" KeyDown="OnKeyDownHandler" Height="23" Width="250">
</TextBox>

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        txtbx.Text = "You Entered: " + txtbx.Text;
    }
}

5
很高兴看到有人包括了XML部分,这样对于那些没有经验的人来说更易懂了。 - NiceDevelopmentq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接