简体   繁体   English

如何开始使用C#测试按键事件?

[英]How to get started with c# for testing keypressed event?

I am quite new to c# , and the first thing i want to do is being familiar with that environment by trying Key's Combination Events . 我对c#相当陌生,我想做的第一件事是通过尝试Key的Combination Events熟悉该环境。 In particular, alt+k . 特别是alt + k I'm working on Microsoft Visual c# 2010 Express . 我正在使用Microsoft Visual c#2010 Express I want to test if that code works. 我想测试该代码是否有效。 If errors are found, please notify me :) 如果发现错误,请通知我:)

public void begin(object sender, KeyEventArgs ev)
{
    if (ev.KeyCode == Keys.K && ev.Modifiers == Keys.Alt)
    {
        //display a message
    }
}

but even if I know theoretically what are the different models of projects that are proposed when clicking on new project and what are their uses, I tried unsuccessfully several models to test that code. 但是,即使我从理论上知道点击新项目时提出的不同项目模型是什么,以及它们的用途是什么,我也没有成功尝试使用多个模型来测试该代码。 In short, i don't know what models to choose and where to put code for testing that kind of simple code, and more precisely working on events(key+mouse) with a minimalist gui. 简而言之,我不知道该选择哪种模型以及在哪里放置代码来测试这种简单的代码,更确切地说,是使用最小化的GUI处理事件(键+鼠标) Someone could help me to tell me how to concretly get started with events stuff in c# ? 有人可以帮助我告诉我如何确切地开始使用C#中的事件内容吗? Thks in advance :) 提前Thks :)

The MSDN Documentation has a good example of what you need to do. MSDN文档提供了一个很好的示例,说明您需要做什么。 Here are a few important parts: 以下是一些重要的部分:

  • Start with a simple "Windows Forms" application. 从一个简单的“ Windows窗体”应用程序开始。 While there are many other types of applications, WinForms is the simplest to start with. 尽管有许多其他类型的应用程序,但是WinForms是最简单的入门。
  • Wire up the KeyPress event. 连接KeyPress事件。 In the Form constructor, you need to tell it what to do when it gets a KeyPress event. 在Form构造函数中,您需要告诉它在收到KeyPress事件时该怎么办。 If you change the name of your function begin in your question above to Form1_KeyPress (to more-accurately describe what it does), the following code should work: 如果将函数名称更改为Form1_KeyPress (以更准确地描述它的作用),请在上面的问题中begin ,下面的代码应该起作用:

    this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);

  • Use KeyPressEventArgs instead of KeyEventArgs . 使用KeyPressEventArgs而不是KeyEventArgs It may or may not make a huge difference, but it is good to use the most-specific EventArgs when you can so you can use properties specific to it. 这可能会或可能不会有很大的不同,但是最好在可能的情况下使用最特定的EventArgs ,以便可以使用特定于它的属性。

  • Pay attention to KeyPressEventArgs.Handled . 注意KeyPressEventArgs.Handled If you have KeyPress (or some other keyboard events) on other objects on your forms, such as buttons or text boxes, you need to say whether the event was handled there or whether it should bubble up to the parent (in your case, to the Form). 如果您在窗体上其他对象(例如按钮或文本框)上具有KeyPress(或其他一些键盘事件),则需要说明是否在该事件中处理了该事件,或者该事件是否应该向上级传递给父对象(在您的情况下,表格)。

EDIT (Thanks @RBarryYoung): 编辑 (感谢@RBarryYoung):

  • Set this.KeyPreview = true in the form constructor. 在表单构造函数中设置this.KeyPreview = true In order for your form to receive keyboard events that happen on child controls (such as buttons and text boxes) as mentioned in the tip immediately above this one, you need to set this property to true to allow the form to get a first look at Keyboard events that happen on the child. 为了使您的表单接收在子控件(如按钮和文本框)上发生的键盘事件(如该提示正上方的提示中所述),您需要将此属性设置为true以使表单能够初看在孩子身上发生的键盘事件。 (Note: of course, this tip and the previous one only apply if you want the form to see these events. Sometimes, you might want keyboard shortcuts to be trapped in the child control instead of being handled in the parent.) (注意:当然,本技巧和上一个技巧仅在您希望表单看到这些事件时才适用。有时,您可能希望将键盘快捷键捕获在子控件中,而不是在父控件中处理。)

It can help you: 它可以帮助您:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.K && this.AcceptButton == null)
    {
        MessageBox.Show("Key K pressed");
    }

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

It will detect if key K is pressed in any control of your form and return msgbox 它将检测是否在窗体的任何控件中按下了K键并返回msgbox

Test it and try make work your way. 测试它并尝试按自己的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM