Mirror

Working with KeyBoard Binding (Views: 100)


Problem/Question/Abstract:

How to set your own shortcut keys while working with delphi editor? If you want your own piece of shortcut key to perform a certain action for you then this code will help

Answer:

Include this unit in a delphi package, and install the package. Now, if you press ctrl + d you will get the 'This was written by Subha Narayanan' in your editor window.
The actual process is very simple. We use the interface TNotifier Object and IOTAkeyboardbinding to create our own interface.
Our main key to perform this action is the procedure 'Dupline'

unit DupLineBinding;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  menus, ToolsApi;

type
  TDupLineBinding = class(TNotifierObject, IOTAKeyboardBinding)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    procedure DupLine(const Context: IOTAKeyContext; KeyCode: TShortCut;
      var BindingResult: TKeyBindingResult);
    {IOTAKeyBoardBinding}
    function GetBindingType: TBindingType;
    function GetDisplayName: string;
    function GetName: string;
    procedure BindKeyboard(const BindingServices: IOTAKeyBindingServices);

  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  (BorlandIDEServices as
    IOTAKeyBoardServices).AddKeyBoardBinding(TDupLineBinding.Create);
end;

function TDupLineBinding.GetBindingType: TBindingType;
begin
  Result := btPartial;
end;

function TDupLineBinding.GetDisplayName;
begin
  Result := 'Subha Line Binding';
    {The way it shoudl appear in the delphi ide editor window}
end;

function TDupLineBinding.GetName;
begin
  Result := 'sn.dupline'; {Should be unique}
end;

procedure TDupLineBinding.DupLine(const Context: IOTAKeyContext; KeyCode: TShortCut;
  var BindingResult: TKeyBindingResult);
var
  ep: IOTAEditPosition;
  eb: IOTAEditBlock;
  r, c: Integer;
begin
  {Actual place where the writting into editor takes place}
  try
    ep := Context.EditBuffer.EditPosition;
    ep.Save;
    r := ep.Row;
    c := ep.Column;
    eb := Context.EditBuffer.EditBlock;
    ep.MoveBOL;
    eb.Reset;
    eb.BeginBlock;
    eb.Extend(Ep.Row + 1, 1);
    eb.EndBlock;
    eb.Copy(False);
    ep.MoveBOL;
    ep.Paste;
    ep.Move(r, c);
  finally
    ep.Restore;
  end;
  BindingResult := krHandled;
end;

procedure TDupLineBinding.BindKeyboard(const BindingServices: IOTAKeyBindingServices);
{Here we specify the shortcut key which should do the action}
begin
  BindingServices.AddKeyBinding([Shortcut(Ord('D'), [ssCtrl])], DupLine, nil);
end;

end.

<< Back to main page