0% found this document useful (0 votes)
16 views1 page

Arrow Keystroke in Controls

To receive keystroke messages for arrow keys in Delphi, a control must handle the CM_WANTSPECIALKEY message. Returning a non-zero value in the message's Result field for specific keys like the arrow keys will cause those key presses to be passed to the control's KeyPress method rather than using the default navigation behavior. This allows finer-grained control over which arrow and special keys the control wants to handle itself versus letting the IDE handle for navigation.

Uploaded by

ZeroCalls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Arrow Keystroke in Controls

To receive keystroke messages for arrow keys in Delphi, a control must handle the CM_WANTSPECIALKEY message. Returning a non-zero value in the message's Result field for specific keys like the arrow keys will cause those key presses to be passed to the control's KeyPress method rather than using the default navigation behavior. This allows finer-grained control over which arrow and special keys the control wants to handle itself versus letting the IDE handle for navigation.

Uploaded by

ZeroCalls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

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

Product:
Delphi all versions

Category:
Component Writing

Skill Level:

Search Keys:
delphi delphi3000 article borland vcl code-snippet
CM_WANTSPECIALKEY TWinControl
Uploader: Bernhard Angerer
Company: bluestep.com IT Consulting

Scoring:

Last Update:
02/29/2000

Times Scored:
2

Visits:
3265

Reference: Max Nilson

Question/Problem/Abstract:
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.

You might also like