转发KeyPress事件

时间:2009-07-06 16:48:41

标签: c# events

我有一个TextBox,想从另一个Form转发KeyPress事件。

到目前为止,我有我的表格:

private readonly Action<KeyPressEventArgs> m_KeyPress;

public KeyboardForm( Action<KeyPressEventArgs> keyPress )
{
  m_KeyPress = keyPress;
}

protected override void OnKeyPress( KeyPressEventArgs e )
{
  m_KeyPress( e );
  base.OnKeyPress( e );
}

一个派生的TextBox,用于初始化Form:

var keyboardForm = new KeyboardForm( OnKeyPress );
keyboardForm.Show();

现在,按预期调用OnKeyPress方法(Form,然后是TextBox)。但是没有任何反应......当我按下'a'时我希望'a'出现在我的TextBox中......

有没有人知道这里的问题是什么?

它也无法与KeyDown一起使用,并且附加到常规公开事件KeyPress也无法帮助我。我认为,问题是OnKeyPress的明确调用。是允许的吗?

3 个答案:

答案 0 :(得分:2)

表格1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Form2 f = new Form2();
        f.mEvent += new Form2.TestEvent(f_mEvent);
        f.Show();
    }

    void f_mEvent(KeyPressEventArgs e)
    {
        textBox1.Text += e.KeyChar;
    }
}

表格2:

public partial class Form2 : Form
{
    public delegate void TestEvent(KeyPressEventArgs e);
    public event TestEvent mEvent;

    public Form2()
    {
        InitializeComponent();
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (mEvent != null)
        {
            mEvent(e);
        }
        base.OnKeyPress(e);
    }
}

答案 1 :(得分:1)

这应该做你想要的。根据{{​​3}}制作键盘表单上的按钮文本。例如,如果您想要小写字母a,只需为键盘按钮文本添加“a”即可。如果你想要一个退格按钮,只需将“退格”作为按钮的文本。所有键盘按钮Click事件都可以注册ButtonClick功能

键盘形式:

public partial class KeyboardForm : Form
{
    public delegate void ButtonPressed(string keyPressed);
    public event ButtonPressed ButtonPressedEvent;

    public KeyboardForm()
    {
        InitializeComponent();
    }

    private void ButtonClick(object sender, EventArgs e)
    {
        Button button = sender as Button;
        if (button != null)
        {
            if ((ButtonPressedEvent != null))
            {
                ButtonPressedEvent("{"+button.Text+"}");
            }
        }
    }
}

表单,其中包含用户输入内容的文本框:

public partial class Form1 : Form
{
    private KeyboardForm mKeyboardForm = new KeyboardForm();
    private bool mIsKeyboardCode = false;

    public Form1()
    {
        InitializeComponent();
        mKeyboardForm.ButtonPressedEvent += new KeyboardForm.ButtonPressed(KeyboardFormButtonPressedEvent);
    }

    void KeyboardFormButtonPressedEvent(string keyPressed)
    {
        mIsKeyboardCode = true;
        textBox1.Focus();
        SendKeys.SendWait(keyPressed.ToString());
        mKeyboardForm.Focus();
        mIsKeyboardCode = false;
    }

    private void TextBoxKeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.ControlKey)
        {
            if (!mKeyboardForm.Visible)
            {
                mKeyboardForm.Show(this);
                e.Handled = true;
            }
        }
        else if (!mIsKeyboardCode)
        {
            mKeyboardForm.Hide();
        }
    }
}

注意:我没有使用带有表单的扩展文本框。我不认为这是一个很好的设计,可以在自定义文本框中显示/隐藏表单。

答案 2 :(得分:0)

从此答案开始:https://social.msdn.microsoft.com/Forums/windows/en-US/92215fdf-8be0-4e3a-b796-dd7c0f131666/keypreview-true-how-do-you-then-detect-enterreturn?forum=winforms

如果您的表单上有一个按下Enter键的按钮,则可以使用以下代码将Enter键作为普通的KeyPreview键处理:

void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        e.IsInputKey = true;
    }
}