Mega Code Archive

 
Categories / Delphi / VCL
 

VCL for AppWide EnableDisable Remapping of Enter to Tab Key

Title: VCL for AppWide Enable/Disable Remapping of Enter to Tab Key Question: Component to GLOBALLY Enable/Disable Enter Key mapping as Tab Key. The effect is application wide. (Just plonk on from and call Enable) Both the Keyboard and Keypad Enter keys may be set individually. They both can be set to act as either ENTER or TAB keys. Certain components such as TMemo etc. need the Enter key, so a list of components to EXCLUDE remapping is included. Properties KeyboardEnterType : TEnterAsTabKeyValue (kyTab or kyEnter) KeypadEnterType : TEnterAsTabKeyValue (kyTab or kyEnter) ExcludeComponents : TStringList (eg Tmemo etc) Methods Enable Enables GLOBAL remapping of Enter Keys Disable Disables GLOBAL remapping of Enter Keys Many thanks to : Odilon Nelson Grisi (for the Application.OnMessage idea) Igor Siticov (for the Keyboard - Keypad differentiation code) Answer: unit EnterTab; // ================================================================== // Mike Heydon - mheydon@eoh.co.za // Sep 2000 // // Component to GLOBALLY Enable/Disable Enter Key mapping as Tab Key. // The effect is application wide. // // 23/09/2000 - Modified to allow a list of components that // do not allow ENTER - TAB ramapping // // Both the Keyboard and Keypad Enter keys may be set individually. // They both can be set to act as either ENTER or TAB keys // // Properties // KeyboardEnterType : TEnterAsTabKeyValue (kyTab or kyEnter) // KeypadEnterType : TEnterAsTabKeyValue (kyTab or kyEnter) // ExcludeComponents : TStringList (eg TMemo etc.) // // Methods // Enable Enables GLOBAL remapping of Enter Keys // Disable Disables GLOBAL remapping of Enter Keys // // ================================================================== interface uses Windows, Messages, Forms, Classes, Controls, SysUtils; type TEnterAsTabKeyValue = (kyTab,kyEnter); TEnterAsTab = class(TComponent) private SaveOnMessage : TMessageEvent; FExcludeComponents : TStringList; FKBenter, FKPenter : TEnterAsTabKeyValue; procedure SetExcludeComponents(NewValue : TStringList); protected procedure DoEnterAsTab(var Msg: TMsg; var Handled: Boolean); public constructor Create(AOwner : TComponent); override; destructor Destroy; override; procedure Enable; procedure Disable; published property KeyboardEnterType : TEnterAsTabKeyValue read FKBenter write FKBenter; property KeypadEnterType : TEnterAsTabKeyValue read FKPenter write FKPenter; property ExcludeComponents : TStringList read FExcludeComponents write SetExcludeComponents; end; procedure Register; // ------------------------------------------------------------------ implementation constructor TEnterAsTab.Create(AOwner : TComponent); begin inherited Create(AOwner); FKBenter := kyTab; FKPenter := kyTab; FExcludeComponents := TStringList.Create; FExcludeComponents.Add('TBitBtn'); FExcludeComponents.Add('TButton'); FExcludeComponents.Add('TDBMemo'); FExcludeComponents.Add('TDBRichEdit'); FExcludeComponents.Add('TMemo'); FExcludeComponents.Add('TRichEdit'); if not (csDesigning in ComponentState) then SaveOnMessage := Application.OnMessage; end; destructor TEnterAsTab.Destroy; begin FExcludeComponents.Free; end; procedure TEnterAsTab.DoEnterAsTab(var Msg: TMsg; var Handled: Boolean); type TEnterType = (KeyPad,KeyBoard); var EnterType : TEnterType; CurrControl : TWinControl; i : integer; Excluded : boolean; begin if Msg.Message = WM_KEYDOWN then begin if Msg.wParam = VK_RETURN then begin Excluded := false; CurrControl := Screen.ActiveControl; for i := 0 to FExcludeComponents.Count - 1 do begin if UpperCase(FExcludeComponents.Strings[i]) = UpperCase(CurrControl.ClassName) then begin Excluded := true; break; end; end; if not Excluded then begin if (Msg.lParam and $1000000 0) then EnterType := KeyPad // ENTER on numeric keypad else EnterType := KeyBoard; // ENTER on the std keyboard if ((FKPenter = kyTab) and (EnterType = KeyPad)) or ((FKBenter = kyTab) and (EnterType = KeyBoard)) then Keybd_Event(VK_TAB,0,0,0); end; end; end; if Assigned(SaveOnMessage) then SaveOnMessage(Msg,Handled); end; procedure TEnterAsTab.SetExcludeComponents(NewValue : TStringList); begin FExcludeComponents.Assign(NewValue); end; procedure TEnterAsTab.Enable; begin Application.OnMessage := DoEnterAsTab; end; procedure TEnterAsTab.Disable; begin Application.OnMessage := SaveOnMessage; end; // =================================== // Register to Delphi pallete // =================================== procedure Register; begin RegisterComponents('Win95', [TEnterAsTab]); end; end.