If you want to press F1 to bring up the CHM help, Code As follows: Private Void Frmmain_load ( Object Sender, eventargs E)
{
This . Keypreview = True ; // Gets or sets a value that indicates whether the form will receive this key event before passing a key event to a control with focus.
}
Private VoidFrmmain_keyup (ObjectSender, keyeventargs E)
{
If(Keys. F1 = E. keycode)
{
// MessageBox. Show ("The key you press is:"+ E. keycode. tostring ());
Help. showHelp (this, @ "C: \ Users \ Hongye \ Desktop \ revitapi. chm ");
}
}
A keyboard event occurs when you press a key on the keyboard. There are two types of events. The first type is the keypress event, this type of event is triggered when the key is a ASCII character, you can use its keypresseventargs attribute keychar to determine the ASCII . Use keypress time cannot determine whether the modification key is pressed ( shift, alt and CTRL ). To determine these actions, keyup or keydown events, which constitute the second type of events. This type of event has a keyeventsargs type parameter, you can use this parameter to test whether some modification keys are pressed.
1. keypresseventargsMain attributes of the class (KeypressA parameter type of the event)
(1). HandleAttribute: used to obtain or set a value that indicates whether it has been processedKeypressEvent.
(2). KeycharAttribute: used to obtain the character corresponding to the pressed key, usuallyASCIIMA (the return value is the keyboard value, for example, K is returned by pressing 'k ).
2 . Keyeventargs Main attributes of the class (Keyup And Keydown An event parameter)
( 1 ) . ALT Attribute: used to obtain a value, indicating whether the value has been pressed ALT Key
( 2 ) . Control Attribute: used to obtain a value, indicating whether the value has been pressed Crtl Key
( 3 ). Shift Attribute: used to obtain a value, indicating whether the value has been pressed Shift Key
( 4 ) . Handle Attribute: used to obtain a value indicating whether the event has been processed
( 5 ) . Keycode Property: Keys The enumerated Value Returns the key code of the keyboard key. This attribute does not contain the modification key information and is used to test the specified keyboard key.
( 6 ) . Keydata Property:Keys The enumerated Value Returns the key code of the keyboard key. This attribute contains the key modification information, which is used to determine all the information of the press Key (you can return the key combination)
( 7 ) . KeyValue Attribute: return the key code in integer format, instead Keys Enumeration type value. Used to obtain the number representation of the pressed key
( 8 ) . Modifiers Property: Keys The enumerated Value Returns the modified key. Only the information of the modified key is determined.
3. in C #ProgramDefine these events:
In C #, the delegate of the events "keydown" and "keyup" is "keyeventhandler ". The delegate used to describe "keypress" is "keypresseventhandler ". Both delegate are encapsulated in the "syetem. Windows. froms" named empty. The class that provides data for "keydown" and "keyup" events is "keyeventargs ". The class that provides data for the "keypress" event is "keypresseventargs ". The two are also encapsulated in the namespace "syetem. Windows. froms.
The syntax for defining "keydown" and "keyup" events in the C # program is as follows:
"Component name". "event name" + = new syetem. Windows. froms. keyeventhandler ("event name ");
The specific implementation code in the program is as follows:
Button1. keyup + = new syetem. Windows. froms. keyeventhandler (button%kup );
The following is the basic structure of the response to the above event.
Private void button%kup (Object sender, syetem. Windows. froms. keyeventargs E)
{
Add the code to respond to this event.
}
The syntax for defining the "keypress" event in the C # program is as follows:
"Component name". "event name" + = new syetem. Windows. froms. keypresseventhandler ("event name ");
The specific implementation code in the program is as follows:
Button1. keypress + = new syetem. Windows. froms. keypresseventargs (button#kpress );
After defining an event, you must add the code to the program to respond to the event. Otherwise, an error will be reported during compilation. The following is the basic structure of the response to the above event.
Private void button1_kpress (Object sender, syetem. Windows. froms. keypresseventargs E)
{
Add the code to respond to this event.
}
Note: The "button1" in the program is a defined button component.
Example:
Private void form1_keyup (Object sender, keyeventargs E)
{
MessageBox. Show ("the key you pressed is:" + E. keycode. tostring ());
}
URL: http://greatverve.cnblogs.com/archive/2012/05/15/KeyCode.html