繁体   English   中英

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

[英]How to detect Ctrl+Alt+x in the keypress event handler of a VCL TMemo control?

我创建了一个带有单个 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,但我只想知道为什么我的代码不起作用。

您应该改用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; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM