Ejemplo n.º 1
0
 /// <summary>
 ///     General invocation handler
 /// </summary>
 /// <param name="hotKeyDelegate"></param>
 private void InvokeHotKeyHandler(HotKeyHandler hotKeyDelegate)
 {
     if (hotKeyDelegate != null)
     {
         hotKeyDelegate(this, new HotKeyArgs(DateTime.Now));
     }
 }
Ejemplo n.º 2
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(_hookId, nCode, wParam, lParam));
            }

            var vkCode  = Marshal.ReadInt32(lParam);
            var wmParam = (Wm)wParam;
            var isDown  = wmParam is Wm.Keydown or Wm.SysKeyDown;
            var isUp    = wmParam is Wm.Keyup or Wm.SysKeyUp;

            if (isDown || isUp)
            {
                var hotKey = HotKeyHandler.HasKey(vkCode);

                if (hotKey != null)
                {
                    Task.Run(() => HotKeyHandler.TryRunAction(hotKey, wmParam));
                    return(IntPtr.Parse("1"));
                }
            }

            if (isDown)
            {
                KeyDown?.Invoke(vkCode);
            }

            return(CallNextHookEx(_hookId, nCode, wParam, lParam));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 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>the hotkey number, -1 if failed</returns>
        public static int RegisterHotKey(string hotkeyString, HotKeyHandler handler)
        {
            if (hotkeyHWND == IntPtr.Zero)
            {
                LOG.Warn("hotkeyHWND not registered!");
                return(-1);
            }
            ModifierKeys modifiers = HotkeyModifiersFromString(hotkeyString);
            Key          key       = HotkeyFromString(hotkeyString);

            if (key == Key.None)
            {
                LOG.Warn("Trying to register a Keys.none hotkey, ignoring");
                return(0);
            }

            if (RegisterHotKey(hotkeyHWND, hotKeyCounter, (uint)modifiers, (uint)KeyInterop.VirtualKeyFromKey(key)))
            {
                keyHandlers.Add(hotKeyCounter, handler);
                return(hotKeyCounter++);
            }
            else
            {
                LOG.Warn(String.Format("Couldn't register hotkey modifier {0} key {1}", modifiers, key));
                return(-1);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// General invocation handler
 /// </summary>
 /// <param name="hotKeyDelegate"></param>
 private void InvokeHotKeyHandler(HotKeyHandler hotKeyDelegate)
 {
     if (hotKeyDelegate != null)
     {
         hotKeyDelegate(this, new EventArgs());
     }
 }
Ejemplo n.º 5
0
 private void InvokeHotKeyHandler(HotKeyHandler hotKeyDelegate)
 {
     if (hotKeyDelegate != null)
     {
         hotKeyDelegate(this);
     }
 }
Ejemplo n.º 6
0
        private void RegisterHandler(string spec, HotKeyHandler handler)
        {
            if (string.IsNullOrEmpty(spec))
            {
                return; //this can happen and is allowed => simply don't register
            }
            if (handler == null)
            {
                throw new ArgumentNullException();
            }

            int modifiers = 0, keyCode = 0;

            try {
                HotKeyMethods.TranslateStringToKeyValues(spec, out modifiers, out keyCode);
            }
            catch (ArgumentException) {
                //TODO: swallowed exception
                return;
            }

            var reg = HotKeyHandlerRegistration.Register(Form, keyCode, modifiers, handler);

            if (reg != null)
            {
                _handlers.Add(reg.RegistrationKey, reg);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 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>the hotkey number, -1 if failed</returns>
        public static int RegisterHotKey(Keys modifierKeyCode, Keys virtualKeyCode, HotKeyHandler handler)
        {
            if (virtualKeyCode == Keys.None)
            {
                LOG.Warn("Trying to register a Keys.none hotkey, ignoring");
                return(0);
            }
            // Convert Modifiers to fit HKM_SETHOTKEY
            uint modifiers = 0;

            if ((modifierKeyCode & Keys.Alt) > 0)
            {
                modifiers |= (uint)Modifiers.ALT;
            }
            if ((modifierKeyCode & Keys.Control) > 0)
            {
                modifiers |= (uint)Modifiers.CTRL;
            }
            if ((modifierKeyCode & Keys.Shift) > 0)
            {
                modifiers |= (uint)Modifiers.SHIFT;
            }

            if (RegisterHotKey(hotkeyHWND, hotKeyCounter, modifiers, (uint)virtualKeyCode))
            {
                keyHandlers.Add(hotKeyCounter, handler);
                return(hotKeyCounter++);
            }
            else
            {
                LOG.Warn(String.Format("Couldn't register hotkey modifier {0} virtualKeyCode {1}", modifierKeyCode, virtualKeyCode));
                return(-1);
            }
        }
Ejemplo n.º 8
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Set title to product name (from API)
            Title = Config.Active.ActiveLicense.ProductName;

            // Set footer to include version
            versionInfoTxt.Text = $"MultiTable Pro v{Assembly.GetEntryAssembly().GetName().Version}";

            // Set DataContext
            DataContext = Config.Active;

            // Refresh the profile list & select Active
            RefreshProfileList(selectActive: true);

            // Auto minimize
            if (Config.Active.AutoMinimize)
            {
                WindowState = WindowState.Minimized;
            }

            // Start watching open tables
            watchOpenTablesTimer = new Timer(WatchOpenTables, null, 1000, 1000);

            // Register hotkeys
            IntPtr hWnd = new WindowInteropHelper(this).Handle;

            HotKeyHandler.RegisterHotKey(Config.Active.AsideHotKey, hWnd);
            ComponentDispatcher.ThreadFilterMessage += new ThreadMessageEventHandler(HotKeyHandler.HotkeyPressed);

            // Listen for license expiration events
            Config.Active.ActiveLicense.ExpirationEvent += ActiveLicense_ExpirationEvent;
        }
Ejemplo n.º 9
0
 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     Logger.Log("Clean shutdown");
     Thread.Sleep(100);
     App.Current.Properties["IsRunning"] = false;
     Hide();
     HotKeyHandler.UnregisterAllHotkeys();
     Thread.Sleep(2000); // Give threaded loops a few seconds to finish up
 }
Ejemplo n.º 10
0
        private static bool RegisterWrapper(StringBuilder failedKeys, HotKeyHandler handler)
        {
            bool success = RegisterHotkey(
                failedKeys,
                Settings.Default.HotKey,
                handler);

            return(success);
        }
Ejemplo n.º 11
0
        public static void Init(MainWindow window)
        {
            actionPlay = new Action <object>((o) => window.playerPlay_MouseDown(window, null));
            actionNext = new Action <object>((o) => window.playerNext_MouseDown(window, null));
            actionPrev = new Action <object>((o) => window.playerPrev_MouseDown(window, null));

            actionVolUp   = new Action <object>((o) => window.ChangeVolume(Player.Volume + (float)o));
            actionVolDown = new Action <object>((o) => window.ChangeVolume(Player.Volume - (float)o));

            handler = new HotKeyHandler(window);
            Update();
        }
Ejemplo n.º 12
0
        private void SetDefaultConfig()
        {
            config.SetConfig(ConfigKey.JumpTime, 2000);
            config.SetConfig(ConfigKey.PauseTime, 1000);
            config.SetConfig(ConfigKey.Topmost, false);
            config.SetConfig(ConfigKey.IntelligentPlaying, true);
            HotKeyHandler playpause = PlayPause;
            HotKeyHandler jumpbck   = JumpBackward;

            config.AddHotKeyHandler(new HotKey(Forms.Keys.RControlKey, functions[playpause.Method.Name]));
            config.AddHotKeyHandler(new HotKey(Forms.Keys.LControlKey, functions[jumpbck.Method.Name]));
        }
Ejemplo n.º 13
0
        private void KeyDownHandler(object sender, Forms.KeyEventArgs e)
        {
            var hotkeys = config.HotKeys;

            Forms.Keys key = e.KeyCode;
            intelligentPlayingManager.KeyPressed(key);
            HotKeyHandler handler = config.GetHandler(key);

            if (handler != null)
            {
                handler();
            }
        }
Ejemplo n.º 14
0
        public void Toggle(List <IAction> nodePipe, HotKey hotKey)
        {
            var state = HotKeyHandler.GetState(hotKey.Key);

            RunnerState = !RunnerState;

            Task.Run(() => {
                while (RunnerState)
                {
                    if (state is KeyStates.Hold or KeyStates.Down)
                    {
                        Console.WriteLine("here");
                        Run(nodePipe);
                    }
Ejemplo n.º 15
0
            private HotKeyHandlerRegistration(IntPtr hwnd, int key, HotKeyHandler handler)
            {
                if (hwnd == IntPtr.Zero)
                {
                    throw new ArgumentException();
                }
                if (handler == null)
                {
                    throw new ArgumentNullException();
                }

                _hwnd           = hwnd;
                RegistrationKey = key;
                Handler         = handler;
            }
        /// <summary>
        /// Initialize a hotkey handler and HotKeys
        /// </summary>
        private void InitHotKeys()
        {
            this.HotkeyHandler = HotKeyHandler.GetHotkeyHandler(new WindowInteropHelper(this).Handle);

            SetHotKeyForModeSwitch();
            SetHotKeyForToggleRecord();
            SetHotKeyForTogglePause();

            SetHotKeyForActivatingMainWindow();

            SetHotKeyForMoveToParent();
            SetHotKeyForMoveToFirstChild();
            SetHotKeyForMoveToLastChild();
            SetHotKeyForMoveToNextSibbling();
            SetHotKeyForMoveToPreviousSibbling();
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Sets the hotkey handler, only one can be set
 /// </summary>
 /// <param name="hndl"></param>
 public static void SetHotkeyHandler(HotKeyHandler hndl)
 {
     ConsoleBase.hndl = hndl;
     hotkey           = hndl != null;
     if (hotkey)
     {
         if (thd != null)
         {
             return;
         }
         thd = new Thread(Thd);
         thd.Start();
     }
     else
     {
         NativeClass.NullThread(ref thd);
     }
 }
Ejemplo n.º 18
0
        /// <summary>
        /// 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>the hotkey number, -1 if failed</returns>
        public static int RegisterHotKey(Keys modifierKeyCode, Keys virtualKeyCode, HotKeyHandler handler)
        {
            if (virtualKeyCode == Keys.None)
            {
                LOG.Warn("Trying to register a Keys.none hotkey, ignoring");
                return(0);
            }
            // Convert Modifiers to fit HKM_SETHOTKEY
            uint modifiers = 0;

            if ((modifierKeyCode & Keys.Alt) > 0)
            {
                modifiers |= (uint)Modifiers.ALT;
            }
            if ((modifierKeyCode & Keys.Control) > 0)
            {
                modifiers |= (uint)Modifiers.CTRL;
            }
            if ((modifierKeyCode & Keys.Shift) > 0)
            {
                modifiers |= (uint)Modifiers.SHIFT;
            }
            if (modifierKeyCode == Keys.LWin || modifierKeyCode == Keys.RWin)
            {
                modifiers |= (uint)Modifiers.WIN;
            }
            // Disable repeating hotkey for Windows 7 and beyond, as described in #1559
            if (isWindows7OrOlder)
            {
                modifiers |= (uint)Modifiers.NO_REPEAT;
            }
            if (RegisterHotKey(hotkeyHWND, hotKeyCounter, modifiers, (uint)virtualKeyCode))
            {
                keyHandlers.Add(hotKeyCounter, handler);
                return(hotKeyCounter++);
            }
            else
            {
                LOG.Warn(String.Format("Couldn't register hotkey modifier {0} virtualKeyCode {1}", modifierKeyCode, virtualKeyCode));
                return(-1);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        ///     Register a hotkey
        /// </summary>
        /// <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>the hotkey number, -1 if failed</returns>
        public static int RegisterHotKey(Keys modifierKeyCode, Keys virtualKeyCode, HotKeyHandler handler)
        {
            if (virtualKeyCode == Keys.None)
            {
                Log.Warn().WriteLine("Trying to register a Keys.none hotkey, ignoring");
                return(0);
            }
            // Convert Modifiers to fit HKM_SETHOTKEY
            uint modifiers = 0;

            if ((modifierKeyCode & Keys.Alt) > 0)
            {
                modifiers |= (uint)Modifiers.ALT;
            }
            if ((modifierKeyCode & Keys.Control) > 0)
            {
                modifiers |= (uint)Modifiers.CTRL;
            }
            if ((modifierKeyCode & Keys.Shift) > 0)
            {
                modifiers |= (uint)Modifiers.SHIFT;
            }
            if (modifierKeyCode == Keys.LWin || modifierKeyCode == Keys.RWin)
            {
                modifiers |= (uint)Modifiers.WIN;
            }
            // Disable repeating hotkey for Windows 7 and beyond, as described in #1559
            if (!WindowsVersion.IsWindows7OrLater)
            {
                modifiers |= (uint)Modifiers.NO_REPEAT;
            }
            if (RegisterHotKey(_hotkeyHwnd, _hotKeyCounter, modifiers, (uint)virtualKeyCode))
            {
                KeyHandlers.Add(_hotKeyCounter, handler);
                return(_hotKeyCounter++);
            }
            Log.Warn().WriteLine($"Couldn't register hotkey modifier {modifierKeyCode} virtualKeyCode {virtualKeyCode}");
            return(-1);
        }
Ejemplo n.º 20
0
        private static bool RegisterWrapper(StringBuilder failedKeys, HotKeyHandler handler, bool ignoreFailedRegistration)
        {
#warning todo with Properties.Settings.Default.HotKey
            //IniValue hotkeyValue = _conf.Values[configurationKey];
            try
            {
                bool success = RegisterHotkey(failedKeys,
                                              //hotkeyValue.Value.ToString(),
                                              Properties.Settings.Default.HotKey,
                                              handler);
                if (!success && ignoreFailedRegistration)
                {
#warning logging
                    //LOG.DebugFormat("Ignoring failed hotkey registration for {0}, with value '{1}', resetting to 'None'.", functionName, hotkeyValue);
#warning todo with Properties.Settings.Default.HotKey
                    //_conf.Values[configurationKey].Value = Keys.None.ToString();
                    //_conf.IsDirty = true;
                }
                return(success);
            }
            catch (Exception)
            {
#warning logging
                //LOG.Warn(ex);
                //LOG.WarnFormat("Restoring default hotkey for {0}, stored under {1} from '{2}' to '{3}'", functionName, configurationKey, hotkeyValue.Value, hotkeyValue.Attributes.DefaultValue);

                // when getting an exception the key wasn't found: reset the hotkey value
                //hotkeyValue.UseValueOrDefault(null);
                //hotkeyValue.ContainingIniSection.IsDirty = true;
#warning todo set default Properties.Settings.Default.HotKey =
                return(RegisterHotkey(failedKeys,
                                      //hotkeyValue.Value.ToString(),
                                      Properties.Settings.Default.HotKey,
                                      handler));
            }
        }
Ejemplo n.º 21
0
        public ISensor Build()
        {
            var parser   = new SettingsParser(_parameters, _sockets);
            var settings = parser.Parse();

            var translation = new Translation();
            var scheduler   = new Scheduler();

            var builder = new TrayIconMenuBuilder(translation);

            foreach (var socket in _sockets)
            {
                builder.AddSocketStrip(socket.Key);
            }
            builder.AddSeparatorStrip();
            builder.AddModeStrip(_modes);
            builder.AddSeparatorStrip();
            builder.AddExitStrip();
            var trayIcon = builder.Build();

            var hotKeyHandler = new HotKeyHandler(new HotKeyNotifier(), settings.HotKeys);

            return(new GUI(translation, scheduler, trayIcon, hotKeyHandler));
        }
Ejemplo n.º 22
0
            /// <summary>
            /// Registers a new hotkey and returns a handle to the registration.
            /// </summary>
            /// <returns>Returns null on failure.</returns>
            public static HotKeyHandlerRegistration Register(Form owner, int keyCode, int modifiers, HotKeyHandler handler)
            {
                var key = ++_lastUsedKey;

                if (!HotKeyMethods.RegisterHotKey(owner.Handle, key, modifiers, keyCode)) {
                    Log.Write("Failed to create hotkey on key {0} with modifiers {1}", keyCode, modifiers);
                    return null;
                }

                return new HotKeyHandlerRegistration(owner.Handle, key, handler);
            }
Ejemplo n.º 23
0
		public static int RegisterHotKey(string hotkey, HotKeyHandler handler) {
			return RegisterHotKey(HotkeyModifiersFromString(hotkey), HotkeyFromString(hotkey),handler);
		}
Ejemplo n.º 24
0
		/// <summary>
		/// 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>the hotkey number, -1 if failed</returns>
		public static int RegisterHotKey(Keys modifierKeyCode, Keys virtualKeyCode, HotKeyHandler handler) {
			if (virtualKeyCode == Keys.None) {
				LOG.Warn("Trying to register a Keys.none hotkey, ignoring");
				return 0;
			}
			// Convert Modifiers to fit HKM_SETHOTKEY
			uint modifiers = 0;
			if ((modifierKeyCode & Keys.Alt) > 0) {
				modifiers |= (uint)Modifiers.ALT;
			}
			if ((modifierKeyCode & Keys.Control) > 0) {
				modifiers |= (uint)Modifiers.CTRL;
			}
			if ((modifierKeyCode & Keys.Shift) > 0) {
				modifiers |= (uint)Modifiers.SHIFT;
			}
			if (modifierKeyCode == Keys.LWin || modifierKeyCode == Keys.RWin) {
				modifiers |= (uint)Modifiers.WIN;
			}
			// Disable repeating hotkey for Windows 7 and beyond, as described in #1559
			if (isWindows7OrOlder) {
				modifiers |= (uint)Modifiers.NO_REPEAT;
			}
			if (RegisterHotKey(hotkeyHWND, hotKeyCounter, modifiers, (uint)virtualKeyCode)) {
				keyHandlers.Add(hotKeyCounter, handler);
				return hotKeyCounter++;
			} else {
				LOG.Warn(String.Format("Couldn't register hotkey modifier {0} virtualKeyCode {1}", modifierKeyCode, virtualKeyCode));
				return -1;
			}
		}
Ejemplo n.º 25
0
        /// <summary>
        /// Helper method to cleanly register a hotkey.
        /// </summary>
        /// <param name="failedKeys">failedKeys.</param>
        /// <param name="hotkeyString">hotkeyString.</param>
        /// <param name="handler">handler.</param>
        /// <returns>bool success.</returns>
        private static bool RegisterHotkey(StringBuilder failedKeys, string hotkeyString, HotKeyHandler handler)
        {
            Keys modifierKeyCode = HotkeyModifiersFromString(hotkeyString);
            Keys virtualKeyCode  = HotkeyFromString(hotkeyString);

            if (!Keys.None.Equals(virtualKeyCode))
            {
                if (RegisterHotKey(modifierKeyCode, virtualKeyCode, handler) < 0)
                {
                    if (failedKeys.Length > 0)
                    {
                        failedKeys.Append(", ");
                    }

                    failedKeys.Append(hotkeyString);
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 26
0
        private bool RegisterWrapper(StringBuilder failedKeys, string functionName, string configurationKey, HotKeyHandler handler, bool ignoreFailedRegistration)
        {
            var hotkeyValue = _coreConfiguration[configurationKey];

            try
            {
                var success = RegisterHotkey(failedKeys, functionName, hotkeyValue.Value.ToString(), handler);
                if (!success && ignoreFailedRegistration)
                {
                    Log.Debug().WriteLine("Ignoring failed hotkey registration for {0}, with value '{1}', resetting to 'None'.", functionName, hotkeyValue);
                    _coreConfiguration[configurationKey].Value = Keys.None.ToString();
                }
                return(success);
            }
            catch (Exception ex)
            {
                Log.Warn().WriteLine(ex);
                Log.Warn().WriteLine("Restoring default hotkey for {0}, stored under {1} from '{2}' to '{3}'", functionName, configurationKey, hotkeyValue.Value,
                                     hotkeyValue.DefaultValue);
                // when getting an exception the key wasn't found: reset the hotkey value
                hotkeyValue.ResetToDefault();
                return(RegisterHotkey(failedKeys, functionName, hotkeyValue.Value.ToString(), handler));
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        ///     Helper method to cleanly register a hotkey
        /// </summary>
        /// <param name="failedKeys"></param>
        /// <param name="functionName"></param>
        /// <param name="hotkeyString"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        private static bool RegisterHotkey(StringBuilder failedKeys, string functionName, string hotkeyString, HotKeyHandler handler)
        {
            var modifierKeyCode = HotkeyControl.HotkeyModifiersFromString(hotkeyString);
            var virtualKeyCode  = HotkeyControl.HotkeyFromString(hotkeyString);

            if (!Keys.None.Equals(virtualKeyCode))
            {
                if (HotkeyControl.RegisterHotKey(modifierKeyCode, virtualKeyCode, handler) < 0)
                {
                    Log.Debug().WriteLine("Failed to register {0} to hotkey: {1}", functionName, hotkeyString);
                    if (failedKeys.Length > 0)
                    {
                        failedKeys.Append(", ");
                    }
                    failedKeys.Append(hotkeyString);
                    return(false);
                }
                Log.Debug().WriteLine("Registered {0} to hotkey: {1}", functionName, hotkeyString);
            }
            else
            {
                Log.Info().WriteLine("Skipping hotkey registration for {0}, no hotkey set!", functionName);
            }
            return(true);
        }
Ejemplo n.º 28
0
 public static int RegisterHotKey(string hotkey, HotKeyHandler handler)
 {
     return(RegisterHotKey(HotkeyModifiersFromString(hotkey), HotkeyFromString(hotkey), handler));
 }
Ejemplo n.º 29
0
 /// <summary>
 /// General invocation handler
 /// </summary>
 /// <param name="hotKeyDelegate"></param>
 private void InvokeHotKeyHandler( HotKeyHandler hotKeyDelegate )
 {
     if ( hotKeyDelegate != null )
         hotKeyDelegate( this, new HotKeyArgs( DateTime.Now ) );
 }
Ejemplo n.º 30
0
 private void InvokeHotKeyHandler( HotKeyHandler hotKeyDelegate )
 {
     if ( hotKeyDelegate != null )
         hotKeyDelegate( this );
 }
Ejemplo n.º 31
0
            /// <summary>
            /// Registers a new hotkey and returns a handle to the registration.
            /// </summary>
            /// <returns>Returns null on failure.</returns>
            public static HotKeyHandlerRegistration Register(Form owner, int keyCode, int modifiers, HotKeyHandler handler)
            {
                var key = ++_lastUsedKey;

                if (!HotKeyMethods.RegisterHotKey(owner.Handle, key, modifiers, keyCode))
                {
                    Log.Write("Failed to create hotkey on key {0} with modifiers {1}", keyCode, modifiers);
                    return(null);
                }

                return(new HotKeyHandlerRegistration(owner.Handle, key, handler));
            }
Ejemplo n.º 32
0
            /// <summary>
            /// Registers a new hotkey and returns a handle to the registration.
            /// </summary>
            /// <returns>Returns null on failure.</returns>
            public static HotKeyHandlerRegistration Register(Form owner, int keyCode, int modifiers, HotKeyHandler handler)
            {
                var key = ++_lastUsedKey;

                if (!HotKeyMethods.RegisterHotKey(owner.Handle, key, modifiers, keyCode)) {
                    Console.Error.WriteLine("Failed to create hotkey on keys {0}.", keyCode);
                    return null;
                }

                return new HotKeyHandlerRegistration(owner.Handle, key, handler);
            }
Ejemplo n.º 33
0
            private HotKeyHandlerRegistration(IntPtr hwnd, int key, HotKeyHandler handler)
            {
                if (hwnd == IntPtr.Zero)
                    throw new ArgumentException();
                if (handler == null)
                    throw new ArgumentNullException();

                _hwnd = hwnd;
                RegistrationKey = key;
                Handler = handler;
            }
Ejemplo n.º 34
0
        private void RegisterHandler(string spec, HotKeyHandler handler)
        {
            if (string.IsNullOrEmpty(spec))
                return; //this can happen and is allowed => simply don't register
            if (handler == null)
                throw new ArgumentNullException();

            int modifiers = 0, keyCode = 0;

            try {
                HotKeyMethods.TranslateStringToKeyValues(spec, out modifiers, out keyCode);
            }
            catch (ArgumentException) {
                //TODO: swallowed exception
                return;
            }

            var reg = HotKeyHandlerRegistration.Register(Form, keyCode, modifiers, handler);
            if(reg != null)
                _handlers.Add(reg.RegistrationKey, reg);
        }
Ejemplo n.º 35
0
 private static bool RegisterWrapper(StringBuilder failedKeys, string functionName, string configurationKey, HotKeyHandler handler, bool ignoreFailedRegistration)
 {
     IniValue hotkeyValue = _conf.Values[configurationKey];
     try {
         bool success = RegisterHotkey(failedKeys, functionName, hotkeyValue.Value.ToString(), handler);
         if (!success && ignoreFailedRegistration) {
             LOG.DebugFormat("Ignoring failed hotkey registration for {0}, with value '{1}', resetting to 'None'.", functionName, hotkeyValue);
             _conf.Values[configurationKey].Value = Keys.None.ToString();
             _conf.IsDirty = true;
         }
         return success;
     } catch (Exception ex) {
         LOG.Warn(ex);
         LOG.WarnFormat("Restoring default hotkey for {0}, stored under {1} from '{2}' to '{3}'", functionName, configurationKey, hotkeyValue.Value, hotkeyValue.Attributes.DefaultValue);
         // when getting an exception the key wasn't found: reset the hotkey value
         hotkeyValue.UseValueOrDefault(null);
         hotkeyValue.ContainingIniSection.IsDirty = true;
         return RegisterHotkey(failedKeys, functionName, hotkeyValue.Value.ToString(), handler);
     }
 }
Ejemplo n.º 36
0
        /// <summary>
        /// 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>the hotkey number, -1 if failed</returns>
        public static int RegisterHotKey(Keys modifierKeyCode, Keys virtualKeyCode, HotKeyHandler handler)
        {
            if (virtualKeyCode == Keys.None) {
                LOG.Warn("Trying to register a Keys.none hotkey, ignoring");
                return 0;
            }
            // Convert Modifiers to fit HKM_SETHOTKEY
            uint modifiers = 0;
            if ((modifierKeyCode & Keys.Alt) > 0) {
                modifiers |= (uint)Modifiers.ALT;
            }
            if ((modifierKeyCode & Keys.Control) > 0) {
                modifiers |= (uint)Modifiers.CTRL;
            }
            if ((modifierKeyCode & Keys.Shift) > 0) {
                modifiers |= (uint)Modifiers.SHIFT;
            }

            if (RegisterHotKey(hotkeyHWND, hotKeyCounter, modifiers, (uint)virtualKeyCode)) {
                keyHandlers.Add(hotKeyCounter, handler);
                return hotKeyCounter++;
            } else {
                LOG.Warn(String.Format("Couldn't register hotkey modifier {0} virtualKeyCode {1}", modifierKeyCode, virtualKeyCode));
                return -1;
            }
        }
Ejemplo n.º 37
0
        public override int?Run(AugmentrexContext context, string[] args)
        {
            var opts = Parse <KeyOptions>(context, args);

            if (opts == null)
            {
                return(null);
            }

            if (!opts.Add && !opts.Delete)
            {
                foreach (var kvp in _bindings)
                {
                    context.InfoLine("{0} = {1}", kvp.Key, kvp.Value);
                }

                return(null);
            }

            var info = new HotKeyInfo(opts.Key, opts.Alt, opts.Control, opts.Shift);

            if (_handler == null)
            {
                void KeyHandler(HotKeyInfo info)
                {
                    var freq = context.Configuration.HotKeyBeepFrequency;

                    if (freq != 0)
                    {
                        context.Ipc.Channel.Beep(freq, context.Configuration.HotKeyBeepDuration);
                    }

                    if (_bindings.TryGetValue(info, out var command))
                    {
                        context.Interpreter.RunCommand(command, true);
                    }
                }

                _handler = new HotKeyHandler(KeyHandler);
            }

            if (opts.Add)
            {
                var command = string.Join(" ", opts.Fragments);

                if (string.IsNullOrWhiteSpace(command))
                {
                    context.ErrorLine("No command given.");

                    return(null);
                }

                if (_bindings.TryAdd(info, command))
                {
                    context.HotKeys.Add(info, _handler);

                    context.InfoLine("Added key binding: {0} = {1}", info, command);
                }
                else
                {
                    context.ErrorLine("Key binding already exists: {0} = {1}", info, _bindings[info]);
                }
            }

            if (opts.Delete)
            {
                if (_bindings.Remove(info, out var command))
                {
                    context.InfoLine("Deleted key binding: {0} = {1}", info, command);
                }
                else
                {
                    context.ErrorLine("Key binding not found: {0}", info);
                }
            }

            return(null);
        }
Ejemplo n.º 38
0
        /// <summary>
        /// Helper method to cleanly register a hotkey
        /// </summary>
        /// <param name="failedKeys"></param>
        /// <param name="functionName"></param>
        /// <param name="hotkeyString"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        private static bool RegisterHotkey(StringBuilder failedKeys, string hotkeyString, HotKeyHandler handler)
        {
            Keys modifierKeyCode = HotkeyControl.HotkeyModifiersFromString(hotkeyString);
            Keys virtualKeyCode  = HotkeyControl.HotkeyFromString(hotkeyString);

            if (!Keys.None.Equals(virtualKeyCode))
            {
                if (HotkeyControl.RegisterHotKey(modifierKeyCode, virtualKeyCode, handler) < 0)
                {
#warning logging
                    //LOG.DebugFormat("Failed to register {0} to hotkey: {1}", functionName, hotkeyString);
                    if (failedKeys.Length > 0)
                    {
                        failedKeys.Append(", ");
                    }
                    failedKeys.Append(hotkeyString);
                    return(false);
                }
#warning logging
                //LOG.DebugFormat("Registered {0} to hotkey: {1}", functionName, hotkeyString);
            }
            else
            {
#warning logging
                //LOG.InfoFormat("Skipping hotkey registration for {0}, no hotkey set!", functionName);
            }
            return(true);
        }
Ejemplo n.º 39
0
 /// <summary>
 /// Helper method to cleanly register a hotkey
 /// </summary>
 /// <param name="failedKeys"></param>
 /// <param name="functionName"></param>
 /// <param name="hotkeyString"></param>
 /// <param name="handler"></param>
 /// <returns></returns>
 private static bool RegisterHotkey(StringBuilder failedKeys, string functionName, string hotkeyString, HotKeyHandler handler)
 {
     Keys modifierKeyCode = HotkeyControl.HotkeyModifiersFromString(hotkeyString);
     Keys virtualKeyCode = HotkeyControl.HotkeyFromString(hotkeyString);
     if (!Keys.None.Equals(virtualKeyCode)) {
         if (HotkeyControl.RegisterHotKey(modifierKeyCode, virtualKeyCode, handler) < 0) {
             LOG.DebugFormat("Failed to register {0} to hotkey: {1}", functionName, hotkeyString);
             if (failedKeys.Length > 0) {
                 failedKeys.Append(", ");
             }
             failedKeys.Append(hotkeyString);
             return false;
         }
         LOG.DebugFormat("Registered {0} to hotkey: {1}", functionName, hotkeyString);
     } else {
         LOG.InfoFormat("Skipping hotkey registration for {0}, no hotkey set!", functionName);
     }
     return true;
 }
Ejemplo n.º 40
0
 public Function(HotKeyHandler Handler, string FunctionText)
 {
     this.KeyHandler   = Handler;
     this.FunctionText = FunctionText;
 }