我創建了一個非常簡單的 VCL 應用程式。它只是一個帶有 TMemo 的表格。我已經在 TMemo 上添加了按鍵事件。
procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = Ord('X')) and (Shift * [ssCtrl, ssLeft] = [ssCtrl, ssLeft]) then
begin
ShowMessage('hi');
end;
end;
即使我使用左CTRL 鍵 X,似乎ssLeft永遠無法檢測到。為什么會這樣?
uj5u.com熱心網友回復:
如此處所述:
移位狀態
ssLeft 不是鍵盤狀態,而是滑鼠狀態。IE。當您按下“X”鍵時,您正在檢查是否按下了滑鼠左鍵以及(任何)Ctrl 鍵。
為了檢查是否專門按下了左控制鍵,而不是(也)右控制鍵,您需要將其添加到您的測驗中:
USES WinAPI.Windows;
FUNCTION KeyPressed(VirtualKey : WORD) : BOOLEAN; INLINE;
BEGIN
Result:=(GetKeyState(VirtualKey) AND $80000000<>0)
END;
FUNCTION LeftCtrl : BOOLEAN; INLINE;
BEGIN
Result:=KeyPressed(VK_LCONTROL)
END;
FUNCTION RightCtrl : BOOLEAN; INLINE;
BEGIN
Result:=KeyPressed(VK_RCONTROL)
END;
procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = Ord('X')) and (Shift*[ssCtrl, ssShift, ssAlt] = [ssCtrl]) and LeftCtrl and not RightCtrl then
begin
ShowMessage('hi');
end;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453965.html
