So with a lot of help of this nice site
i was able to create the following Code
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())"
}
But all i get is an Error that says ErrorCode 1004 Invalid Attributes
i think the $modifier Variables have the wrong value but i have no clue how to fix it. any help is appreciated
The solution is in your link to pinvoke.net (
KeyModifiersEnum):Output:
Hot key WinKeyCtrl+M is already registered to Settings -> Magnifier for me…