tags:

views:

210

answers:

3

Hi,

I'm developing a Word addin, and somehow 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.

+1  A: 

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).

Craig Young
Hi Craig, thank you. Since I'm developing my Word addin based on a complex framework, so it's hard for me to find the source of the problem and why I compromised.
Edwin
Hi Edwin, we experienced a similar problem to you; and also opted for the same kind of workaround. We know our problem is due a 3rd party component suite that calls `Application.ProcessMessages`. This particular component suite is in general poorly written (i.e. this is not the only problem); as such we have decided to phase the component suite out of our systems. (We'll probably leave the 'workaround' in plaec though, because it really should not be possible to fire a UI event while the same one is busy processing. - No matter what the cause!)
Craig Young
Hi Craig, thank you again! I finally found that this (unwanted Application.ProcessMessages calls) is exactly what caused the problem!
Edwin
A: 

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.
Lieven
Hi Lieven, Thank you. You are right, but since I'm base my development on a very complex Office addin development framework it's too difficult for me to solve this root cause, that's why I compromised. <BR/>Maybe using custom draw of the menu items will work, anybody has a sample code this? I mean showing both the menu item text and the shortcut hint on the right side of it.
Edwin
A: 

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.

kuaw26
Thank you. I have looked at the source but couldn't find where to modify the source...
Edwin
This is absolutely wrong. You should *never* modify the VCL source, because you can cause breakages in other things you're not aware of the effect on. Almost 100% of the time, there are better ways of doing things.
Ken White
Ken, it seems that you didn't read my answer carefully. I write that it is a HACKER way and NOT RECOMMENDED, but in some critical situations it is the only way to solve the problem. As a superior example - patches that Andreas Hausladen (http://andy.jgknet.de/) made for Delphi IDE and VCL library.VCL is writen by people not by god :)
kuaw26