«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» Help

How to check if a key is pressed

   

Use the Windows GetKeyState function to get data about the required key. GetKeyState can be passed either a virtual key code such as VK_CONTROL or an ascii value such as Ord('A').

The data returned by GetKeyState has its high bit set if the key is pressed, making the returned (signed) value negative. So to check if a key is down we simply check for a negative return value from GetKeyState. Here's a function that checks if the Control key is presssed:

function IsControlKeyPressed: Boolean;
begin
  Result := GetKeyState(VK_CONTROL) < 0;
end;
See also the IsKeyPressed routine in the Code Snippets Database.
Author: Peter Johnson
Contributor: Peter Johnson
Added: 2007-07-04
Last updated: 2013-10-12

« Return to contents »