How keyboard input works

Source: Internet
Author: User
Windows form Programming How keyboard input works

Windows Forms process keyboard input by triggering a keyboard event to respond to Windows messages. Most Windows Forms applicationsProgramAll handle keyboard input exclusively by handling Keyboard Events. However, you must understand how keyboard messages work to implement more advanced keyboard input schemes (for example, intercepting them before keys arrive at the control ). This topic describes the types of key data that can be recognized by Windows Forms, and describes the transmission mode of keyboard messages. For more information about Keyboard Events, see use Keyboard Events.

Key type

In Windows form, the keyboard is marked as a virtual key represented by the by-bit keys enumeration.Code. UseKeysEnumeration, which can combine a series of buttons to generate a single value. These values correspond to the values of wm_keydown and wm_syskeydown Windows messages. Most physical key operations can be detected by handling keydown or keyup events. The character key isKeysA subset of the enumerated values that correspond to the values that accompany wm_char and wm_syschar Windows messages. If you use a combination of buttons to obtain a character, you can detect the character by handling the keypress event. Alternatively, you can use the keyboard exposed by the Visual Basic programming interface to send keys that have been pressed. For more information, see access the keyboard.

Sequence of Keyboard Events

As listed above, three keyboard-related events may occur on a control. The general sequence of these events is as follows:

    1. Press the "A" key to pre-process and schedule the key.KeydownEvent.

    2. Press and hold the "A" key, and the key is pre-processed and scheduled.KeypressEvent.

      This event occurs multiple times when you press a key.

    3. The user releases the "A" key, which is pre-processed and scheduled and will occurKeyupEvent.

Key preprocessing

Like other messages, keyboard messages are processed in the wndproc method of the form or control. However, before processing a keyboard message, the preprocessmessage method calls one or more methods that can be rewritten to process special character keys and physical keys. You can override these methods to detect and filter certain buttons before the control processes messages. The following table lists the operations being performed and related methods in the order in which the methods appear.

Keydown event preprocessing
Operation Related Methods Description

Check command keys (such as shortcut keys or menu shortcuts ).

Processcmdkey

This method processes the command key, which has a higher priority than the regular key. If this method returnsTrue, The key message is not scheduled, and no key event occurs. If this method returnsFalseIsinputkey will be called.

Check whether the key is a special key to be preprocessed, or whether it isKeydownCommon characters that are scheduled to a widget.

Isinputkey

If this method returnsTrue, Indicates that the control is a regular character, andKeydownEvent. If this method returnsFalseProcessdialogkey is called.

Note:

To ensure that the control obtains a combination of keys, you can handle the previewkeydown event and set the isinputkey of previewkeydowneventargsTrue.

Check whether the key is a navigation key (ESC, tab, enter, or arrow)

Processdialogkey

