How to disable TAction.Shortcut or TMenuItem.Shortcut?
I'm developing a Word addin, and s开发者_开发问答omehow the shortcuts defined in TAction.ShortCut are always trigged more than one time, and this is tricky to me and hard to solve, so I resort to TForm.OnKeyDown event and cleared all TAction.ShortCut properties, this approach works well, except that the shortcuts are not shown on the corresponding menu items, but I want them to be displayed on those menu items.
So I come up this idea: Set values for TMenuItem.Shortcut so that the program can show the shortcut hint to the end user, and does not allow VCL to handle these shortcuts, instead, handle them in TForm.OnKeyDown. So my question is how to disable TAction.Shortcut or TMenuItem.Shortcut? Thank you in advance.
For a start, you have an Enabled
property on both TAction
and TMenuItem
. Just set it to False
.
Next, one of the possible causes of your event being triggered more than once is that you may be using Application.ProcessMessages
; or at least a badly written component that you're using is doing so. One should be very wary of using that Delphi feature because it can cause 're-entrant' code (unintentional recursion).
The root cause of your problem is the events being triggered more than one time. You could try to workaround this problem offcourse but I would suggest to:
- Place a breakpoint in your eventhandler.
- Copy the Call Stack's content
[CTRL+ALT+S]
to whatever editor you like for every time you hit the breakpoint. - Start brainstorming as to why the calls lead to hitting the event multiple times.
- Fix your code if it is your code to fix.
Hacker way (usually not recommended): copy unit that contain TAction in separate folder, modify source of TAction that makes ShortCut method do nothing. Put this folder to search path as first item. rebuild your app.
I use this technique to fix bugs in VCL, but after installing Delphi patches you should not forget to update 'hacked' version of modified units.
精彩评论