Главная страница | назад





Article #17091: Shift, Ctrl, Alt key checking

 Question and Answer Database

FAQ2091D.txt   Shift, Ctrl, Alt key checking
Category   :VCL
Platform    :All
Product    :All 32 bit  

Question:
How can I determine if the Shift, Alt, or Ctrl key is 
depressed at any given time?

Answer:
The following example demonstrates checking if the Shift key is
pressed during a menu item selection. The example presents 
functions for testing the status of the Alt, Ctrl, and 
shift keys.

Example:

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;

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


7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99