Delphi: How do I stop TAction shortcut keys autorepeating?

烂漫一生 提交于 2019-12-07 16:12:59

问题


I'm using a Delphi TActionList, with Shortcut Keys for some actions.

I want to prevent certain actions from being triggered multiple times by keyboard auto-repeat, but I do not want to affect auto-repeat operation globally. What's the best way of doing this?

Clarification: I still need to handle multiple fast keypresses - it's only the keypresses generated by auto-repeat that I want to ignore.


回答1:


Intercept the WM_KEYDOWN messages, and look at bit 30 to see if it is auto-repeating. If it is, just don't pass on the message as usual and it will not be seen.

You may need to enable form key-preview to make this work.




回答2:


You can drop TTimer, set TTimer.Interval to value you want (1000 = 1sec), then in TActionList do something like:

procedure TfrmMain.ActionList1Execute(Action: TBasicAction; var Handled: Boolean);
begin
  if Timer1.Enabled then 
  begin
    Handled := True;
    Exit;
  end;

  Handled := false; 
  Timer1.Enabled := true;     
end;

Also don't forget to disable timer in Timer.OnTimer.




回答3:


You can save the last time an action is used and ignore it if the time in between is too short. For a single action you can do like:

procedure TForm.FormCreate(const Sender: TObject);
begin
  // ...

  FLastActionTime := Now; // 
end;

proceudure TForm.Action1Execute(const Sender: TObject);
const
  cThreshold = 1/(24*60*60*10); // 0.1 sec
begin
  if Now-FLastActionTime<cThreshold then
    Exit; // Ignore two actions within 0.1 sec
  FLastActionTime := Now;
end;

You can combine this with the solution of dmajkic to get a more generic aproach. And if you are really ambitious, you can create a new version of TAction/TActionlist.



来源:https://stackoverflow.com/questions/503538/delphi-how-do-i-stop-taction-shortcut-keys-autorepeating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!