如何为我以编程方式创建的快捷方式分配热键?

问题描述 投票:0回答:1

我正在使用此类在 C# 上创建桌面快捷方式:

[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
internal class ShellLink2
{
}
internal interface IShellLinkW
{
    /// <summary>Retrieves the path and file name of a Shell link object</summary>
    void GetPath([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATAW pfd, SLGP_FLAGS fFlags);
    /// <summary>Retrieves the list of item identifiers for a Shell link object</summary>
    void GetIDList(out IntPtr ppidl);
    void GetHotkey(out short pwHotkey);
    /// <summary>Sets a hot key for a Shell link object</summary>
    void SetHotkey(short wHotkey);
    /// <summary>Retrieves the show command for a Shell link object</summary>
}

为了为“Application.lnk”创建热键,我使用这个:

var m = (IShellLinkW)new ShellLink2();
// setup shortcut information
m.SetDescription("desc");
m.SetPath("path");
m.SetWorkingDirectory("path");
m.SetIconLocation("icon", 0);
m.SetHotkey((short)(Keys.Control + Keys.Alt + Keys.S));
// save it
var file = m as IPersistFile;
file?.Save(Path.Combine(target, $"{name}.lnk"), false);

当我想像这样分配下面的热键时

m.SetHotkey((short)(Keys.Control + Keys.Alt + Keys.S));
,代码显示如下错误:

在此场景中如何将

Ctrl + Alt + S
分配给我的“Application.lnk”快捷方式?

c# hotkeys
1个回答
0
投票

Keys
enum
,它们不支持
+
运算符。 您需要先将每个转换为
short

(short)Keys.Control + (short)Keys.Alt + (short)Keys.S

// or use bitwise operator

(short)(Keys.Control | Keys.Alt | Keys.S)
© www.soinside.com 2019 - 2024. All rights reserved.