在PowerShell(C#?)

zfciruhq  于 2023-05-17  发布在  Shell
关注(0)|答案(1)|浏览(165)

在这个漂亮的site的帮助下
我可以创建以下代码

Add-Type -TypeDefinition @"
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    namespace User32{
    public static class NativeMethods{
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    [DllImport("Kernel32.dll")]
    public static extern int GetLastError();
    [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
    public static extern int FormatMessage(uint dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, [Out]StringBuilder lpBuffer, uint nSize, IntPtr Arguments);
    }
    }
    "@ -passthru
    #Add-Type -TypeDefinition $TypeDefinition -PassThru
    $modifier1 = [int][System.Windows.Forms.Keys]::ControlKey
    $modifier2 = [int][System.Windows.Forms.Keys]::Menu
    $key = [int][System.Windows.Forms.Keys]::M
    $Callback = { $notifyicon1_MouseDoubleClick.Invoke() }
    
    [int]$hotkey_ID = 1

    $hotkey_registered = [User32.NativeMethods]::RegisterHotKey([System.IntPtr]::Zero, $hotkey_id, $modifier1 -bor $modifier2, $key)
    
    if (-not $hotkey_registered)
    {
        $errorCode = [User32.NativeMethods]::GetLastError()
        $errorMsg = New-Object System.Text.StringBuilder(255)
        [User32.NativeMethods]::FormatMessage(0x00001000, [IntPtr]::Zero, $errorCode, 0, $errorMsg, $errorMsg.Capacity, [IntPtr]::Zero) | Out-Null
        Logline -Message "Failed to register hotkey. Error code: $errorCode Error message: $($errorMsg.ToString().Trim())"
    }

但我得到的是一个错误,说ErrorCode 1004无效属性
我认为$modifier变量有错误的值,但我不知道如何修复它。任何帮助都很感激

flvlnr44

flvlnr441#

解决方案在pinvoke.net(* KeyModifiers Enum*)的链接中:

if ( -not ('User32.HotKeyModifiers' -as [type]) ) {
   Add-Type -TypeDefinition @"
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    namespace User32{
        [Flags]
        public enum HotKeyModifiers
        {
        Alt = 1,        // MOD_ALT
        Control = 2,    // MOD_CONTROL
        Shift = 4,      // MOD_SHIFT
        WindowsKey = 8, // MOD_WIN // Either WINDOWS key was held down.
                        // These keys are labeled with the Windows logo.
                        // Keyboard shortcuts that involve the WINDOWS key
                        // are reserved for use by the operating system.
        }

        public static class NativeMethods{
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

            [DllImport("Kernel32.dll")]
            public static extern int GetLastError();

            [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
            public static extern int FormatMessage(uint dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, [Out]StringBuilder lpBuffer, uint nSize, IntPtr Arguments);
        }
    }
"@ -passthru
#Add-Type -TypeDefinition $TypeDefinition -PassThru
}

if ( -not ('System.Windows.Forms.Keys' -as [type]) ) {
    Add-Type -AssemblyName System.Windows.Forms
}
$modifier1 = [User32.HotKeyModifiers]::Control.value__    #[int][System.Windows.Forms.Keys]::ControlKey
$modifier2 = [User32.HotKeyModifiers]::WindowsKey.value__ #[int][System.Windows.Forms.Keys]::Menu
$key = [int][System.Windows.Forms.Keys]::M
$Callback = { $notifyicon1_MouseDoubleClick.Invoke() }
    
[int]$hotkey_ID = 1

$hotkey_registered = [User32.NativeMethods]::RegisterHotKey(
    [System.IntPtr]::Zero, $hotkey_id, $modifier1 -bor $modifier2, $key)
    
if (-not $hotkey_registered)
{
    $errorCode = [User32.NativeMethods]::GetLastError()
    $errorMsg = New-Object System.Text.StringBuilder(255)
    [User32.NativeMethods]::FormatMessage(0x00001000, [IntPtr]::Zero, $errorCode, 0, $errorMsg, $errorMsg.Capacity, [IntPtr]::Zero) | Out-Null
    # Logline -Message
    "Failed to register hotkey. Error code: $errorCode Error message: $($errorMsg.ToString().Trim())"
}

输出

.\SO\76045855.ps1
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     HotKeyModifiers                          System.Enum
True     False    NativeMethods                            System.Object
Failed to register hotkey. Error code: 1409 Error message: Hot key is already registered.

热键WinKeyCtrl+M已注册到 * 设置 * -> * 放大镜 * 为我...

相关问题