如何在 VCL TMemo 控件的按键事件处理程序中检测 Ctrl+Alt+x?
我创建了一个带有单个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
OnKeyDown
来代替,它为您提供键值和修饰键。我在下面的代码中演示了如何捕获一个修饰键和多个修饰键。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.