仅触发一次热键/快捷方式事件

克里斯·B

我正在开发一个Delphi XE7多平台应用程序,并希望使用一些热键/快捷方式。

TActionListTMainMenu并且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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

仅触发一次热键/快捷事件

来自分类Dev

在C#中仅拆分一次的快捷方式?

来自分类Dev

“ onclick”事件仅触发一次

来自分类Dev

拖动事件仅触发一次

来自分类Dev

jQuery .change()事件仅触发一次

来自分类Dev

类似componentDidUpdate的事件,但仅触发一次

来自分类Dev

PropertyChanged事件触发器仅发生一次

来自分类Dev

更改事件处理程序仅触发一次,jQuery

来自分类Dev

window.resize事件仅触发一次

来自分类Dev

仅触发一次jQuery滚动事件

来自分类Dev

仅触发一次jQuery mousemove事件

来自分类Dev

AngularJS-事件仅触发一次

来自分类Dev

iframe中的onload事件仅触发一次

来自分类Dev

jQuery Bootstrap Datepicker Click事件仅触发一次

来自分类Dev

仅触发一次的Symfony身份验证事件?

来自分类Dev

更改事件处理程序仅触发一次,jQuery

来自分类Dev

仅触发一次jQuery滚动事件

来自分类Dev

jQuery Mobile vclick事件仅触发一次

来自分类Dev

jQuery animate仅通过keypress事件触发一次

来自分类Dev

Word加载项DocumentBeforeClose事件仅触发一次

来自分类Dev

Javascript onkeydown事件仅触发一次

来自分类Dev

onclick输入文件,仅触发一次onChange事件

来自分类Dev

Kendo Tabstrip单击事件仅触发一次

来自分类Dev

单击事件仅触发一次JQuery / bPopup

来自分类Dev

如何使由键触发的事件仅发生一次?

来自分类Dev

Xamarin AnimationEnd事件仅触发一次

来自分类Dev

jQuery 单击事件仅触发一次

来自分类Dev

Virtual Machine Manager希望一次又一次地禁止Wayland上的快捷方式

来自分类Dev

在Word中创建热键的快捷方式