我正在开发一个Delphi XE7多平台应用程序,并希望使用一些热键/快捷方式。
TActionList
,TMainMenu
并且TMenuBar
都具有分配快捷方式的属性。
我正在使用快捷方式在上添加新TTabItem
的内容TTabControl
。那个ShortCut是Ctrl+ T。
因此,如果用户按Ctrl+,T则在其上添加了新标签TTabControl
-正常工作。
但是,如果用户按住这两个键,则会同时创建多个选项卡。
只要用户按住这些键,就会触发快捷方式事件。
添加新标签只是一个例子。我使用的多个快捷方式只想触发一次。
有什么方法只能触发一次快捷事件吗?
我尝试使用计时器/等待特定的时间。但这会导致问题,如果用户想要快速执行2个热键。
感谢您的阅读,我感谢所有帮助。
这是一个如何使用Timer解决此问题的示例,以便它确实阻止用户连续使用多个不同的动作。使用同一操作的速度取决于您的配置,但受系统密钥自动重复延迟间隔的限制。参见代码注释。
const
//Interval after which the action can be fired again
//This needs to be greater than system key autorepeat delay interval othervise
//action event will get fired twice
ActionCooldownValue = 100;
implementation
...
procedure TForm2.MyActionExecute(Sender: TObject);
begin
//Your action code goes here
I := I+1;
Form2.Caption := IntToStr(I);
//Set action tag to desired cooldown interval (ms before action can be used again )
TAction(Sender).Tag := ActionCooldownValue;
end;
procedure TForm2.ActionList1Execute(Action: TBasicAction; var Handled: Boolean);
begin
//Check to see if ActionTag is 0 which means that action can be executed
//Action tag serves for storing the cooldown value
if Action.Tag = 0 then
begin
//Set handled to False so that OnExecute event for specific action will fire
Handled := False;
end
else
begin
//Reset coldown value. This means that user must wait athleast so many
//milliseconds after releasing the action key combination
Action.Tag := ActionCooldownValue;
//Set handled to True to prevent OnExecute event for specific action to fire
Handled := True;
end;
end;
procedure TForm2.Timer1Timer(Sender: TObject);
var Action: TContainedAction;
begin
//Itearate through all actions in the action list
for Action in ActionList1 do
begin
//Check to see if our cooldown value is larger than zero
if Action.Tag > 0 then
//If it is reduce it by one
Action.Tag := Action.Tag-1;
end;
end;
注意:将计时器间隔设置为1 ms。并且不要忘记将ActionCooldownValue设置为大于系统密钥自动重复延迟间隔
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句