To check if a specific key is pressed you can use the
GetKeyState
method.
type
TUtility = class
public
class function CheckKeyPressed(key: Integer): Boolean;
class function ShiftKeyPressed(): Boolean;
class function CtrlKeyPressed(): Boolean;
class function AltKeyPressed(): Boolean;
end
One method can check any key you want using virtual key codes:
// Helper methods to check various key states
class function TUtility.CheckKeyPressed(key: Integer): Boolean;
begin
//The return value of GetKeyState specifies the status of the specified virtual key, as follows:
//
//If the high-order bit is 1, the key is down; otherwise, it is up.
//If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled
//if it is turned on.
// The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any)
// on the keyboard will be on when the key is toggled, and off when the key is untoggled.
//
// Check high-order of state
Result := (GetKeyState(key) and $80) <> 0;
end;
...and specific methods for the most interesting keys can be made:
// Helper methods to check various key states
class function TUtility.ShiftKeyPressed(): Boolean;
begin
Result := CheckKeyPressed(VK_SHIFT);
end;
class function TUtility.CtrlKeyPressed(): Boolean;
begin
Result := CheckKeyPressed(VK_CONTROL);
end;
class function TUtility.AltKeyPressed(): Boolean;
begin
Result := CheckKeyPressed(VK_MENU);
end;