Mega Code Archive

 
Categories / Delphi / Examples
 

How come my control does not get keystroke messages for the arrow keys

Title: How come my control does not get keystroke messages for the arrow keys? Question: To receive keystroke messages for arrow keys you have to handle the WM_GETDLGCODE messages. In the messages handler return DLGC_WANTARROWS. If you do not handle WM_GETDLGCODE Windows intercepts the arrow keys and uses them to move among controls. Answer: To receive keystroke messages for arrow keys you have to handle the CM_WANTSPECIALKEY messages. The CM_WANTSPECIALKEY allows a much more 'fine grained' way of deciding if you require a special key than responding to the WM_GETDLGCODE messages. The control message CM_WANTSPECIALKEY is sent to a control whenever a special key is being handled. These special keys are VK_TAB, VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_RETURN, VK_EXECUTE, VK_ESCAPE and VK_CANCEL. If the message result is non zero then the key is passed onto the KeyPress method for you to handle, otherwise it is passed onto the controls parent form for Delphi's standard navigation handling. The Delphi standard navigation is where Delphi handles the Tab, Shift-Tab and arrow key motion between controls, without needing to use the default Windows dialog manager at all! A simple example: type TMyComponent = class(TWinControl) ... protected procedure CMWantSpecialKey(var Message: TCMWantSpecialKey); message CM_WANTSPECIALKEY; ... end; procedure TMyComponent.CMWantSpecialKey (var Message: TCMWantSpecialKey); begin inherited; // We want to handle the left arrow ourselves if Message.CharCode = VK_LEFT then Message.Result := 1; end; The 'fine grained' I refered to above comes from this ability to examine a specific keypress and decide if you need to handle the key your self, or allow it to continue on into Delphi's handler. If you had a single control with three images, for example, you could allow the left and right arrows to more back and forth between them, and also let the user to move onto the next tab stop from the last image on your control, by allowing Delphi to handle the keypress instead of your internal control handling.