一般可在 Form 的 FormKeyDown 事件(even)下,透過 Key 來判斷
EX:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key = VK_Shift) then ...
// ----- or ----
case key of
VK_ESCAPE:
begin
...
end;
VK_SHIFT:
begin
...
end;
end;
end;
若是在其他的 procedure, function 裡頭,要檢查是否按下 shift 時,該怎麼作呢?
可使用
GetKeyState(nVirtKey: Integer): Smallint;
GetAsyncKeyState(vKey: Integer): Smallint;
來判斷指定的按鍵是否有按下
EX:
if GetAsyncKeyState(VK_SHIFT) <> 0 then
ShowMessage('有按下 Shift 鍵')
else
ShowMessage('沒有按');
註記:
其中 GetKeyState 是以 Toggle 的形式來判斷,按一次會顯示為 1,再按一次才會回復為 0;
而 GetAsyncKeyState則是以當下是否按著來判斷。
沒有留言:
張貼留言