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
This commit is contained in:
RKrom 2010-08-19 05:25:10 +00:00
commit bd60a8c339
2 changed files with 27 additions and 16 deletions

View file

@ -407,10 +407,10 @@ namespace Greenshot {
private void RegisterHotkeys() { private void RegisterHotkeys() {
bool suc = true; bool suc = true;
suc &= HotkeyHelper.RegisterHotKey((int)this.Handle, (int)HotkeyHelper.Modifiers.NONE, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureRegion)); suc &= HotkeyHelper.RegisterHotKey(this.Handle, (uint)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(this.Handle, (uint)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(this.Handle, (uint)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.SHIFT, HotkeyHelper.VK_SNAPSHOT, new HotKeyHandler(CaptureLastRegion));
if (!suc) { if (!suc) {
MessageBox.Show(lang.GetString(LangKey.warning_hotkeys),lang.GetString(LangKey.warning)); MessageBox.Show(lang.GetString(LangKey.warning_hotkeys),lang.GetString(LangKey.warning));
} }
@ -644,7 +644,7 @@ namespace Greenshot {
LOG.Info("Exit: " + EnvironmentInfo.EnvironmentToString(false)); LOG.Info("Exit: " + EnvironmentInfo.EnvironmentToString(false));
try { try {
// Make sure hotkeys are disabled // Make sure hotkeys are disabled
HotkeyHelper.UnregisterHotkeys(Handle.ToInt32()); HotkeyHelper.UnregisterHotkeys(Handle);
} catch (Exception e) { } catch (Exception e) {
LOG.Error("Error unregistering hotkeys!", e); LOG.Error("Error unregistering hotkeys!", e);
} }

View file

@ -32,11 +32,11 @@ namespace Greenshot.Helpers {
public class HotkeyHelper { public class HotkeyHelper {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(HotkeyHelper)); private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(HotkeyHelper));
private const int WM_HOTKEY = 0x312; private const uint WM_HOTKEY = 0x312;
public const int VK_SNAPSHOT = 0x2C; public const uint VK_SNAPSHOT = 0x2C;
#region enums #region enums
public enum Modifiers : int { public enum Modifiers : uint {
NONE = 0, NONE = 0,
ALT = 1, ALT = 1,
CTRL = 2, CTRL = 2,
@ -53,14 +53,25 @@ namespace Greenshot.Helpers {
} }
#region User32 #region User32
[DllImport("user32.dll")] [DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey (int hwnd, int id, int fsModifiers, int vk); [return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint virtualKeyCode);
private static extern bool UnregisterHotKey (int hwnd, int id);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#endregion #endregion
public static bool RegisterHotKey(int hnd, int modifierKeyCode, int virtualKeyCode, HotKeyHandler handler) { /// <summary>
if (RegisterHotKey(hnd, hotKeyCounter, modifierKeyCode, virtualKeyCode)) { /// Register a hotkey
/// </summary>
/// <param name="hWnd">The window which will get the event</param>
/// <param name="modifierKeyCode">The modifier, e.g.: Modifiers.CTRL, Modifiers.NONE or Modifiers.ALT</param>
/// <param name="virtualKeyCode">The virtual key code</param>
/// <param name="handler">A HotKeyHandler, this will be called to handle the hotkey press</param>
/// <returns></returns>
public static bool RegisterHotKey(IntPtr hWnd, uint modifierKeyCode, uint virtualKeyCode, HotKeyHandler handler) {
if (RegisterHotKey(hWnd, hotKeyCounter, modifierKeyCode, virtualKeyCode)) {
keyHandlers.Add(hotKeyCounter, handler); keyHandlers.Add(hotKeyCounter, handler);
hotKeyCounter++; hotKeyCounter++;
return true; 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) { foreach(int hotkey in keyHandlers.Keys) {
UnregisterHotKey(hnd, hotkey); UnregisterHotKey(hWnd, hotkey);
} }
// Remove all key handlers // Remove all key handlers
keyHandlers.Clear(); keyHandlers.Clear();