【问题标题】:Convert Shortcut Hotkey combination to real keys?将快捷键热键组合转换为真实键?
【发布时间】:2013-12-01 04:52:08
【问题描述】:

如何将快捷方式的 Hotkey 属性转换为字符串类型的组合或直接将值转换为正确的组合?

我正在使用IShellLink 接口从快捷方式中检索信息。

Private Shared lnk_hotkey As Short

interface IShellLinkW...
    Sub GetHotkey(ByRef pwHotkey As Short)
    Sub SetHotkey(ByVal wHotkey As Short)
end interface...

function to retrieve the hotkey...
    DirectCast(lnk, IPersistFile).Load(ShortcutFile, 0) ' Load the shortcut
    DirectCast(lnk, IShellLinkW).GetHotkey(lnk_hotkey) ' Retrieve the Hotkey
    Return lnk_hotkey ' Return the hotkey
end function...

现在,我有一个快捷键,它用于 Hotkey 属性:

CONTROL + ALT + A

嗯,Keys 枚举是这样说的:

  Keys.ControlKey = 17
  Keys.Alt = 262144
  Keys.A = 65

那么对于像Keys.ControlKey Or Keys.Alt Or Keys.A这样的组合,总和是:262226,但是IShellLink返回一个1601,那么这个用什么样的键表示呢?

我既需要一个解决方案来表示正确的快捷键热键,也需要为Sub SetHotkey(ByVal wHotkey As Short) mehotd 设置一个新的自定义热键。

编辑:

哦,这就是我尝试过的:

    Dim ShortcutInfo As Shortcut.ShortcutInfo =
        Shortcut.GetInfo("File.lnk")

    MsgBox(ShortcutInfo.Hotkey) ' Result: 1601

    MsgBox([Enum].Parse(GetType(Keys), ShortcutInfo.Hotkey).ToString)
    ' Result: 1601

    Dim k As New KeysConverter
    MsgBox(k.ConvertToString(1601).ToString)
    ' Result: (empty)

更新:

我正在尝试...

首先我添加了这个枚举:

Public Enum HotkeyModifiers As Short
    SHIFT = 1
    CONTROL = 2
    ALT = 4
End Enum

所以流程应该自动化任务,但我不知道现在在这里做什么:

Public Shared Sub CreateShortcut(blah blah blah... 
                                 ByVal HotKey As Tuple(Of HotkeyModifiers, Keys))

MsgBox(CInt(HotKey.Item1 & HotKey.Item2))
' Result: 665

' I need to translate the 665 into the right value, 1601.

end sub

这就是我调用方法的方式:

Shortcut.CreateShortcut(Tuple.Create(HotkeyModifiers.CONTROL Or HotkeyModifiers.ALT, Keys.A))

【问题讨论】:

    标签: .net vb.net winapi hotkeys keyboard-shortcuts


    【解决方案1】:

    在 CommCtrl.h 中有:

    #define HOTKEYF_SHIFT           0x01
    #define HOTKEYF_CONTROL         0x02
    #define HOTKEYF_ALT             0x04
    

    1601 是十六进制的 641。

    低位字节为0x41,十进制65:'A'

    高位字节为0x06,即HOTKEYF_CONTROL + HOTKEYF_ALT。

    要将 hoy 键设置为 SHIFT+CONTROL+B,请使用 0x342 (834)

    AFAIK,您不能将 KeysConverter 与两个字节的组合一起使用。 使用 KeysConverter 作为低位字节,从高位字节手动添加修饰键。

    IShellLink::SetHotkey 文档指出:

    wHotkey
    Type: WORD
    The new keyboard shortcut. The virtual key code is in the low-order byte,
    and the modifier flags are in the high-order byte. The modifier flags can be
    a combination of the values specified in the description of
    the IShellLink::GetHotkey method.
    

    您必须创建一个 16 位的值,低 8 位设置为 Key,高 8 位由 3 个可能的修饰符按位组合而成。

    【讨论】:

    • 感谢您提供的所有信息,但我需要更多帮助才能做到这一点,您已经说过如何翻译 1601,但我还需要翻译 Keys 枚举组合(262226 号)到那个1601,我需要做哪些步骤?举个例子,我想设置这样的热键Dim hotkey = Keys.ControlKey Or Keys.Shift Or Keys.A然后将这些键转换为正确值(1601)的过程需要自动化而不是手动
    • 我已经更新了我的问题,添加了一个新代码,请查看更新,谢谢。
    • 完成!:DirectCast(lnk, IShellLinkW).SetHotkey(Convert.ToInt32(CInt(HotKey.Item1 & Hex(HotKey.Item2)), 16))
    猜你喜欢
    • 2018-01-03
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多