gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:36:22 25 4
gpt4 key购买 nike

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

TActionListTMainMenuTMenuBar 都具有分配快捷方式的属性。

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

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

但是,如果用户继续按住这两个键,也会创建多个选项卡。

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

添加新选项卡只是一个示例。我正在使用多个快捷方式,但我只想触发一次。

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

我尝试了计时器/等待特定的时间。但如果用户想要快速执行 2 个热键,就会出现问题。

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

最佳答案

下面是如何使用计时器解决此问题的示例,以便它不会阻止用户连续使用多个不同的操作。使用相同操作的速度取决于您的配置,但受系统按键自动重复延迟间隔的限制。请参阅代码注释。

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 毫秒。并且不要忘记将 ActionCooldownValue 设置为大于系统键自动重复延迟间隔

关于delphi - 仅触发一次热键/快捷键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26890772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com