delphi - 为什么左 ctrl 不触发 ssLeft?

标签 delphi events keyboard-events

我创建了一个非常简单的 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。为什么会这样?

最佳答案

如这里所述:

TShiftState

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;

关于delphi - 为什么左 ctrl 不触发 ssLeft?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71675885/

相关文章:

javascript - 使用 D3 画笔进行细粒度事件处理

c# - 在 C# : synchronisation issues 中记住和重放击键

c++ - SDL 2.0 按键重复和延迟

c++ - C 虚拟键盘输入有选择地工作

delphi - 当我的应用程序没有焦点时,如何捕获键盘状态?

delphi - 创建功能区样式的应用程序

delphi - 在 Delphi 中搜索字符串数组

php - 如何在 Delphi 中转换 HTML Webform 的参数以与 TIdHTTP 一起使用?

VB.NET - 如何将大量事件添加到单个句柄?

java - Java 中的事件序列