The standard Windows event model is that the keyboard focus window receives all keyboard events. Remember that in Windows everything - this window - “control” - is just a window that is a child of another window. It is up to this window to send messages to his parent if he wants to do this when some keys are pressed.
To standardize navigation between controls in a dialog box, Windows also provides a "dialog manager." In the native code for modal dialogs, this is handled by the modal outline of the message inside the DialogBox function. For modeless dialogs, you should call IsDialogMessage inside your own message loop. Here's how he steals the Tab and cursor keys to move between controls, and Enter to press the default button. This has the opposite effect of not letting controls handle Enter by default, which typically handle multi-line controls. To find out if the controller wants to process the key, the dialog manager code sends focused control a WM_GETDLGCODE message; if the control responds appropriately, the dialog manager returns FALSE , allowing DispatchMessage actually deliver it to the window procedure, otherwise the dialog manager does its own job.
Windows Forms basically just wraps the old native controls, so it should fit the Win32 event model. It implements the same approach to the dialog manager - therefore, by default, it does not allow you to see the Tab, Return, and cursor keys.
The recommended approach, if you want to process one of these keys, is to override the PreviewKeyDown and set the PreviewKeyDownEventArgs IsInputKey to true .
Mike Dimmick Mar 23 '15 at 16:24 2015-03-23 16:24
source share