This method handles the physical buttons that implement special functions in the control (such as switching the focus between the control and its parent. If the intermediate control does not process the key,ProcessdialogkeyUntil the top control in the hierarchy. If this method returnsTrue. If this method returnsFalseKeydownEvent.

Keypress event preprocessing
Operation Related Methods Description

Check whether the key is a common character that the control should process

Isinputchar

If this character is a common character, this method returnsTrueAnd will causeKeypressEvents, and no preprocessing is performed. Otherwise, processdialogchar is called.

Check whether the character is a mnemonic (for example, "OK (& O)" on the button )")

Processdialogchar

SimilarProcessdialogkeyThe method is called up along the control hierarchy. If the control is a container control, this method checks the mnemonic key by calling the processmnemonic of the Control and its child controls. IfProcessdialogcharReturnTrue, ThenKeypressThe event does not occur.

Process keyboard messages

When a keyboard message arrives at a form or controlWndprocMethods are processed by a set of override methods. Each method returns a Boolean value that specifies whether the control has processed and used keyboard messages. If one of the methods returnsTrueThe keyboard message is treated as being processed, and it is not passed to the control's base control or parent Control for further processing. Otherwise, the message will stay in the message queue and may be processed in the control's base control or other methods of the parent control. The following table shows how to process keyboard messages.

Method Description

Processkeymessage

This method processesWndprocAll keyboard messages received by the method.

Processkeypreview

This method sends a Keyboard Message to the parent control of the control. IfProcesskeypreviewReturnTrueOtherwise, processkeyeventargs is called.

Processkeyeventargs

This method is triggered as neededKeydown,KeypressAndKeyupEvent.

Keyboard Rewriting Method

Many methods can be used to preprocess and process keyboard messages; however, these methods are good or bad. The following table shows the possible tasks and the best way to rewrite the keyboard.

Task Method

The navigation key is intercepted and triggered.KeydownEvent. For example, you want to process the tab and enter keys in the text box.

RewriteIsinputkey.

Perform special input or navigation processing on the control. For example, you may want to use the arrow keys in the list control to change the selected item.

RewriteProcessdialogkey

The navigation key is intercepted and triggered.KeypressEvent. For example, you may want to press the arrow key multiple times in the number display box control to speed up the value setting.

RewriteIsinputchar.

InKeypressPerform special input or navigation during the event. For example, in the list control, press the "r" key to jump to an item starting with the letter "R" and switch between these items.

RewriteProcessdialogchar

Execute a custom note key. For example, you want to process the note key that is described by the owner and included in the buttons in the toolbar.

RewriteProcessmnemonic.

See Reference

My. Computer. keyboard object
Keys
Wndproc
Preprocessmessage
Concept

Access Keyboard
Use Keyboard Events

1. Several terminologies about the keyboard
Accelerator key, shortcut cut key, navigation key, Mnemonic Key

Accelerator key and shortcut key are a set of keys corresponding to a program command. When you press this set of keys, the corresponding program command will be executed. for example, when we press Ctrl + A in word, word selects the entire document. CTRL + A is an accelerator key, or an accelerator cut key.

The accelerator key is a name of the previous Win32 program. It is called the shortcut key in winform. in addition, shurtcut key in winform can only be used on menu item, and a shortcut cut key cannot be directly assigned to other control. if you want to add a shortcut key to your button or other control, you can add a menu item and a shortcut cut key to this menu item, then make this menu item invisible. (This is Charles Petzold's suggestion)

Navigation keys are used to move the focus (including TAB, arrow, and ESC)

Mnemonic Key: triggers a click events (for example, click menu item/button) by pressing Alt + A key (if the button name is & OK, the key is O)

2. Origin of keyboard messages:

When you press the key on the keyboard, the OS gets a Keyboard Message (how does the OS get the key message ), the OS extracts the Keyboard Message from the message queue and assigns it to the thread message queue where our application is located, finally, the keyboard message will be allocated to the form or control window function (winproc ). this process is probably like this: the OS sends the Keyboard Message to the Message Queue of the thread where the window with focus is located, and then the thread sends the message to the winproc with the focus form or control. A window with focus is either an active window or a child window of an active window.

When you press the key, the wm_keydown message is generated (only one)
When you press the key, the wm_keypress message is generated (more than one, depending on the duration of the key)
When you release the key, the wm_keyup message is generated (only one)

3. Category of keyboard messages:
Keyboard messages are divided into three groups:

3.1. keydown:
Wm_keydown: It is triggered when the F10 or Alt key is not pressed and the focus is on the active window.
Wm_syskeydown: After F10 is pressed or Alt key is pressed, the key is triggered. The message is also triggered when no window has focus. In this case, wm_syskeydown is sent to the active window for processing.

3.2.char:
Wm_char: code containing the pressed key after the wm_keydown message
Wm_syschar, after the wm_syskeydown message, contains the code for pressing the key that triggers the wm_syskeydown message.

3.3 keyup:

Wm_keyup is issued when the user releases a key
Wm_syskeyup is triggered when the Alt key is pressed and the key that triggers the wm_syskeydown message is released.

4. Process keyboard messages
How is a Keyboard Message processed when it reaches a form or control?
The keyboard message processing process is as follows:
Keyboard Message (wm_keydown, wm_syskeydown) ---> preprocess ---> dispatched ---> keydown event fired

Keyboard Message (wm_char, wm_syschar) ---> preprocess ---> dispatched ---> keypress event fired.

Keyboard Message (wm_keyup, wm_syskeyup) ---> preprocess ---> dispatched ---> keyup event fired.

If a keyboard message can be preprocessed, the keyboard message will not generate a keyboard event.

4. Process keyboard messages
How is a Keyboard Message processed when it reaches a form or control?
The keyboard message processing process is as follows:
Keyboard Message (wm_keydown, wm_syskeydown) ---> preprocess ---> dispatched ---> keydown event fired Keyboard Message (wm_char, wm_syschar) ---> preprocess) ---> dispatched ---> keypress event fired. keyboard Message (wm_keyup, wm_syskeyup) ---> preprocess ---> dispatched ---> keyup event fired. if a keyboard message can be preprocessed, the keyboard message will not generate a keyboard event. 4. the pre-processing of keyboard messages is mainly in control. preprocessmessage in this method. with reflector, you can see that the preprocessing process depends on the current message. a. if the current message is wm_keydown or wm_syskeydown, the preprocessing process is as follows:
Control. preprocessmessage: call these methods for preprocessing according to the following call sequence. 1. Call processcmdkey to determine if it is a shortcut key.
1.1 If the buttons are not found in the shortcut cut keys of the control context menu, processcmdkey calls the processcmdkey of its parent, so that the recursion continues until the main menu of Form
1.2 If true is returned, preprocessing is terminated and no keyboard event (keydown) is generated)
1.3 if the button is not found in cut keys during recursion, false is returned and isinputkey is called.

