In the Windows application, many have shortcut key function, this Delphi also has, is a button above has one such as cut (&x), this time cut the shortcut key is alt+x, this function sometimes is very useful, recently, the company has colleagues, A lot of use of Speedbutton, and then use the whole method of the shortcut keys, can not be used, so ask me, this is God horse problem, in fact, it is not useless, but in some cases can not be used, such as the use of Pagecontrol, such as a class of controls, Then Tabsheet and then put a panel, and then put Speedbutton on the panel, this time, the use of shortcut keys will lead to response problems, such as TabSheet1 directly there is a Speedbutton on the TabSheet1, TabSheet2 on the Speedbutton on the panel, two tabsheet Speedbutton shortcut keys are alt+a, at this time, according to the truth, should be shortcut keys, which tabsheet is the active state, You should respond to the shortcut key event for the Speedbutton on the Tabsheet, but in fact, once the Speedbutton page of the panel has been activated, it will always respond to the Speedbutton shortcut key activation for that page. And it can lead to confusion.
In response to this question, what method, nature can not blindly go to the whole, Delphi is better than the source of the VCL is all with, so go directly to the VCL to find the answer on the line, through tracking found alt+x this kind of shortcut key mode is actually the response of the Delphi CM_ Dialogchar this message, and then look at the implementation in Twincontrol
Procedure Twincontrol.cmdialogchar (var message:tcmdialogchar);
Begin
Broadcast (Message);
End
It is known that he will broadcast this quick message to the global, all the controls will get this message, at this time who first obtained, interception processing, the message will no longer continue. And we'll see how this message is handled by Speedbutton.
procedureTspeedbutton.cmdialogchar (varMessage:tcmdialogchar);begin withMessage Do ifIsaccel (CharCode, Caption) andEnabled andVisible and(Parent<>Nil) andParent.showing Then beginClick; Result:=1; End Else inherited;End;
The Isaccel function, in fact, is based on the caption to determine whether and shortcut keys match, if matched, and enabled and visible, and the parent is visible, then will trigger, so the root cause of the problem found, is the parent visible, Because the parent on the Tabsheet is always visible, this triggers, but the parent's tabsheet is actually hidden, so it causes this mess. Now that the problem is found, for this message
Process to intercept the processing is OK. The implementation process is as follows:
Tspeedbutton =class(Buttons.tspeedbutton)protected procedureCmdialogchar (varMessage:tcmdialogchar);messageCm_dialogchar; End;procedureTspeedbutton.cmdialogchar (varMessage:tcmdialogchar);varP:twincontrol; Candlgchar:boolean;beginCandlgchar:=False; P:=Parent; whileP <>Nil Do beginCandlgchar:=iswindowvisible (P.handle); if notCandlgchar ThenBreak ; P:=p.parent; End; ifCandlgchar Then withMessage Do ifIsaccel (CharCode, Caption) andEnabled andVisible and(Parent<>Nil) andIsWindowVisible (Parent.handle) andParent.showing Then beginClick; Result:=1; End Else inherited;End;
To use again, it triggers normal