C # responding to Keyboard Events

Source: Internet
Author: User

Process mouse-related events in C:

There are generally six types of mouse-related events:

"Mousehover", "mouseleave", "mouseenter", "mousemove", "mousedown", and "mouseup ".

(1). How to define these events in the C # program:

In C #, different delegate are used to describe the preceding events. The delegate of the "mousehover", "mouseleave", and "mouseenter" events is "eventhandler ", the delegate that describes the next three events is described by "mouseeventhandler. The two delegate are encapsulated in different namespaces, where "eventhandler" is encapsulated in the "System" namespace, and "mouseeventhandler" is encapsulated in "syetem. windows. in the froms namespace. The "eventargs" class used for "mousehover", "mouseleave", and "mouseenter" events is also encapsulated in the "System" namespace; the class that provides data for the next three events is "mouseeventargs", but it is encapsulated in "syetem. windows. froms "namespace. These determine that there are different solutions for defining these events in C # and responding to these events. The following describes these similarities.

The preceding three events are defined using the following syntax:

"Component name". "event name" + = new system. eventhandler ("event name ");

The specific implementation code in the program is as follows:

Button1.mouseleave + = new syetem. evenhandler (button?mleave );

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_mleave (Object sender, system. eventargs E)
{
Add the code to respond to this event.
}

The syntax for defining "mousemove", "mousedown", and "mouseup" events is roughly the same as the preceding three events, as shown below:

"Component name". "event name" + = new system. Windows. Forms. mouseeventhandler ("event name ");

The specific implementation code in the program is as follows:

Button1.mousemove + = new system. Windows. Forms. mouseeventhandler (button?mmove );

The following is the basic structure of the response to the above event:

Private void button=mmove (Object sender, system. Windows. Forms. mouseeventargs E)
{
Add the code to respond to this event.
}

Note: "button1" in the above program is a button component defined.

2) typical problems in mouse-related events:

After understanding the definition of mouse-related events in C #, we will discuss typical problems related to mouse events. One is to read the current position of the mouse, and the other is to determine whether the mouse is pressed.

You can use the event "mousemove" to determine the mouse position. Two attributes "x" and "y" are provided in the "mouseeventargs" class ", to determine the vertical and horizontal coordinates of the current mouse. To determine the mouse button's pressing status, you can use the event "mousedown" to handle it. In the "mouseeventargs" class, a "button" is also provided to determine the mouse button status. Based on this knowledge, you can get the program code written in C # To read the current mouse position and determine the mouse button. The following figure shows the code (mouse. CS) and the runtime interface after the code is compiled:

The source code of mouse. CS is as follows:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Public class form1: Form
{
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
File: // initialize each component in the form
Initializecomponent ();
}
File: // clear resources used in the program
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
Private void initializecomponent ()
{
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,273 );
This. Name = "form1 ";
This. Text = "C # handle mouse-pressing events! ";
File: // define an event processing process for mouse-pressing "form1_mousedown"
This. mousedown + = new mouseeventhandler (form1_mousedown );
File: // define an event processing process for mouse movement "form1_mousemove"
This. mousemove + = new mouseeventhandler (form1_onmousemove );

}
Static void main ()
{
Application. Run (New form1 ());
}
Private void form1_onmousemove (Object sender, mouseeventargs E)
{
This. Text = "the current cursor position is: (" + E. x + "," + E. Y + ")";
}

Private void form=mousedown (Object sender, mouseeventargs E)
{
File: // response to different mouse buttons
If (E. Button = mousebuttons. Left)
{
MessageBox. Show ("click the left mouse button! ");
}
If (E. Button = mousebuttons. Middle)
{
MessageBox. Show ("click it! ");
}
If (E. Button = mousebuttons. Right)
{
MessageBox. Show ("right click! ");
}
}
}

Handle keyboard-related events in C:

There are relatively few keyboard-related events in C #. There are roughly three types: "keydown", "keyup", and "keypress ".

(1). How to define these events in the C # program:

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 namespace "syetem. Windows. froms. 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.

(

2). Typical troubleshooting methods for keyboard-related events:

A typical keyboard-related problem is to determine which button is being pressed. You can complete the preceding three events. In addition, the "keyeventargs" class uses a property "keycode" to read the current key. Therefore, this issue is handled in the "keyup" or "keydown" event. Based on the above knowledge, we can get the program code for reading and writing keys in C #. below is the code (key. CS) and the interface after the code is run:

Figure 02: program running interface for reading keyboard keys with C #

The key. CS code is as follows:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Public class form1: Form
{
Private system. componentmodel. Container components = NULL;

Public form1 ()
{
File: // initialize each component in the form
Initializecomponent ();
}
Protected override void dispose (bool disposing)
{
File: // clear resources used in the program
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
Private void initializecomponent ()
{
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,273 );
This. Name = "form1 ";
This. Text = "C # handle keyboard events! ";
File: // define an event processing process for pressing the button "form1_keyup"
This. keyup + = new keyeventhandler (this. form1_keyup );

}
Static void main ()
{
Application. Run (New form1 ());
}
File: // display the name of the pressed Button
Private void form1_keyup (Object sender, keyeventargs E)
{
MessageBox. Show (E. keycode. tostring (), "The health you are pressing is :");

}
}

The above are all found on the Internet. After reading it, I tried it many times and the buttons didn't respond ......
Later, it was found on csdn. Originally, we had to set the keypreview attribute of the corresponding form to true.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.