Delphi: How to use TShiftState type variable?

32,051

Solution 1

IIUC you want the empty set []:

Something([ssShift]); // X
Something([ssCtrl]); // Y
Something([]); // Z

Regarding your update:

procedure Something(keyState : TShiftState);
begin
  if ssShift in KeyState then // KeyState contains ssShift (and maybe more)
    X;
  if ssCtrl in KeyState then // KeyState contains ssCtrl (and maybe more)
    Y;
  if [ssShift, ssCtrl] * KeyState = [] then // KeyState contains neither ssShift nor ssCtrl
    Z;
end;

If you are only interested in ssShift and ssCtrl, and the other values (ssAlt, ssLeft, ...) don't matter, you can mask the latter ones out:

procedure Something(keyState : TShiftState);
var
  MaskedKeyState : TShiftState
begin
  MaskedKeyState := KeyState * [ssShift, ssCtrl];
  if ssShift in MaskedKeyState then // MaskedKeyState contains ssShift
    X;
  if ssCtrl in MaskedKeyState then // MaskedKeyState contains ssCtrl
    Y;
  if MaskedKeyState = [] then // MaskedKeyState contains neither ssShift nor ssCtrl
    Z;
end;

Solution 2

if ssShift in keyState then
  ShowMessage('1')
else if ssCtrl in keyState then
  ShowMessage('2')
else
  ShowMessage('3')

try this

Share:
32,051
Himadri
Author by

Himadri

I am a freelancer working on PHP and WordPress development. I was working as an assistant professor in Uka Tarsadia University, Gujarat, India. My area of interest is Natural Language Processing, Open Source Technologies, PHP, CMS etc. #SOreadytohelp

Updated on February 21, 2020

Comments

  • Himadri
    Himadri over 4 years

    I am developing a Delphi application.
    On TImage.MouseDown event I want to do X task if shift key is pressed, Y task if control key is pressed and Z task if any of them is not pressed. For that I am using TShiftState variable. Now I have a function in which I have to pass this variable as parameter.

    procedure Something(keyState : TShiftState);
    

    Now In this function what I should right to check the state of key?

    if KeyState <> ssShift then begin
    
    end;
    

    The above code shows error.
    Thanks.

  • Himadri
    Himadri almost 14 years
    Thanks... You provide all I need.