为什么左 ctrl 不触发 ssLeft?

Faj*_*iya 2 delphi events keyboard-events

我创建了一个非常简单的 VCL 应用程序。它只是一个带有 TMemo 的表格。我已经在 TMemo 上添加了 key up 事件。

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;
Run Code Online (Sandbox Code Playgroud)

即使我使用左CTRL 键 + X,似乎ssLeft永远无法检测到。为什么会出现这种情况?

Hea*_*are 6

正如这里所描述的:

TShift状态

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;
Run Code Online (Sandbox Code Playgroud)