When writing WinForm applications, it is sometimes necessary to do the same for a keyboard input or mouse event regardless of which control gets the focus. For example, when you write an application that handles pictures, I hope that regardless of which control gets the focus, when the user presses the top, bottom, left, and right keys, the scroll bar of the picture control moves up and down, and the picture always gets larger or smaller when the user toggles the mouse wheel. If you set the keyboard or mouse events for each control, you will inevitably cause a lot of duplication of effort. In this article I'll show you how to do the same for a keyboard input or mouse event under WinForm.
1. Capture and rewrite of keyboard events
The first is the keyboard operation, in order to achieve the above functions, now introduce a method of WinForm. processCmdKey. The address of its MSDN description is: MSDN
processCmdKey Introduction
As you can see from the function name, this is actually the function that sets the shortcut key for WinForm.
" call this method during message preprocessing to process the command key. a command key is a key that always has precedence over a regular input key. Examples of command keys include shortcut keys and menu shortcuts. This method must return true to indicate that it has finished processing the command key, or false to indicate that the key is not a command key. only if the control is hosted on
This method is called in a Windows forms application or when it is acting as an ActiveX control. ”
The above is an introduction to this function by MSDN. This means that the keyboard action response event defined in the function has the highest priority and is executed before all the keyboard response time. If you want to perform an event that is related to the keyboard input after the event is executed, return False (that is, the input event is considered unhandled) and return TRUE if you want to mask other response times (that is, tell the application that the keyboard input has already been processed and will not be processed later).
So with this function it's good to mask the other keyboard input response events in the program, so that you can define the response to each keyboard input yourself.
An example is given below:
protected override bool processCmdKey (ref Message msg, Keys keyData) { switch (keyData) { case Keys.right: MessageBox.Show ("right"); return true; Case Keys.left: MessageBox.Show ("left"); return true; Case Keys.up: MessageBox.Show ("Up"); return true; Case Keys.down: MessageBox.Show ("Down"); return true; Case Keys.space: MessageBox.Show ("Space"); return true; Case Keys.enter: MessageBox.Show ("Enter"); return true; } return false; }
In the actual program, just add the above code to the form class of the WinForm application and replace the MessageBox with the functionality you want to implement.
2, for the mouse event, there are the above requirements are mainly the mouse wheel scrolling events. Because the mouse click and double-click Move and other events are related to the mouse pointer, there is no need to do the same for all, only the mouse wheel events and the current pointer to the point of the relationship between the mouse, it is necessary to perform the same operation has some practical significance. As mentioned earlier, in applications that work with pictures, you may need to zoom in on the mouse wheel, regardless of the circumstances, and the roll-up corresponds to a zoomed-out image.
A mouse event delegate is required for this feature
The specific action is to delegate the mouse wheel event to a custom function when the form is initialized, with the following code:
Public Form1 () { InitializeComponent (); This. mousewheel+= new MouseEventHandler (Mymousewheel); }
The second sentence means that the mouse wheel event of the program is bound to the Mymousewheel function, and then the subsequent definition of a mymousewheel function can be
The Mymousewheel function I define is as follows:
private void Mymousewheel (object sender, System.Windows.Forms.MouseEventArgs e) {if (E.delta > 0) {MessageBox.Show ("scroll wheel Forward"); } else {MessageBox.Show ("scroll wheel Backward"); } }
Replace the MessageBox with the functionality you want to implement in a specific application.
Capture and rewrite of keyboard and mouse events in WinForm (GO)