delphi - `TActionToolBar` 的加速键不起作用

标签 delphi

我无法让 TActionToolBar 工作的加速键。

这就是我正在做的事情(可在 D2006、XE4 中重现):

  1. 选择新建 -> VCL 表单应用程序
  2. ActionManager1 添加到表单
  3. ActionManager1中添加新操作Action1,将操作标题设置为&Test
  4. ActionToolBar1 添加到表单
  5. 将项目添加到 ActionManager.ActionBars 并将 ActionManager.ActionBars[0].ActionBar 设置为 ActionToolBar1
  6. 将项目添加到 ActionManager.ActionBars[0].Items 并将 Action 设置为 Action1
  7. 设置 Action1.OnExecute 事件以显示消息
  8. 启动程序 --> 工具栏显示得很好并且可以通过鼠标工作
  9. 按 ALT+T --> 没有任何反应,但发出“叮”声

我缺少哪一步?

最佳答案

如现有的answer指出,操作工具栏不支持此功能。

我个人的观点是,这一点被忽视了。工具栏按钮经常显示图像而不是文本可能是这样做的原因之一(至少对我来说是这样)。然而,显然,工具栏按钮在显示标题时具有该功能,操作工具栏按钮也可以。

@Silver 在 comment 中指出操作栏能够找到加速项目。事实上,操作菜单使用该功能。相同的功能可以轻松集成到操作工具栏的 TCustomForm.IsShortCut 中,该工具栏已经迭代操作列表以查找可能的快捷方式目标。

我们可以重写该方法并自己完成。下面的示例优先考虑默认处理,因此分配的快捷键将抑制具有相同字符的键盘加速器,但这种逻辑很容易逆转。

function TForm1.IsShortCut(var Message: TWMKey): Boolean;
var
  Item: TActionClientItem;
  i: Integer;
begin
  Result := inherited IsShortCut(Message);
  if not Result and (KeyDataToShiftState(Message.KeyData) = [ssAlt]) then begin

    for i := 0 to ActionManager1.ActionBars.Count - 1 do begin
      if ActionManager1.ActionBars[i].ActionBar is TActionToolBar then begin
        Item := TActionToolBar(ActionManager1.ActionBars[i].ActionBar)
            .FindAccelItem(Message.CharCode);
        if Assigned(Item) and Item.ShowCaption and Assigned(Item.Action) 
            and Item.Action.Execute then begin
          Result := True;
          Break;
        end;
      end;
    end;

  end;
end;

关于delphi - `TActionToolBar` 的加速键不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57992263/

相关文章:

delphi - 如何在 cxGrid 中使用 ADT 字段中的子字段?

delphi - TFDTable 是否支持 SortOptions.NullsFirst?

delphi - 将两个字节合并为 WideChar

delphi - 调用过程 "delayed"的最佳方法是什么?

string - OleVariant 可用于 DLL 过程中的输出参数吗?

delphi - Visual C++ 是开始可视化编程的好选择吗?

java - 在java spring项目中使用delphi dll

delphi - 如何中断 Delphi 中的查找对话框?

android - 使用 Delphi for Android 显示 TMediaPlayer.Duration 和 TMediaPlayer.CurrentTime 的秒数

delphi - 在delphi中的计算字段上使用locate函数