C# 按下 ALT+KEY 时处理 KeyDown 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2146970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:54:19  来源:igfitidea点击:

Handle the KeyDown Event when ALT+KEY is Pressed

c#winformskeyboarduser-input

提问by Jedi Master Spooky

How do you handle a KeyDownevent when the ALT key is pressed simultaneously with another key in .NET?

KeyDown当 ALT 键与 .NET 中的另一个键同时按下时,您如何处理事件?

采纳答案by Jedi Master Spooky

This is the code that finally Works

这是最终有效的代码

if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z &&  e.Alt){
     //Do SomeThing
}

回答by Henk Holterman

Something like:

就像是:

   private void Form1_KeyDown(object sender, KeyEventArgs e)
   {
        if (e.Alt)
        {
            e.Handled = true;
            // ,,,
        }
    }

回答by Oded

The KeyEventArgsclass defines several properties for key modifiers - Altis one of them and will evaluate to trueif the alt key is pressed.

KeyEventArgs类定义了几个属性的关键调节剂- Alt键就是其中之一,实际等于true如果Alt键被按下。

回答by kor_

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Alt && e.KeyData != (Keys.RButton | Keys.ShiftKey | Keys.Alt))
    {
        // ...
    }
}

回答by Jim Lahman

I capture the alt and down or up arrow key to increment the value of a numericUpDown control. (I use the alt key + down/up key because this form also has a datagridview and I want down/up keys to act normally on that control.)

我捕获 alt 和向下或向上箭头键以增加 numericUpDown 控件的值。(我使用 alt 键 + 向下/向上键,因为此表单也有一个 datagridview,我希望向下/向上键在该控件上正常操作。)

    private void frmAlzCalEdit_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Alt && e.KeyCode == Keys.Down)
        {
            if (nudAlz.Value > nudAlz.Minimum) nudAlz.Value--;

        }
        if (e.Alt && e.KeyCode == Keys.Up)
        {
            if (nudAlz.Value < nudAlz.Maximum) nudAlz.Value++;
        }

    }

回答by Hesein Burg

Create a KeyUp event for your Form or use a library like I did to get a GlobalHook so you can press these keys outside the form.

为您的表单创建一个 KeyUp 事件,或者像我一样使用库来获取 GlobalHook,这样您就可以在表单外按下这些键。

Example:

例子:

 private void m_KeyboardHooks_KeyUp(object sender, KeyEventArgs e)
                {
                    if ( e.KeyCode == Keys.Alt || e.KeyCode == Keys.X)
                    {     


                    }
                }