C # handle mouse and keyboard event

xiaoxiao2021-03-06  63

In the operation, there are many mains that generate events, especially in keyboards and mice. This article explores the processing of events related to these two subjects in C #.

One. Program design and running software environment for this article:

(1). Microsoft Window 2000 Server Edition

(2) .. Net Framework SDK Beta 2

two. C # Processes the mouse related events:

There are roughly six species related incidents, namely:

"Mousehover", "MouseEleave", "MouseEnter", "MouseMove", "MouseDown", and "Mouseup".

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

The above event is described in C #, which describes the above events, which describes "Mousehover", "MouseEleAve", "mouseEnter" event's delegate is "EventHandler", and Delegate, which describes the three events, is "MouseEventHandler" to describe . These two delegate are packaged in different namespaces, where "EventHandler" is encapsulated in the "system" namespace; "MouseEventHandler" is encapsulated in "SyeTem.Windows.froms" namespace. In the "Mousehover", "MouseEleAve", "MouseEnter" event is "Eventargs", and he is also encapsulated in the "system" namespace; and the class that provides data is "MouseEventArgs" for the three events behind. He was encapsulated in the "Syetem.Windows.froms" namespace. These have decided to define these events and respond to these events in C #. Here are these different points.

For the first three events described above, it is defined by the following syntax:

"Component Name". "Event Name" = New System.EventHandler ("Event Name");

Below is the specific implementation code in the program:

Button1.mouseLeave = new sytem.evenhandler (Button1_mleave);

After completing the definition of the event, add the code to respond to this event in the program, otherwise it will report an error when the program is compiled. Here is the basic structure in response to the above event.

Private void button1_mleave (Object Sender, System.EventArgs E)

{

Join the code to respond to this event here

}

The syntax defined "Mousemove", "MouseDown" and "Mouseup" events is roughly the same, as follows:

"Component Name". "Event Name" = New System.Windows.Forms. MouseEventHandler ("Event Name");

Below is the specific implementation code in the program:

Button1.mousemove = new system.windows.Forms.MouseEventHandler (Button1_mmove); below is the basic structure of the above event:

Private void button1_mmove (Object Sender, System.Windows.Forms. MouseEventArgs E)

{

Join the code to respond to this event here

}

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

2) Typical problems in mice related events:

We have a typical problem with the mouse-related events in mastering C #. One is to read the current position of the mouse; its second is to determine that the mouse button is pressed.

Determination of the position of the mouse can be processed by event "mousemove", two attributes "X" and "Y" are provided in the "MouseEventArgs" class to determine the current mouse shade and the abscissa. To determine the motion of the mouse button, you can process the event "mousedown" and a property "button" is also provided in the "MouseEventArgs" class to determine the mouse button. Based on this knowledge, you can get a read mouse current location with C # and determine the program code of the mouse button. Below is this code (mouse.cs) and this code compiles the interface:

The source code code for 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: Form1: Form

{

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

File: // Initialize the various components in the form

InitializationComponent ();

}

File: / / Clear the resources used in the program

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Private vidinitiRizeComponent ()

{

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (292, 273);

THIS.NAME = "Form1";

This.Text = "C # handles the mouse to press the event!";

File: / / For the mouse, define an event processing process "Form1_MouseDown"

THIS.MOUSEDOWN = New MouseEventHandler (Form1_MouseDown);

File: / / Define an event handler "Form1_Mousemove" for mouse movement

this.mousemove = new mouseeventhandler (form1_onmousemove);

Static void main ()

{

Application.run (New Form1 ());

}

Private Void Form1_onmousemove (Object Sender, MouseEventArgs E)

{

This.Text = "The location of the current mouse is: (" E.x "," E.Y ")";

}

Private Void Form1_MouseDown (Object Sender, MouseEventArgs E)

{

File: / / Respond to different buttons of the mouse

IF (E.Button == MouseButtons.Left)

{

Messagebox.show ("Press the left button!");

}

IF (e.button == mousebuttons.middle)

{

Messagebox.show ("Press the mouse button!");

}

IF (E.Button == MouseButtons.right)

{

Messagebox.show ("Tap the mouse button!");

}

}

}

three. C # in process and keyboard related events:

There is relatively small events related to the keyboard in C # and the keyboard, which is roughly three: "keydown", "keyup", and "keypress".

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

DELEGATE for "KeyDown" and "Keyup" in C # is "KeyeventHandler". Describes that delegate used by "KeyPress" is "KeyPressEventHandler". These two delegate are packaged in the namespace "SyeTem.Windows.froms". The class for "keydown" and "keyup" is "keyeventargs". The class that provides data for the "KeyPress" event is "keypressEventArgs". The same is also packaged in the namespace "SyeTem.windows.froms".

The syntax of "KeyDown", "Keyup" event is as follows:

"Component Name". "Event Name" = New Syetem.windows.froms. KeyeventHandler ("Event Name");

Below is the specific implementation code in the program:

Button1. Keyup = New Syetem.windows.froms. KeyEventHandler (Button1_kup);

Here is the basic structure in response to the above event.

Private void button1_kup (Object Sender, Syetem.Windows.froms. KeyEventArgs E)

{

Join the code to respond to this event here

}

The syntax of the "keypress" event in the C # program is as follows: "Component Name". "Event Name" = New Syetem.Windows.froms. KeyPressEventHandler ("Event Name);

Below is the specific implementation code in the program:

Button1. Keypress = New Syetem.windows.froms. KeypressEventArgs (Button1_kpress);

After completing the definition of the event, add the code to respond to this event in the program, otherwise it will report an error when the program is compiled. Here is the basic structure in response to the above event.

Private void button1_kpress (Object Sender, Syetem.Windows.froms. KeypressEventArgs E)

{

Join the code to respond to this event here

}

Note: "Button1" that appears in the program is a button component defined.

(

2). Typical problems in the keyboard related events:

The typical problem related to the keyboard is nothing more than that it is determined which button is pressed. It can be done by three events above. And in the "KeyEventargs" class, by "Keycode", you can use him to read the current button. So dealing with this problem in the "Keyup" or "KeyDown" event. According to these knowledge, you can get the program code to read the read button with C #, which is the interface after this code (key.cs) and this code run:

Figure 02: Run interface with C # read keyboard buttons

The code of Key.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: Form1: Form

{

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

File: // Initialize the various components in the form

InitializationComponent ();

}

Protected Override Void Dispose (Bool Disposing)

{

File: / / Clear the resources used in the program

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Private vidinitiRizeComponent ()

{

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 "Form1_Keyup" for the pressing of the button

this.keyup = new keyeventhandler (this.form1_keyup);

Static void main ()

{

Application.run (New Form1 ());

}

File: / / Display the name of the button you press

Private Void Form1_Keyup (Object Sender, Keyeventargs E)

{

Messagebox.show (E.Keycode.toString (), "Into the effect:");

}

}

four. to sum up:

This article describes how to define and mouse and keyboards in C # and how typical problems in these events are described. Although these knowledge is the most basic, it is also most important, because in programming, these issues and the most opportunities to deal. Of course, there are many events and questions related to the mouse and keyboard, which can be solved with reference to the above solution.

转载请注明原文地址:https://www.9cbs.com/read-119026.html

New Post(0)