Ejemplo n.º 1
0
        /// <summary>
        /// Called when a key is pressed.
        /// </summary>
        /// <param name="eventKey">The event key.</param>
        /// <returns></returns>
        protected override bool OnKeyPressEvent(EventKey eventKey)
        {
            // If we don't have a line buffer, don't do anything.
            if (LineBuffer == null)
            {
                return(false);
            }

            // Decompose the key into its components.
            ModifierType modifier;
            Key          key;

            GdkUtility.DecomposeKeys(eventKey, out key, out modifier);

            // Get the unicode character for this key.
            uint unicodeChar = Keyval.ToUnicode(eventKey.KeyValue);

            // Pass it on to the controller.
            return(controller.HandleKeyPress(key, modifier, unicodeChar));
        }
        /// <summary>
        /// Called when the <see cref="ActionManager"/> gets a key press and it passes
        /// it on to the current keybindings.
        /// </summary>
        /// <param name="e">The <see cref="Gtk.KeyPressEventArgs"/> instance
        /// containing the event data.</param>
        public void KeyPressed(KeyPressEventArgs e)
        {
            // Decompose the key into its components.
            ModifierType modifier;
            Key          key;

            GdkUtility.DecomposeKeys(e.Event, out key, out modifier);

            // Get the normalized string accelerator which we use for lookups.
            bool   isPartialAccelerator;
            string accelerator = GdkUtility.ToAcceleratorString(
                key, modifier, out isPartialAccelerator);

            if (isPartialAccelerator)
            {
                // If we are partial, just ignore it.
                return;
            }

            // Build a path with the current accelerator and the new one.
            var acceleratorPath = new HierarchicalPath(accelerator, currentAccelerator);

            // See if we have an accelerator for this path.
            if (!keybindings.Contains(acceleratorPath))
            {
                // Reset the current accelerator key.
                currentAccelerator = HierarchicalPath.AbsoluteRoot;

                // We couldn't find it so return false.
                e.RetVal = false;
                return;
            }

            // Grab the accelerator for this key. If we don't have an item, then
            // this is going to be a continued chain.
            string actionName = keybindings.Get(acceleratorPath);

            if (actionName == null)
            {
                // We are only the first part of an action chain.
                currentAccelerator = acceleratorPath;
                e.RetVal           = true;

                // TODO Fire an event to say incomplete accelerator.
                return;
            }

            // We have found a terminal action, so get the action involved.
            Action action = ActionManager.GetAction(actionName);

            if (action != null)
            {
                action.Activate();
                e.RetVal = true;
            }

            // Add it to the errors.
            ActionManager.Messages.Add(
                new SeverityMessage(
                    Severity.Error,
                    string.Format(
                        "Could not find action {0} to apply keybinding {1}.",
                        actionName,
                        acceleratorPath)));

            // We couldn't find the action so we did nothing.
            e.RetVal = false;
        }