为什么此KeyPress方法不接收我的键盘输入? [英] Why isn't this KeyPress method picking up my keyboard input?

查看:82
本文介绍了为什么此KeyPress方法不接收我的键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Windows窗体代码中包含以下代码,并且由于任何原因,由于未执行我的代码,因此没有采取任何按键操作.例如,按空格键将隐藏窗口.没有.

I have the following code in my Windows Form code and for whatever reason the key presses aren''t being picked up because my code isn''t being executed. For example, pressing Space will hide the window. It doesn''t.

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar.Equals(Keys.Space))
    {
        this.Hide();
        restoreToolStripMenuItem.Enabled = true;
    }
    else if (e.KeyChar.Equals(Keys.M))
    {
        //If the menu is hidden, show it. Otherwise, hide it.
        if (menuTrayPanel.Tag.Equals("hidden"))
        {
            animationTimer.Tick += new EventHandler(displayMenu);
            animationTimer.Tick -= new EventHandler(hideMenu); //Don't trigger the hideMenu void when displaying it
            displayMenu(sender, e);
        }
        else
        {
            animationTimer.Tick -= new EventHandler(displayMenu);
            animationTimer.Tick += new EventHandler(hideMenu); //Same as with displaying, but the other way
            hideMenu(sender, e);
        }
    }
}

推荐答案

某些具有焦点的控件可能会占用该事件,因此不会为表单调用该事件. Windows窗体中有一些属性,使窗体可以侦听未在窗体本身上引发的事件.该属性的名称具有预览.由于我是通过手机编写的,因此无法向您提供更多详细信息.请在Google上获取Winform事件预览.

希望有帮助.

MRB
Some control that has the focus may consume the event so it isn''t invoked for the form. There is some property in a windows form that will allow the form to listen in on events that were not raised on the form itself. The property has preview in its name. Since I''m writing this from my mobile I can''t give you more details. Please google for winform event(s) preview.

Hope that helps.

MRB


您是否将方法附加到事件上?
Did you attach your method to the event?


这篇关于为什么此KeyPress方法不接收我的键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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