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

时间:2014-11-12 15:38:59

标签: delphi firemonkey hotkeys shortcuts

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

TActionListTMainMenuTMenuBar都具有指定快捷方式的属性。

我使用快捷方式在TTabItem上添加新的TTabControl。 ShortCut是 Ctrl + T

因此,如果用户按下 Ctrl + T ,则会在所述TTabControl上添加一个新选项卡 - 正常工作。

但是,如果用户持续按住这两个键,也会创建多个标签。

只要用户持续按住这些键,就会触发快捷方式事件。

添加新标签只是一个例子。我正在使用多个快捷方式,我只想触发一次。

有没有办法只触发一次快捷方式事件?

我试过计时器/等待一段特定的时间。但如果用户想要快速执行2个热键,则会导致问题。

感谢阅读,感谢所有人的帮助。

1 个答案:

答案 0 :(得分:0)

以下是如何使用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设置为大于系统密钥自动重复延迟间隔