A successful application system is always inseparable from the input of the keyboard. In other words, the application software needs to interact with the user through tools such as keyboards. Although this is a basic work, it contains a lot of skills. In VisualStudio2010, in general, you accept information from the keyboard by handling keyboard events. The author here takes a Windows Form as an example to talk about some of the tips when it comes to receiving keyboard input.
First, the KeyDown incident.
The KeyDown event is an event that is triggered when a user knocks on the keyboard. This is an initial triggering event that triggers the event as soon as the user presses the keyboard. Mastering the relevant content of this event is the basis for developers to handle keyboard input. The author thinks, can from the following several aspects to carry on the understanding.
One is to master the KeyEventArgs parameters. Simply put, this parameter specifies a physical keyboard button. But as the hardware upgrades, its functionality is much more than that. There are key combinations on the keyboard now. If you now want to enter an uppercase character in the lowercase state of your English, you need to use the key combination of shift and any one character. If you need to enter a number key above the special match, also need to use the shift combination key and so on. The application software needs to determine whether the user is using a key combination, and this is the parameter. That is, when writing code in VisualStudio2010, you may need to use an if statement or some other similar syntax to determine whether the user has a key combination.
The second is to understand the meaning of the modiiers attribute. Note that it is not possible to name it here. In fact, this property will be used in conjunction with the above parameter. As we have just said, users may use key combinations when they enter. Now one of the problems we use is that there are three key combinations, shift, CTRL, and ALT, respectively. The combination of different keys and other keys often play a different effect. To do this, the application not only needs to know whether the user has pressed multiple keys at the same time, but also to understand which key combination the user is pressing. This property is used to record which of the three keys the user is pressing. When writing code, you might want to use a case statement to select a different action based on the user's behavior.
The third is the Deydate attribute. In the Windows operating system, you can press SHIFT, CTRL, and alt three keys at the same time. The system jumps out of task manager at this point. During application development, users may also need to press the three keys at the same time to complete a specific task. The property you need to use at this point is the Keydata property. Typically, developers can use constants from the keys to extract information from the Keydata property. Using the AND operator (which means that three keys are pressed at the same time) compares the data returned by Keydata with the constants in the keys to obtain information about the user pressing the key.
After understanding these parameters and attributes, the KeyDown event is basically mastered. The author once again emphasizes that the Keydows event is a necessary link for an application to accept keyboard input. Developers have to master.
Second, the KeyPress incident.
This event is also triggered when the user presses the keyboard. However, the sequence of triggers is relatively late. That is, the event is triggered only after the KeyDown event has been successfully triggered. There is a sequential order between the two. This is the first thing to know when mastering this event.
Also, this keypress event is not sensitive to all the keys. In general, this time is triggered when the user presses a key that produces characters. This event is not triggered when the user presses a numeric key on the keyboard. The first event mentioned above, in any case, is triggered whenever the user presses the button on the keyboard. It should be noted, however, that sometimes when a user presses any key, the KeyPress event is triggered. This is the second difference between the two events.
In this event, a KeyPressEventArgs parameter is mainly used. Simply put, this parameter is mainly used to represent the character code of the key. Because character codes are unique for each combination of character keys and key combinations, the value of this parameter is unique. For example, the character a key on the keyboard will produce different character codes in different situations. If the uppercase control key is turned on and off, the character code generated in the background is different when the user presses the character a key. When writing code, developers don't need to know exactly when a user presses a button. Just know that the different character codes represent what they mean. A character code of 65 means that the user presses the SHIFT key and a key at the same time. At this point in the code, you need to determine the following behavior based on the current state of the keyboard. If the keyboard is currently in uppercase, the information that the user wants to enter is lowercase a. Conversely, the information that the user wants to enter is uppercase character A.
The last thing to note is that developers can cancel this event when necessary. That is, even if the user presses a button on the keyboard, it does not trigger the event. In some cases, this function is quite useful. If a billing document has been approved, no further changes are allowed. Alternatively, only the user is allowed to operate through the mouse, not through the keyboard. To put it simply, you need the system to ignore the user's keyboard input, and you need to use this method at this point. In fact, the means of implementation is also very simple, only need to use the Handled property. When this property is set to True, the Keypress event is canceled.