无法捕捉Enter键 [英] Unable to capture Enter Key

查看:291
本文介绍了无法捕捉Enter键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表格由我带输入:

I have a simple form by which I take input:

12个按键,1个文本框(禁用&安培;只读)

12 Buttons, 1 Textbox (disabled & read-only)

这是我做什么来处理输入

this is what I do to handle input

Login_KeyDown()是常用的方法,我呼吁每一个UI组件和放大器的所有的的KeyDown ;窗体本身。

Login_KeyDown() is common method I call for all the KeyDown of every UI component & the form itself..

private void Login_KeyDown(object sender, KeyEventArgs e)
{            
  if (e.KeyCode == Keys.Escape)
  {
    Application.Exit();
  }
  else if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.D9)
  {
    button3.BackgroundImage = Properties.Resources.button_hover;
    button3.ForeColor = Color.White;
    pin.Text = pin.Text + "9";
  }
  else if (e.KeyCode == Keys.Back)
  {
    button11.BackgroundImage = Properties.Resources.button_hover;
    button11.ForeColor = Color.White;
    if (pin.Text.Length > 0)
      pin.Text = pin.Text.Substring(0, pin.Text.Length - 1);
  }
  else if (e.KeyCode == Keys.Enter)
  {
    MessageBox.Show(pin.Text);
  }
}

此代码工作正常,当我启动应用程序,但后我已经点击任何组件,代码的其余部分工作正常,但进入关键条件是行不通的。

This code works fine when I start the app but after I have clicked on any component, rest of the code works fine but "Enter Key Condition" doesn't work.

我的猜测是因为进入关键条件是不工作UI组件或类似的东西。

My guess is as "Enter Key Condition" is not working for UI components or something like that.

我一直在使用也尝试过的按键事件,它使用的 KeyPressEventArgs 然后选中 KeyChar == 13 但是这也不能正常工作。

I have also tried using "Key Press Event" which uses KeyPressEventArgs then checking KeyChar == 13 but that is also not working.

有什么问题,以及如何解决我这?

PS
我还没有设置任何按钮的任何按钮点击事件,该应用程序是基于100%KBoard。

p.s. I have not set any button click events for any button, the app is 100% KBoard based.

推荐答案

检查出PreviewKeyDown。返回引发按钮控件的事件。

Check out PreviewKeyDown. Return raises that event on button controls.

    private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
            MessageBox.Show("I found return");

    }



或者您可以强制提高这些特殊键KeyDown事件通过使用:

Or alternatively you can force it to raise those special keys in the KeyDown Event by using:

    private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
            e.IsInputKey = true;
    }



更多信息:的 http://msdn.microsoft.com/en-us/library/system.windows.forms.control。 previewkeydown.aspx

这篇关于无法捕捉Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