DelphiFAQ Home Search:

The keyboard state - is Shift, Ctrl or Alt key pressed?

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

How can I reliably detect the present keyboard state - whether the Shift, Ctrl or Alt key are pressed?

Answer:

You can use GetKeyState() as shown in the other example on this site () or use GetKeyboardState() which works with an array of 256 bytes. This one will work on all Windows platforms while GetKeyState() had problems on Windows ME.

You can use this array also to update the state e.g. turn off a caps-lock. Use function SetKeyState() for this purpose.

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

function isCtrlDown : Boolean;
var
  State: TKeyboardState;
begin { isCtrlDown }
  GetKeyboardState(State);
  Result := ((State[VK_CONTROL] and 128)<>0);
end; { isCtrlDown }

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


Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: