Author: Misha Moellner
How to detect if Ctrl, Alt or Shift key is pressed
Answer:
1 function CtrlDown: Boolean;
2 var3 State: TKeyboardState;
4 begin5 GetKeyboardState(State);
6 Result := ((State[vk_Control] and 128) <> 0);
7 end;
8 9 function ShiftDown: Boolean;
10 var11 State: TKeyboardState;
12 begin13 GetKeyboardState(State);
14 Result := ((State[vk_Shift] and 128) <> 0);
15 end;
16 17 function AltDown: Boolean;
18 var19 State: TKeyboardState;
20 begin21 GetKeyboardState(State);
22 Result := ((State[vk_Menu] and 128) <> 0);
23 end;
24 25 The following example demonstrates checking if the Shift key is pressed during a
26 Button Click.
27 28 procedure TForm1.Button1Click(Sender: TObject);
29 begin30 if ShiftDown then31 Form1.Caption := 'Shift'
32 else33 Form1.Caption := 'No Shift';
34 end;