From bd60a8c339d2bfee395eabdc5e308db550b0d83d Mon Sep 17 00:00:00 2001 From: RKrom Date: Thu, 19 Aug 2010 05:25:10 +0000 Subject: [PATCH] Merge from BRANCH: Fix for Bug # 3048290: hotkeys don't work in Windows 7 x64. Although I can't test this I am pretty sure the problem is due to not using an IntPtr but an int for the windows handle instead. As the old code was wrong I changed it anyhow, hopefully someone can test this. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@823 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Forms/MainForm.cs | 10 ++++---- GreenshotCore/Helpers/HotkeyHelper.cs | 33 ++++++++++++++++++--------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index e95546a7e..9127b3462 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -407,10 +407,10 @@ namespace Greenshot { private void RegisterHotkeys() { bool suc = true; - suc &= HotkeyHelper.RegisterHotKey((int)this.Handle, (int)HotkeyHelper.Modifiers.NONE, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureRegion)); - suc &= HotkeyHelper.RegisterHotKey((int)this.Handle, (int)HotkeyHelper.Modifiers.ALT, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureWindow)); - suc &= HotkeyHelper.RegisterHotKey((int)this.Handle, (int)HotkeyHelper.Modifiers.CTRL, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureFullScreen)); - suc &= HotkeyHelper.RegisterHotKey((int)this.Handle, (int)HotkeyHelper.Modifiers.SHIFT, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureLastRegion)); + suc &= HotkeyHelper.RegisterHotKey(this.Handle, (uint)HotkeyHelper.Modifiers.NONE, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureRegion)); + suc &= HotkeyHelper.RegisterHotKey(this.Handle, (uint)HotkeyHelper.Modifiers.ALT, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureWindow)); + suc &= HotkeyHelper.RegisterHotKey(this.Handle, (uint)HotkeyHelper.Modifiers.CTRL, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureFullScreen)); + suc &= HotkeyHelper.RegisterHotKey(this.Handle, (uint)HotkeyHelper.Modifiers.SHIFT, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureLastRegion)); if (!suc) { MessageBox.Show(lang.GetString(LangKey.warning_hotkeys),lang.GetString(LangKey.warning)); } @@ -644,7 +644,7 @@ namespace Greenshot { LOG.Info("Exit: " + EnvironmentInfo.EnvironmentToString(false)); try { // Make sure hotkeys are disabled - HotkeyHelper.UnregisterHotkeys(Handle.ToInt32()); + HotkeyHelper.UnregisterHotkeys(Handle); } catch (Exception e) { LOG.Error("Error unregistering hotkeys!", e); } diff --git a/GreenshotCore/Helpers/HotkeyHelper.cs b/GreenshotCore/Helpers/HotkeyHelper.cs index ae9a053ab..10167d168 100644 --- a/GreenshotCore/Helpers/HotkeyHelper.cs +++ b/GreenshotCore/Helpers/HotkeyHelper.cs @@ -32,11 +32,11 @@ namespace Greenshot.Helpers { public class HotkeyHelper { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(HotkeyHelper)); - private const int WM_HOTKEY = 0x312; - public const int VK_SNAPSHOT = 0x2C; + private const uint WM_HOTKEY = 0x312; + public const uint VK_SNAPSHOT = 0x2C; #region enums - public enum Modifiers : int { + public enum Modifiers : uint { NONE = 0, ALT = 1, CTRL = 2, @@ -53,14 +53,25 @@ namespace Greenshot.Helpers { } #region User32 - [DllImport("user32.dll")] - private static extern bool RegisterHotKey (int hwnd, int id, int fsModifiers, int vk); - [DllImport("user32.dll")] - private static extern bool UnregisterHotKey (int hwnd, int id); + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode); + + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); #endregion - public static bool RegisterHotKey(int hnd, int modifierKeyCode, int virtualKeyCode, HotKeyHandler handler) { - if (RegisterHotKey(hnd, hotKeyCounter, modifierKeyCode, virtualKeyCode)) { + /// + /// Register a hotkey + /// + /// The window which will get the event + /// The modifier, e.g.: Modifiers.CTRL, Modifiers.NONE or Modifiers.ALT + /// The virtual key code + /// A HotKeyHandler, this will be called to handle the hotkey press + /// + public static bool RegisterHotKey(IntPtr hWnd, uint modifierKeyCode, uint virtualKeyCode, HotKeyHandler handler) { + if (RegisterHotKey(hWnd, hotKeyCounter, modifierKeyCode, virtualKeyCode)) { keyHandlers.Add(hotKeyCounter, handler); hotKeyCounter++; return true; @@ -70,9 +81,9 @@ namespace Greenshot.Helpers { } } - public static void UnregisterHotkeys(int hnd) { + public static void UnregisterHotkeys(IntPtr hWnd) { foreach(int hotkey in keyHandlers.Keys) { - UnregisterHotKey(hnd, hotkey); + UnregisterHotKey(hWnd, hotkey); } // Remove all key handlers keyHandlers.Clear();