Results 1 to 4 of 4

Thread: [RESOLVED] Creating an event handler for a keyboard character

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Resolved [RESOLVED] Creating an event handler for a keyboard character

    Hi guys. Is there any way to create an even handler for a keyboard key. e.g. similar to a normal event with a button. But now all I want is like to press a certain key, say 'm' and write some code when the user presses this key. Is there any way to do this? so let's say, the program is up and running, and when the user presses the 'm' key, a message box appears with "hello". Is there any way to do this?

    Jennifer

  2. #2
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268

    Re: Creating an event handler for a keyboard character

    Code:
    //
    // In the constructor
    //
    AddEvent(this)
    
          private void AddEvent(Control control)
          {
             control.KeyDown += new KeyEventHandler(control_KeyDown);
             foreach (Control co in control.Controls)
             {
                AddEvent(co);
             }
          }
    
          private void control_KeyDown(object sender, KeyEventArgs e)
          {
             //
             // When the user presses 'm'(keyvalue 77) trigger the message box
             //
             if (e.KeyValue == 77)
             {
                MessageBox.Show("Hello");
             }
          }

  3. #3

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Creating an event handler for a keyboard character

    Thanks hon, it worked fine!!!!

  4. #4
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268

    Re: Creating an event handler for a keyboard character

    Youre welcome, dont forget to mark it as resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width