允许用户在 Inno Setup 中为 icon/shortcut 选择热键

Allow users choose hotkeys for icon/shortcut in Inno Setup

我希望允许 运行 安装程序通过 Inno Setup 进行安装的用户选择是否使用热键,如果是,允许他们选择使用哪些热键。

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}";  \
    HotKey: "ctrl+alt+b"

Name: "{autoprograms}\{#MyAppName2}"; Filename: "{app}\{#MyAppExeName2}"; \
    HotKey: "ctrl+alt+x"

这是我的[Icons]部分

Icons section HotKey parameter cannot be modified at run time. So you will have to create the whole shortcut programmatically. An easy API that allows creating a shortcut with a hot key is WScript.Shell.CreateShortcut.

[Code]
var
  App1HotKeyCombo: TNewComboBox;
  App2HotKeyCombo: TNewComboBox;

const
  NoneHotKey = 'None';

procedure InitializeWizard();
var
  HotKeysPage: TWizardPage;
  ALabel: TNewStaticText;
begin
  Log('InitializeWizard');
  HotKeysPage := CreateCustomPage(wpSelectTasks, 'Select your hot keys', '');

  App1HotKeyCombo := TNewComboBox.Create(HotKeysPage);
  App1HotKeyCombo.Parent := HotKeysPage.Surface;
  App1HotKeyCombo.Left := ScaleX(200);
  App1HotKeyCombo.Top := 0;
  App1HotKeyCombo.Width := ScaleX(100);
  App1HotKeyCombo.Style := csDropDownList;
  App1HotKeyCombo.Items.Add(NoneHotKey);
  App1HotKeyCombo.Items.Add('Ctrl+Alt+A');
  App1HotKeyCombo.Items.Add('Ctrl+Alt+B');
  App1HotKeyCombo.Items.Add('Ctrl+Alt+C');
  App1HotKeyCombo.ItemIndex := 1;
  ALabel := TNewStaticText.Create(HotKeysPage);
  ALabel.Parent := HotKeysPage.Surface;
  ALabel.Top := App1HotKeyCombo.Top + ScaleY(4);
  ALabel.Left := 0;
  ALabel.Caption := 'Hot key for application 1:';
  ALabel.FocusControl := App1HotKeyCombo;

  App2HotKeyCombo := TNewComboBox.Create(HotKeysPage);
  App2HotKeyCombo.Parent := HotKeysPage.Surface;
  App2HotKeyCombo.Left := App1HotKeyCombo.Left;
  App2HotKeyCombo.Top := App1HotKeyCombo.Top + App1HotKeyCombo.Height + ScaleY(8);
  App2HotKeyCombo.Width := App1HotKeyCombo.Width;
  App2HotKeyCombo.Style := csDropDownList;
  App2HotKeyCombo.Items.Assign(App1HotKeyCombo.Items);
  App2HotKeyCombo.ItemIndex := 2;
  ALabel := TNewStaticText.Create(HotKeysPage);
  ALabel.Parent := HotKeysPage.Surface;
  ALabel.Top := App2HotKeyCombo.Top + ScaleY(4);
  ALabel.Left := 0;
  ALabel.Caption := 'Hot key for application 2:';
  ALabel.FocusControl := App2HotKeyCombo;
end;

procedure CreateShortCut(IconName, Path: string; AppHotKeyCombo: TNewComboBox);
var
  WshShell: Variant;
  ShellLink: Variant;
  Msg: string;
begin
  WshShell := CreateOleObject('WScript.Shell');
  IconName := ExpandConstant(IconName) + '.lnk';
  ShellLink := WshShell.CreateShortcut(IconName)
  ShellLink.TargetPath := ExpandConstant(Path);
  ShellLink.WindowStyle := SW_SHOWNORMAL;
  if AppHotKeyCombo.Text <> NoneHotKey then
    ShellLink.Hotkey := AppHotKeyCombo.Text;
  ShellLink.Save;
  Msg := 'Created "%s" icon pointing to "%s" with "%s" hotkey';
  Log(Format(Msg, [IconName, ShellLink.TargetPath, ShellLink.Hotkey]));
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    CreateShortCut('{autoprograms}\MyProg1', '{app}\MyProg.exe', App1HotKeyCombo);
    CreateShortCut('{autoprograms}\MyProg2', '{app}\MyProg.exe', App2HotKeyCombo);
  end;
end;


一个更健壮的API,是IShellLink。这就是 Inno Setup 内部使用的。虽然它需要相当多的代码。有关示例,请参阅 .


您还必须确保快捷方式在卸载时被删除。您可以为此使用 UninstallDelete section


如果您不想编写快捷方式创建代码,另一种方法是使用预处理器为您要提供的每个快捷方式生成单独的 [Icons] 条目,并使用 Check parameters 仅激活与用户选择的快捷方式对应的条目。