2010. június 22., kedd

How to do a SHIFT TAB through code


Problem/Question/Abstract:

I use SendMessage(EditHandle, WM_KEYDOWN, VK_TAB, 0) to mimic pressing TAB key, but how about SHIFT-TAB? I know I can use WM_NEXTDLGCTL, but that is exactly what I try to avoid.

Answer:

You can do both by sending the WM_KEYDOWN message to the control or generate the keyboard event through the keybd_event function. See the example below for details:

{ ... }
var
  XKeyState, XNewKeyState: TKeyboardState;
begin
  try
    {set shift key down}
    GetKeyboardState(XKeyState);
    XNewKeyState := XKeyState;
    XNewKeyState[VK_SHIFT] := $81;
    SetKeyboardState(XNewKeyState);
    {post tab key down message}
    PostMessage(YourComponent.Handle, WM_KEYDOWN, VK_TAB, 0);
    Application.ProcessMessages;
  finally
    {return old keyboard state back}
    SetKeyboardState(XKeyState);
  end;
end;

or you use

{ ... }
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_TAB, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
end;

Nincsenek megjegyzések:

Megjegyzés küldése