Skip to content

Commit 1ed5315

Browse files
committed
Improve error message when hotkey registration fails
See #65
1 parent 9af0697 commit 1ed5315

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

PasteIntoFile/KeyboardHook.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PasteIntoFile {
99
/// </summary>
1010
public sealed class KeyboardHook : IDisposable {
1111
// Registers a hot key with Windows.
12-
[DllImport("user32.dll")]
12+
[DllImport("user32.dll", SetLastError = true)]
1313
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
1414
// Unregisters the hot key with Windows.
1515
[DllImport("user32.dll")]
@@ -77,8 +77,10 @@ public void RegisterHotKey(ModifierKeys modifier, Keys key) {
7777
_currentId += 1;
7878

7979
// register the hot key.
80-
if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
81-
throw new InvalidOperationException("Couldn’t register the hot key.");
80+
if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key)) {
81+
var error = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
82+
throw new InvalidOperationException($"Registration of HotKey {modifier.ToString().Replace(", ", "+").ToUpper()}+{key} failed with error {error.NativeErrorCode}: {error.Message}");
83+
}
8284
}
8385

8486
/// <summary>

0 commit comments

Comments
 (0)