我无法让 TActionToolBar
工作的加速键。
这就是我正在做的事情(可在 D2006、XE4 中重现):
- 选择新建 -> VCL 表单应用程序
- 将
ActionManager1
添加到表单 - 在
ActionManager1
中添加新操作Action1
,将操作标题设置为&Test
- 将
ActionToolBar1
添加到表单 - 将项目添加到
ActionManager.ActionBars
并将ActionManager.ActionBars[0].ActionBar
设置为ActionToolBar1
- 将项目添加到
ActionManager.ActionBars[0].Items
并将Action
设置为Action1
- 设置
Action1.OnExecute
事件以显示消息 - 启动程序 --> 工具栏显示得很好并且可以通过鼠标工作
- 按 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/