This is something i use in the OnTimer event of a TTimer ::Alternatively,,, and I would like to be able to do this also ... How do I write my own keyboard event handler
[pascal]
type
TVirtualKeyCode = Integer;
function IsKeyDown(c : Char): Boolean; overload;
function IsKeyDown(vk : TVirtualKeyCode): Boolean; overload;
implementation
function IsKeyDown(c : Char) : Boolean;
var
vk : Integer;
begin
// '$FF' filters out translators like Shift, Ctrl, Alt
vk:=VkKeyScan(c) and $FF;
if vk<>$FF then
Result:=(GetAsyncKeyState(vk)<0)
else Result:=False;
end;
// IsKeyDown
//
function IsKeyDown(vk : TVirtualKeyCode) : Boolean;
begin
Result:=(GetAsyncKeyState(vk)<>0);
end;
[/pascal]
To use the Virtual Key Codes you will need to add 'Windows' to your uses list.
You use them like ::
If IsKeyDown(VK_UP) Then
Begin
// Move forwards
End;
Or ::
If IsKeyDown('U') Then
Begin
// Do something
End;
Bookmarks