简体   繁体   English

TextBox没有监听在C#中按空格键之前输入密钥

[英]TextBox not listening Enter key before Space key is pressed in C#

I have to process an "Enter" key event on text box so I created a new class inheriting from textBox and have already overridden the IsInputKey method. 我必须在文本框上处理“Enter”键事件,所以我创建了一个继承自textBox的新类,并且已经重写了IsInputKey方法。 Problem is the KeyDown event is not being fired with out pressing the space bar first. 问题是KeyDown事件没有被激发而没有先按空格键。

public class EnterTextBox : TextBox
{
    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            return true;
        }
        else
        {
            return base.IsInputKey(keyData);
        }
    }
}

After creating from the above class as 从上面的类创建后

EnterLisTextBox enterTextBox1 = new EnterLisTextBox();            
enterTextBox1.KeyDown += enterTextBox1_KeyDown;

the key down event handler is 键向下事件处理程序是

void enterTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("Pressed enter");;
    }  
}

But this only works if I pressed the "Space bar" before using the "Enter key". 但这只有在我使用“输入键”之前按下“空格键”时才有效。 But once i pressed the space bar even if I retype every thing on the text box it works. 但是,即使我在文本框中重新输入了所有内容,我也按下了空格键。 Why is this? 为什么是这样? Why is it the prerequisite to press the space bar? 为什么按空格键是先决条件?

UPDATE: 更新:

Implementing keyPress event worked for me, but if any one has any idea why the above not worked before using the sapce bar. 实现keyPress事件对我有用,但是如果任何人知道为什么在使用sapce bar之前上面没有工作。 This is the how i used keypress, note the e.KeyChar == '\\r' 这就是我使用按键的方式,请注意e.KeyChar =='\\ r'

void enterTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        MessageBox.Show("Pressed enter");
    }  
}

I guess that the enter key does not count as "normal" key in this event, so you have to explicitly make it an input key in the PreviewKeyDown event method: 我猜在这个事件中输入键不算作“普通”键,所以你必须在PreviewKeyDown事件方法中明确地使它成为输入键:

    private void enterTextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Down:
                e.IsInputKey = true;
                break;
            case Keys.Up:
                e.IsInputKey = true;
                break;
            case Keys.Enter:
                e.IsInputKey = true;
                break;
        }
    }

This is also true for the arrow keys, for example. 例如,对于箭头键也是如此。 And of course, your textbox needs to have the focus first. 当然,您的文本框需要首先关注。

First you have to select TextBox and then you have to press enter key ,otherwise KeyDown Event on Textbox won't be fired. 首先,您必须选择TextBox,然后必须按Enter键,否则不会触发Textbox上的KeyDown事件。

Coming to your case when you press Spacebar your textbox is getting the focus/being selected hence there after your textbox is able to handle enter key event. 当您按空格键时,您的文本框正在获得焦点/被选中,因此您的文本框能够处理输入键事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM