46 Votes

Delphi/Lazarus: Is the ALT, SHIFT or CTRL key pressed?

Tutorial by Stefan Trost | Last update on 2023-01-11 | Created on 2013-12-23

Sometimes, we want to check in a keyboard event, a mouse event or at an arbitrary other place in our program, if the user is just holding down one or more than one of the keys ALT, ALT GR, SHIFT or CTRL at the moment. To find out this, the ShiftState is helping us.

In the following, we would like to look at how to query and use the ShiftState in standard events of the keyboard or the mouse as well as how it works outside of the standard events. Finally, we also want to look at how we can use the ShiftState in conjunction with other keys to query key combinations:

ShiftState in Standard-Events

The easiest way to query the pressed modifier keys is to use a standard procedure in Delphi or Lazarus, which already provides us a variable of ShiftState. This is the case, for example, in the default events OnMouseDown (mouse button is pressed), OnMouseUp (mouse is released), OnMouseMove (mouse is moved), OnMouseWheel (mouse wheel is moved), OnKeyDown (key is pressed) or OnKeyUp (key is released).

Let's look at a short example:

procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if ssAlt         in Shift then ShowMessage('ALT');
  if ssShift       in Shift then ShowMessage('SHIFT');
  if ssCtrl        in Shift then ShowMessage('STRG');
  if ssAltGr       in Shift then ShowMessage('ALT GR');
  if ssLeft        in Shift then ShowMessage('Left Mouse Key');
  if ssRight       in Shift then ShowMessage('Right Mouse Key');
  if ssMiddle      in Shift then ShowMessage('Middle Mouse Key');
  if ssDoubleClick in Shift then ShowMessage('Double Click');
end;

Here, for example, we have set the OnKeyUp event for an edit field. Automatically, the variables Key and Shift are available from which we can retrieve which normal key is pressed (Key variable) and which special key was pressed during pressing the key (Shift variable).

Next to find out whether the user is holding the ALT, SHIFT, CTRL or ALT GR key, using Shift State it is also possible to get information about the mouse (is the right or left mouse button pressed or is there a double click, for example). In the sample code we check for some of these states and issue a message to the user that informs about the current state.

ShiftState in an own Procedure

When writing our own procedure or function, of course, Delphi or Lazarus does not automatically make the variable "Shift" available. Nevertheless, it is still possible to retrieve the current shift state.

procedure MyOwnProcedure;
var
  currentShiftState: TShiftState;
begin    
  currentShiftState := GetKeyShiftState;

  if ssShift in currentShiftState then ShowMessage('SHIFT');
end;

We just declare a variable of the type TShiftState. Let's call it currentShiftState as in our example code. With the function GetKeyShiftState, we can request the current shift state and store it in our variable. After that we can just work with it like we have done it in the first example.

ShiftState in Combination with other Keys

Of course, we can also combine the query of the Shift State with the query of other keys and so check for pressing key combinations. Let's have a look at another small example:

procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin    
  if (Shift = [ssCtrl]) and (key = ord('A')) then begin
    ShowMessage('CTRL + A was clicked');
  end;

  if (ssShift in Shift) and (ssAlt in Shift) then begin
    ShowMessage('The SHIFT and the ALT key are held')
  end;
end;

First, we check whether the user has pressed the keyboard shortcut CTRL + A by not only checking Shift for ssCTRL but also whether the A key was pressed at the same time by evaluating the Key variable. Since Key is of the type Word, we first have to convert 'A' into a numerical value for this (we could also directly check for 65).

Subsequently, we check whether both the SHIFT key as well as the ALT key are held at the same time.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.