Delphi:如何将向上箭头键盘快捷键分配给操作/菜单项,并使其保持实际状态以导航列表控件(ListBox/VTV)?

发布于 2024-08-19 20:15:05 字数 76 浏览 5 评论 0原文

请帮助我:如何为操作或菜单项分配向上箭头键盘快捷键,并同时保持其实际导航列表控件(例如列表框/虚拟树视图/其他)?

谢谢!

Please assist me: How to assign an up arrow keyboard shortcut to action or menu item, and keep it actual for navigating the list control (e.g. ListBox/Virtual Treeview/other) at the same time?

Thanks!

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

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

发布评论

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

评论(2

得不到的就毁灭 2024-08-26 20:15:06

您评论:

Winamp 播放器怎么样?它具有相应地分配给向上箭头键和向下箭头键的音量增大/音量减小功能。好吧,如果这在 Delphi 中不可能,那么...

但这当然是可能的,只是这样做不是一个好主意,并违反 Windows 用户体验交互指南。

但如果您决定实施它,请按以下步骤操作。在包含操作组件的表单类中重写以下方法:

function IsShortCut(var Message: TWMKey): Boolean; override;

并且在其中您可以防止向上和向下键触发它们是快捷方式的操作:

function TWeirdForm.IsShortCut(var Message: TWMKey): Boolean;
begin
  if (Message.CharCode in [VK_UP, VK_DOWN])
    // insert test whether message needs to go to the focused control instead
    and (...)
  then begin
    // insert calls to code that should be executed instead
    Result := False;
    exit;
  end;
  inherited;
end;

请注意,您也应该测试正确的切换状态,并检查您的代码不会破坏用户期望的任何其他窗口行为,例如使用箭头键移动窗口。

You comment:

And how about the Winamp player? It has Volume Up/Volume Down features assigned to the up arrow key and down arrow key correspondingly.. Okay, if that impossible in Delphi, then ...

but it certainly is possible, it just isn't a good idea to do it, and against the Windows User Experience Interaction Guidelines.

But if you're set on implementing it, here's how. Override the following method in your form class that contains the action components:

function IsShortCut(var Message: TWMKey): Boolean; override;

and in it you can prevent the Up and Down key from triggering the actions they are shortcuts for:

function TWeirdForm.IsShortCut(var Message: TWMKey): Boolean;
begin
  if (Message.CharCode in [VK_UP, VK_DOWN])
    // insert test whether message needs to go to the focused control instead
    and (...)
  then begin
    // insert calls to code that should be executed instead
    Result := False;
    exit;
  end;
  inherited;
end;

Note that you should test for the correct shift state too, and check that your code doesn't break any other window behaviour users expect, like moving of the window with the arrow keys.

故事还在继续 2024-08-26 20:15:06

在表单属性上设置 KeyPreview := true

然后在表单的 KeyUp 事件上写入事件来检查是否按下了向上键并使其调用菜单项(在本例中菜单项称为 Action1):

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_UP) and (ActiveControl = ListBox1)then
    Action11.Click;
end;

procedure TForm1.Action11Click(Sender: TObject);
begin
  if ListBox1.ItemIndex >=0  then
    ShowMessage(ListBox1.Items[ListBox1.ItemIndex]);
end;

如果您需要执行 Action1,即使当前控件不是列表框,请删除 IF 语句的 and 部分

On the form properties set KeyPreview := true

then on KeyUp event of the form write event to check if you Up key is pressed and make it call the menu item (on this case menu item called Action1):

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_UP) and (ActiveControl = ListBox1)then
    Action11.Click;
end;

procedure TForm1.Action11Click(Sender: TObject);
begin
  if ListBox1.ItemIndex >=0  then
    ShowMessage(ListBox1.Items[ListBox1.ItemIndex]);
end;

If you need the Action1 to be executed even if they Current Control isn't the listbox, remove the and part of the IF statement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文