如何在 VCL TMemo 控件的按键事件处理程序中检测 Ctrl+Alt+x?

发布于 2025-01-16 08:54:52 字数 913 浏览 0 评论 0原文

我创建了一个带有单个 TMemo 控件的 Delphi VCL 应用程序,这是我的代码。 我用它来检测 Ctrl+somekey。例如,当我按下 Ctrl+x 时,会弹出警报 ctrl 并且 Ctrl+x 的效果(剪切)被取消。

function IsKeyDown(Key: Integer): Boolean;
begin
  Result := (GetAsyncKeyState(Key) and (1 shl 15) > 0);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if IsKeyDown(VK_CONTROL) then
  begin
    ShowMessage('ctrl');
    Key := #0;
  end;

end;

然而,当我把它稍微改变一下时:

function IsKeyDown(Key: Integer): Boolean;
begin
  Result := (GetAsyncKeyState(Key) and (1 shl 15) > 0);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if IsKeyDown(VK_CONTROL) and IsKeyDown(VK_MENU) then
  begin
    ShowMessage('ctrl+alt');
    Key := #0;
  end;

end;

它不再起作用了。我需要的是检测像 Ctrl+Alt+f 这样的组合。我知道我可以使用 TActionList,但我只想知道为什么我的代码不起作用。

I've created a Delphi VCL app with a single TMemo control, and this is the code I have.
I use it to detect Ctrl+somekey. For example, when I press Ctrl+x, it pops up the alert ctrl and the Ctrl+x's effect (cut) was cancelled.

function IsKeyDown(Key: Integer): Boolean;
begin
  Result := (GetAsyncKeyState(Key) and (1 shl 15) > 0);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if IsKeyDown(VK_CONTROL) then
  begin
    ShowMessage('ctrl');
    Key := #0;
  end;

end;

However, when I changed it a little bit to this:

function IsKeyDown(Key: Integer): Boolean;
begin
  Result := (GetAsyncKeyState(Key) and (1 shl 15) > 0);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if IsKeyDown(VK_CONTROL) and IsKeyDown(VK_MENU) then
  begin
    ShowMessage('ctrl+alt');
    Key := #0;
  end;

end;

It doesn't work anymore. What I need is to detect combinations like Ctrl+Alt+f. I know I can use a TActionList, but I just want to know why my code doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤仙姿 2025-01-23 08:54:52

您应该使用 OnKeyDown 来代替,它为您提供键值和修饰键。我在下面的代码中演示了如何捕获一个修饰键和多个修饰键。

uses
  { Needed for virtual key codes in recent Delphi versions. }
  System.UITypes;  
                 

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word; 
    Shift: TShiftState);
begin
  if (Key = vkX) and ([ssCtrl] = Shift) then
  begin
    Key := 0;
    ShowMessage('Got Ctrl+X');
  end
  else if (Key = vkZ) and ([ssCtrl, ssAlt] = Shift) then
  begin
    Key := 0;
    ShowMessage('Got Ctrl+Alt+Z');
  end;
end; 

You should use OnKeyDown instead, which provides you with both the key value and the modifier keys. I've demonstrated how to capture both one modifier key and multiple modifier keys in the code below.

uses
  { Needed for virtual key codes in recent Delphi versions. }
  System.UITypes;  
                 

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word; 
    Shift: TShiftState);
begin
  if (Key = vkX) and ([ssCtrl] = Shift) then
  begin
    Key := 0;
    ShowMessage('Got Ctrl+X');
  end
  else if (Key = vkZ) and ([ssCtrl, ssAlt] = Shift) then
  begin
    Key := 0;
    ShowMessage('Got Ctrl+Alt+Z');
  end;
end; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文