Mirror

How to direct all keyboard events to a specific window (Views: 101)


Problem/Question/Abstract:

SetCapture directs all mouse events to a specific window. Is there a similar API for the keyboard? Can this be accomplished without using hooks? I want to track what the user presses on the keyboard and mouse and allow or reject the events.

Answer:

Look up TApplication.OnMessage and study the example given, then look at the example below:


procedure TMainForm.AppMessage(var Msg: TMsg; var Handled: Boolean);
const
  BLOCKEDKEYS = [VK_TAB, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_SPACE];
begin
  if (Msg.message = WM_KEYDOWN) then
  begin
    if (Msg.wParam in BLOCKEDKEYS) then
    begin
      Handled := True;
    end;
  end;
end;

<< Back to main page