Processing the keyboard PDF Print E-mail

The keyboard can also be processed by overriding the appropriate method of the Control class. Here we overrride the method OnKeyDown which is called everytimes the user press a key.

  1.  
  2. using System;
  3. using System.Windows.Forms;
  4. class KeyBoardSample : Form {
  5. public static void Main() {
  6. Application.Run( new KeyBoardSample() );
  7. }
  8. protected override void OnKeyDown( KeyEventArgs e ) {
  9. if( e.KeyCode == Keys.Enter )
  10. MessageBox.Show( "You press Enter" );
  11. }
  12. }
 

The method OnKeyDown of the Control class expects a KeyEventArgs as argument. An object of the type KeyEventArgs has a KeyCode property which can be used for testing against the values of the Keys enumeration.

Here we test for the Enter key and we display a message box when the user has pressed that key.




Bookmark it...
Digg!Reddit!Del.icio.us!Google!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!
 
< Prev   Next >
Joomla Template by Joomlashack
Joomla Templates by JoomlaShack Joomla Templates by Compass Design