VBForums >
.NET >
C# > [RESOLVED] Creating an event handler for a keyboard character
Click to See Complete Forum and Search --> : [RESOLVED] Creating an event handler for a keyboard character
JenniferBabe
Apr 11th, 2006, 08:32 AM
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
BrandonTurner
Apr 11th, 2006, 09:04 AM
//
// 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");
}
}
JenniferBabe
Apr 11th, 2006, 09:38 AM
Thanks hon, it worked fine!!!!
BrandonTurner
Apr 11th, 2006, 09:54 AM
Youre welcome, dont forget to mark it as resolved. ;)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.