如何同时检测两个键输入? [英] How to detect two key inputs at the same time?

查看:79
本文介绍了如何同时检测两个键输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的Windows窗体应用程序中同时检测两个键输入,例如,Ctrl和Z ??

How to detect two key inputs at the same time in my windows forms app for example, Ctrl and Z??

推荐答案

您需要添加一个事件处理程序 - 要识别它的控件的 KeyDown -event的方法。您可以通过Designer通过Properties>来完成此操作。事件或手动。 TextBox-Control的示例:

You need to add an eventhandler-method to the KeyDown-event of the control(s) for which you want to recognize this. You can do this either through the Designer via Properties > Events or manually. Example here for a TextBox-Control:
// if you want to add it "manually" then place this
// line in the constructor of the form:
SomeTextBox.KeyDown += SomeTextBox_KeyDown;

private void SomeTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control)
    {
        // do something here
    }
}



如果要为多个控件处理此事件,可以将相同的eventhandler方法添加到许多控件的事件中:


If you want to process this event for more than one control, you can add the same eventhandler-method to the events of many controls:

SomeTextBox.KeyDown += SomeControl_KeyDown;
SomeOtherTextBox.KeyDown += SomeControl_KeyDown;
SomeOtherControl.KeyDown += SomeControl_KeyDown;



在这种情况下,如果你想知道事件来自哪个控件,你必须查看参数 sender 这将是对该控件的引用。



如果您还想为表单本身接收该事件你必须将其属性 KeyPreview 设置为true。


In that case, if you want to know from which control the event originated, you have to look at the argument sender which will be a reference to that control.

If you want to receive that event also for the Form itself you have to set its property KeyPreview to true.


你可以使用 control.modifierkeys [ ^ ]检查是否< ; alt>,< ctlr>或者< shift>当您在GUI应用程序中获得输入时按下。

在控制台应用程序中,您可以使用 ConsoleKeyInfo.ConsoleModifiers [ ^ ]。
You can use control.modifierkeys[^] to check if <alt>, <ctlr> or <shift> are pressed when you get an input in GUI apps.
In console apps you can use ConsoleKeyInfo.ConsoleModifiers[^].


这篇关于如何同时检测两个键输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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