views:

379

answers:

2

In my application I use tabs, my own component, like Google chrome sort of. Each tab reference an explorer component so it is basicly a tabbed browser/explorer. My problem is that I want to use Ctrl Tab and Ctrl Shift Tab to navigate tabs. Setting forms.KeyPreview will not help since the tab key is special key. How can I, in an easy way, add support for my navigation wish. I can modify component if needed. My component is based on TCustomControl if that helps.

Kind Regards Roy M Klever

+5  A: 

Tab, like the arrow keys, enter and escape are a special keys used in dialog navigation. When a control would like to receive those keys it has to indicate so by responding to WM_GETDLGCODE. Like this code below. Then you will receive a KeyDown event when Tab is pressed.

procedure WMGetDlgCode(var Msg: TWMGetDlgCode); message WM_GETDLGCODE;

procedure TYourControl.WMGetDlgCode(var Msg: TWMGetDlgCode);
begin
 inherited;
 Msg.Result := Msg.Result or DLGC_WANTTAB;
end;

Also see here and here.

PS: And make sure your control has focus or you will receive nothing at all (if CanFocus then SetFocus; in MouseDown).

Lars Truijens
Well this helped me getting on right track... I had some code in my explorer that stopped the keys from working under some conditions. Now I have it working. Thanks!
Roy M Klever
+3  A: 

You can manage the CM_DIALOGKEY message in your component to intercept the Ctrl + Tab and Ctrl + Shift + Tab.

procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;

check this sample

procedure TYourComponent.CMDialogKey(var Message: TCMDialogKey);
begin
  if (Focused) and (Message.CharCode = VK_TAB) and (GetKeyState(VK_CONTROL) < 0) then
  begin
   if GetKeyState(VK_SHIFT) then
    GoBackwardPage()//you must write this method
   else
    GoForwardPage()//you must write this method
    Message.Result := 1;
  end
   else
    inherited;
end;
RRUZ
Almost how I do it now :) Thanks!
Roy M Klever