在这个漂亮的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变量有错误的值,但我不知道如何修复它。任何帮助都很感激
1条答案
按热度按时间flvlnr441#
解决方案在pinvoke.net(*
KeyModifiers
Enum*)的链接中:输出:
热键WinKeyCtrl+M已注册到 * 设置 * -> * 放大镜 * 为我...