Delphi:创建新 UNIT 的键盘快捷键?

Delphi: Keyboard shortcut to create a new UNIT?

在Delphi 10.3.3 IDE中,是否有创建新UNIT的快捷键?或者是否可以创建这样的键盘快捷键?

翻文件->新建太麻烦了

不,没有这样的捷径。另一种方法是右键单击项目 -> Add New -> Unit.

有像 GExperts 或 CnPack 这样的第三方插件可以将宏功能添加到 IDE 中,使您可以自动执行许多此类任务,但这不是内置在 vanilla IDE 中的。您还可以使用 Open Tools API.

编写自己的自定义 IDE 扩展

由于主菜单允许使用 ALT 快捷方式,您可以这样做:

ALT + F, N, U

快速选择文件 > 新建 > 单位。

下面的代码显示了一个最小的 IDE plug-in,它使用 OTA 添加了一个 hot-key 开设新单位。如图所示,使用Ctrl-Alt-U.

激活

它的工作方式是使用 OTA 服务访问 IDE 的主菜单并添加 一个新的菜单条目 - 标题为 MyAdded 以便于识别 - 如果可见 (也不一定)会出现在 IDE 主菜单中 Help 的右侧。

此添加的菜单项的快捷方式为 Ctrl-Alt-U。它的 OnClick 处理程序 MyAddedClick 执行 方法 ClickNewUnit,迭代文件 |找到 Unit - Delphi MenuItem 的新子菜单 并调用其 Click 方法,导致 IDE 创建并打开一个新单元。

将下面的单元添加到一个名为NewUnit.Dpk的新.DPK文件中,编译它然后使用Component |安装包 安装。

unit NewUnitu;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ToolsAPI, Menus;

type
  TNewUnit = class
  private
    procedure ClickNewUnit(Sender: TObject);
  public
    //Services: IOTAServices;
    IntaServices : INTAServices40;
    ModServices: IOTAModuleServices;
    Module : IOTAModule;
    AddedMenuItem : TMenuItem;
    MainMenu : TMainMenu;
    procedure MyAddedClick(Sender : TObject);
    constructor Create;
    destructor Destroy; override;
  end;

var
  NewUnit: TNewUnit;
 [...]

constructor TNewUnit.Create;
begin
  inherited Create;
  //Services := BorlandIDEServices as IOTAServices;  not needed
  IntaServices := BorlandIDEServices as INTAServices40;
  Assert(IntaServices <> Nil);

  MainMenu := IntaServices.GetMainMenu;

  AddedMenuItem := TMenuItem.Create(Nil);
  AddedMenuItem.Caption := 'MyAdded';
  AddedMenuItem.ShortCut := 49237;  // = Ctrl-Alt-U
  AddedMenuItem.OnClick := MyAddedClick;
  //  AddedMenuItem.Visible := False;
  MainMenu.Items.Add(AddedMenuItem);
end;

destructor TNewUnit.Destroy;
begin
  MainMenu.Items.Remove(AddedMenuItem);
  AddedMenuItem.Free;
  //Services := Nil;
  IntaServices := Nil;
  inherited Destroy;
end;

procedure TNewUnit.ClickNewUnit(Sender : TObject);
var
  MenuItem : TMenuItem;
  AMenuItem : TMenuItem;
  i,
  j,
  k : Integer;
  Done : Boolean;
begin
 // ShowMessage('In click new unit'); exit;    for debugging
  Done := False;
  for i := 0 to MainMenu.Items.Count - 1 do begin
    MenuItem := MainMenu.Items[i];
    if Pos('file', LowerCase(MenuItem.Caption)) > 0 then begin
      AMenuItem := MenuItem;
      for j := 0 to AMenuItem.Count - 1 do begin
        for k := 0 to AMenuItem.Items[j].Count - 1 do begin
          if Pos('unit', LowerCase(MenuItem.Items[j].Items[k].Caption)) > 0 then begin
            MenuItem.Items[j].Items[k].Click;
            Done := True;
            Break;
          end;
        end;
        if Done then Break;
      end;
    end;
    if Done then Break;
  end;
end;

procedure TNewUnit.MyAddedClick(Sender: TObject);
begin
  ClickNewUnit(Sender);
end;

initialization

  NewUnit := TNewUnit.Create;

finalization

  NewUnit.Free;

end.

注意事项

  • 这是在 Delphi 10.2.2 Seattle 中编写和测试的,因为我没有安装 Rio。

  • 只要在按下 Ctrl-Alt-U 时打开 code-editor window,它就可以正常工作。如果它检查是否有 s code-editor window 打开,它会更健壮,如果没有,打开一个。