2009. május 31., vasárnap

Shift, Ctrl, Alt Key checking


Problem/Question/Abstract:

How to detect if Ctrl, Alt or Shift key is pressed

Answer:

function CtrlDown: Boolean;
var
  State: TKeyboardState;
begin
  GetKeyboardState(State);
  Result := ((State[vk_Control] and 128) <> 0);
end;

function ShiftDown: Boolean;
var
  State: TKeyboardState;
begin
  GetKeyboardState(State);
  Result := ((State[vk_Shift] and 128) <> 0);
end;

function AltDown: Boolean;
var
  State: TKeyboardState;
begin
  GetKeyboardState(State);
  Result := ((State[vk_Menu] and 128) <> 0);
end;

The following example demonstrates checking if the Shift key is pressed during a Button Click.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ShiftDown then
    Form1.Caption := 'Shift'
  else
    Form1.Caption := 'No Shift';
end;

Nincsenek megjegyzések:

Megjegyzés küldése