Alister
Alister

Reputation: 6837

IDE Plugin to disable the Insert key

I've been playing around with an IDE plugin for Delphi (10.3 Rio) to disable the insert key (does anyone actually use type-over mode?).

It installs into the IDE fine, but if I uninstall it, an exception occurs when I press the insert key.

Can anyone help me out here?

Here is the updated source: (I've added the finalization section, still get an error)

unit MyBinding;

interface

procedure Register;

implementation

uses Windows, Classes, SysUtils, ToolsAPI, Vcl.Menus;

type
  TLearnDelphiKeyBinding = class(TNotifierObject, IOTAKeyboardBinding)
  private
    procedure DoNothing(const Context: IOTAKeyContext; KeyCode: TShortCut; var BindingResult: TKeyBindingResult);

  public
    function GetBindingType: TBindingType;
    function GetDisplayName: string;
    function GetName: string;
    procedure BindKeyboard(const BindingServices: IOTAKeyBindingServices);
  end;

var
  LearnDelphiKeyBindingIndex : integer = 0;

procedure Register;
begin
  LearnDelphiKeyBindingIndex := (BorlandIDEServices as IOTAKeyBoardServices).AddKeyboardBinding(TLearnDelphiKeyBinding.Create);
end;

procedure TLearnDelphiKeyBinding.BindKeyboard(const BindingServices: IOTAKeyBindingServices);
begin
  BindingServices.AddKeyBinding([ShortCut(VK_INSERT, [])], DoNothing, nil);
end;

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

function TLearnDelphiKeyBinding.GetDisplayName: string;
begin
  Result := 'Disable Insert';
end;

function TLearnDelphiKeyBinding.GetName: string;
begin
  Result := 'LearnDelphi.DisableInsert';
end;

procedure TLearnDelphiKeyBinding.DoNothing(const Context: IOTAKeyContext;
  KeyCode: TShortCut; var BindingResult: TKeyBindingResult);
begin
  BindingResult := krHandled;
end;

initialization
finalization
  if LearnDelphiKeyBindingIndex > 0 then
    (BorlandIDEServices as IOTAKeyboardServices).RemoveKeyboardBinding(LearnDelphiKeyBindingIndex);

end.

enter image description here

Upvotes: 3

Views: 241

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595827

You need to call IOTAKeyboardServices.RemoveKeyboardBinding() during finalization, passing it the value returned by IOTAKeyboardServices.AddKeyboardBinding() (which you are not currently saving).

Have a look at Chapter 4: Key Bindings and Debugging Tools in David Hoyle's blog, which provides an example of a keyboard binding wizard using the OpenTools API.

Upvotes: 7

Related Questions