2. Call isinputkey to determine whether the key is a regular input key (like input on textbox). If yes, end preprocessing and trigger the keydown event. Otherwise, false is returned, and processdialogkey is called.
2.1 If true is returned, the key is treated as the regular input key and no preprocessing is performed, directly triggering the keyboard event.
2.2 If false is returned, continue preprocessing and call processdialogkey 3. Call processdialogkey to determine whether the key is a navigation key (such as tab, arrows, and ESC) and move the focus.
3.1 If the control does not need to process this key, it calls its parent processdialogkey, so recursively until topmost form.
3.2 If true is returned during recursion, preprocessing ends and Keyboard Events are no longer triggered.
3.2 If false is returned during recursion, the preprocessing is complete, but the keyboard event is triggered. if the keyboard message is not preprocessed, a corresponding keyboard event is triggered Based on the message in the processkeyeventargs method of control. b. if the Keyboard Message is wm_char or wm_syschar. preprocessmessage pre-processing is as follows:
Control. preprocessmessage: call these methods for preprocessing according to the following call sequence. 1. Call isinputchar to determine whether preprocessing is required for the current wm_char.
1.1 If true is returned, the current wm_char does not require preprocessing and can be directly handed over to winproc for processing. The Preprocessing ends here to trigger the keyboard event (keypress ).
1.2 if false is returned, the wm_char needs to be preprocessed and processdialogchar is called.
2. processdialogchar: determines whether the current key is an mnemonic key.
2.1 The process of calling processdialogchar is the same as that of calling processdialogkey. It is also a recursive process.
2.2 When processdialogchar is called on a containercontrol, it checks for mnemonics by calling processmnemonic on itself. containercontrol's implementation of processmnemonic iterates through the entire child hierarchy (in tab order) until a child returns true-indicating that it has handled the mnemonic.
2.3 If processmnemonic returns true, the wm_char will not be dispatched, and no keyboard event will be generated. 3. processmnemonic determines whether the current wm_char is a control mnemonic key3.1 this method is called to give a control the opportunity to process a mnemonic character. the method shoshould determine whether the control is in a state to process mnemonics and if whether the given character represents a mnemonic. if so, the method shocould perform the action associated with the mnemonic and return true. if not, the method shocould return false. implementations of this method often use the ismnemonic method to determine whether the given character matches a mnemonic in the control's text. c. if the Keyboard Message wm_keyup, wm_syskeyup
Control. preprocessmessage does not pre-process the message. The message enters the winproc of control, and the processkeyeventargs method triggers the keyup event.

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.