Beranda > Delphi > How to check if a key is pressed

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.
Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: