سؤال

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. In particular, alt+k. I'm working on 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. Someone could help me to tell me how to concretly get started with events stuff in c#? Thks in advance :)

هل كانت مفيدة؟

المحلول

The MSDN Documentation has a good example of what you need to do. Here are a few important parts:

  • Start with a simple "Windows Forms" application. While there are many other types of applications, WinForms is the simplest to start with.
  • Wire up the KeyPress event. In the Form constructor, you need to tell it what to do when it gets a KeyPress event. 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:

    this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);

  • Use KeyPressEventArgs instead of 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.

  • Pay attention to 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).

EDIT (Thanks @RBarryYoung):

  • Set this.KeyPreview = true in the form constructor. 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. (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

Test it and try make work your way.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top