Beispiel #1
0
        /// <summary>
        /// Registers a hot key in the system.
        /// </summary>
        /// <param name="modifier">The modifiers that are associated with the hot key.</param>
        /// <param name="key">The key itself that is associated with the hot key.</param>
        public void RegisterHotkey(ModifierHookKeys modifier, Keys key)
        {
            // Increment the counter.
            this.currentId += 1;

            // Register the hot key.
            if (!UnsafeNativeMethods.RegisterHotKey(this.window.Handle, this.currentId, (uint)modifier, (uint)key))
            {
                int lastError = Marshal.GetLastWin32Error();

                if (lastError == 1409)
                {
                    // ERROR_HOTKEY_ALREADY_REGISTERED

                    // This is a temporary message until I can implement custom hotkeys.
                    // Even then some kind of message or notification will need to be shown to let the user
                    // know that the default or chosen hotkey will not work.
                    MessageBox.Show(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            "This hotkey is already registered by another\n" +
                            "application and will be skipped:\n\n{0}, {1}",
                            modifier.ToString("G"),
                            key.ToString()),
                        Globals.ResourceManager.GetString("SnipForm"),
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                }
                else
                {
                    throw new InvalidOperationException("Couldn't register the hotkey: " + lastError);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Registers a hot key in the system.
        /// </summary>
        /// <param name="modifier">The modifiers that are associated with the hot key.</param>
        /// <param name="key">The key itself that is associated with the hot key.</param>
        public void RegisterHotkey(ModifierHookKeys modifier, Keys key)
        {
            // Increment the counter.
            this.currentId += 1;

            // Register the hot key.
            if (!UnsafeNativeMethods.RegisterHotKey(this.window.Handle, this.currentId, (uint)modifier, (uint)key))
            {
                // Disable messagebox for now. It will silently skip hotkeys that are already registered.
                // Some users may use some, but not all of the available, hotkeys and do not want to disable hotkeys.
                // This way it will fail silently instead of showing an annoying popup every time they start Snip.
                // A better solution will be needed in the future.

                /*
                 * int lastError = Marshal.GetLastWin32Error();
                 *
                 * if (lastError == 1409)
                 * {
                 *  // ERROR_HOTKEY_ALREADY_REGISTERED
                 *
                 *  // This is a temporary message until I can implement custom hotkeys.
                 *  // Even then some kind of message or notification will need to be shown to let the user
                 *  // know that the default or chosen hotkey will not work.
                 *  MessageBox.Show(
                 *      string.Format(
                 *          CultureInfo.InvariantCulture,
                 *          "This hotkey is already registered by another\n" +
                 *          "application and will be skipped:\n\n{0}, {1}",
                 *          modifier.ToString("G"),
                 *          key.ToString()),
                 *      LocalizedMessages.SnipForm,
                 *      MessageBoxButtons.OK,
                 *      MessageBoxIcon.Warning);
                 * }
                 * else
                 * {
                 *  throw new InvalidOperationException("Couldn't register the hotkey: " + lastError);
                 * }
                 */
            }
        }