当焦点位于按钮上时处理按下键盘上的向上箭头 [英] Handle pressing up arrow on keyboard when focus is on button

查看:30
本文介绍了当焦点位于按钮上时处理按下键盘上的向上箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下键盘上的向上箭头,而按钮具有焦点时,我必须捕捉.我编写了这段代码来处理按钮的 KeyUp 事件:

I have to catch when the user is pressing the up arrow on the keyboard, while the button has the focus. I have written this code to handle the KeyUp event for the button:

private void btnValider_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        //do stuff
    }
}

但是这个函数没有处理按下向上箭头键.

but this function didn't handle pressing the up arrow key.

我不知道我想做的事情是否可行,或者我是否必须从表单中处理这个事件?

I don't know if it what i want to do is possible or if i have to handle this event from the form ?

推荐答案

As Hans Passant 建议,

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Up && btnValider.Focused)
        {
            MessageBox.Show("hit");

            return true;
        }
        else
            return base.ProcessCmdKey(ref msg, keyData);
    }

这篇关于当焦点位于按钮上时处理按下键盘上的向上箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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