Example #1
0
 /// <summary>
 /// Gets the individual key that was pressed in the given key stroke without any modifier flags.
 /// </summary>
 /// <param name="keyStroke">The key stroke from which the pressed key is to be extracted.</param>
 /// <param name="control">True if the <see cref="XKeys.Control"/> modifier was pressed; False otherwise.</param>
 /// <param name="alt">True if the <see cref="XKeys.Alt"/> modifier was pressed; False otherwise.</param>
 /// <param name="shift">True if the <see cref="XKeys.Shift"/> modifier was pressed; False otherwise.</param>
 /// <returns>A single value from the <see cref="XKeys"/> enumeration excluding
 /// <see cref="XKeys.Control"/>, <see cref="XKeys.Alt"/> and <see cref="XKeys.Shift"/> (i.e. not a bitwise combination).</returns>
 protected static XKeys GetKeyPressed(XKeys keyStroke, out bool control, out bool alt, out bool shift)
 {
     control = IsKeyPressModifiedByControl(keyStroke);
     alt     = IsKeyPressModifiedByAlt(keyStroke);
     shift   = IsKeyPressModifiedByShift(keyStroke);
     return(GetKeyPressed(keyStroke));
 }
Example #2
0
        public void TestEmptyStringParse()
        {
            // empty strings should parse to XKeys.None
            XKeys keyCode = (XKeys)_converter.ConvertFromString(string.Empty);

            Assert.AreEqual(XKeys.None, keyCode, "empty strings should parse to XKeys.None");
        }
Example #3
0
        public void TestSpecialCase()
        {
            // test special case where the key separator is a key name on its own
            const string message       = "Special Case";
            const XKeys  key           = XKeys.OemPlus;
            CultureInfo  culture       = _dummyCulture;
            string       actualKeyName = XKeysConverter.KeySeparator.ToString();

            AssertEquivalency(string.Format("{0}", actualKeyName), (XKeys)key, culture, message);
            AssertEquivalency(string.Format("Control+{0}", actualKeyName), XKeys.Control | (XKeys)key, culture, message);
            AssertEquivalency(string.Format("Alt+{0}", actualKeyName), XKeys.Alt | (XKeys)key, culture, message);
            AssertEquivalency(string.Format("Shift+{0}", actualKeyName), XKeys.Shift | (XKeys)key, culture, message);
            AssertEquivalency(string.Format("Control+Alt+{0}", actualKeyName), XKeys.Control | XKeys.Alt | (XKeys)key, culture, message);
            AssertEquivalency(string.Format("Alt+Shift+{0}", actualKeyName), XKeys.Alt | XKeys.Shift | (XKeys)key, culture, message);
            AssertEquivalency(string.Format("Control+Shift+{0}", actualKeyName), XKeys.Control | XKeys.Shift | (XKeys)key, culture, message);
            AssertEquivalency(string.Format("Control+Alt+Shift+{0}", actualKeyName), XKeys.Control | XKeys.Alt | XKeys.Shift | (XKeys)key, culture, message);

            AssertStringParse(string.Format("{0}   ", actualKeyName), (XKeys)key, culture, message);
            AssertStringParse(string.Format("   {0}", actualKeyName), (XKeys)key, culture, message);
            AssertStringParse(string.Format("   {0}   ", actualKeyName), (XKeys)key, culture, message);
            AssertStringParse(string.Format("Control    +   {0}", actualKeyName), XKeys.Control | (XKeys)key, culture, message);
            AssertStringParse(string.Format("Control+     {0}", actualKeyName), XKeys.Control | (XKeys)key, culture, message);
            AssertStringParse(string.Format("{0}+Control", actualKeyName), XKeys.Control | (XKeys)key, culture, message);
            AssertStringParse(string.Format("{0}    +Control", actualKeyName), XKeys.Control | (XKeys)key, culture, message);
            AssertStringParse(string.Format("Alt+{0}+Control", actualKeyName), XKeys.Control | XKeys.Alt | (XKeys)key, culture, message);
            AssertStringParse(string.Format("Alt+{0}   +Control", actualKeyName), XKeys.Control | XKeys.Alt | (XKeys)key, culture, message);
            AssertStringParse(string.Format("Alt+    {0}+Control", actualKeyName), XKeys.Control | XKeys.Alt | (XKeys)key, culture, message);
            AssertStringParse(string.Format("Alt+    {0}   +Control", actualKeyName), XKeys.Control | XKeys.Alt | (XKeys)key, culture, message);
        }
Example #4
0
        /// <summary>
        /// Tests whether or not a given key combination is a valid key stroke by invoking the <see cref="ValidateKeyStroke"/> event.
        /// </summary>
        /// <param name="keyStroke">The key combination to be tested.</param>
        /// <returns>True if the key combination is a valid key stroke; False otherwise.</returns>
        protected bool IsValidKeyStroke(XKeys keyStroke)
        {
            ValidateKeyStrokeEventArgs e = new ValidateKeyStrokeEventArgs(keyStroke);

            this.OnValidateKeyStroke(e);
            return(e.IsValid);
        }
Example #5
0
        public void TestInvariantKeyCombos()
        {
            // test combining keys with modifiers in the invariant case
            const string message = "Invariant Combinations";
            CultureInfo  culture = CultureInfo.InvariantCulture;

            foreach (string keyName in Enum.GetNames(typeof(XKeys)))
            {
                switch (keyName)
                {
                case "Modifiers":
                case "KeyCode":
                case "None":
                case "Control":
                case "Shift":
                case "Alt":
                    // these aren't real keys anyway
                    continue;
                }

                XKeys  key           = (XKeys)Enum.Parse(typeof(XKeys), keyName);
                string actualKeyName = _converter.ConvertToString(null, culture, key);
                AssertEquivalency(string.Format("Ctrl+{0}", actualKeyName), XKeys.Control | (XKeys)key, culture, message);
                AssertEquivalency(string.Format("Alt+{0}", actualKeyName), XKeys.Alt | (XKeys)key, culture, message);
                AssertEquivalency(string.Format("Shift+{0}", actualKeyName), XKeys.Shift | (XKeys)key, culture, message);
                AssertEquivalency(string.Format("Ctrl+Alt+{0}", actualKeyName), XKeys.Control | XKeys.Alt | (XKeys)key, culture, message);
                AssertEquivalency(string.Format("Alt+Shift+{0}", actualKeyName), XKeys.Alt | XKeys.Shift | (XKeys)key, culture, message);
                AssertEquivalency(string.Format("Ctrl+Shift+{0}", actualKeyName), XKeys.Control | XKeys.Shift | (XKeys)key, culture, message);
                AssertEquivalency(string.Format("Ctrl+Alt+Shift+{0}", actualKeyName), XKeys.Control | XKeys.Alt | XKeys.Shift | (XKeys)key, culture, message);
            }
        }
Example #6
0
        private void AssertStringFormat(string sKeys, XKeys eKeys, CultureInfo culture, string message)
        {
            string actualString = _converter.ConvertToString(null, culture, eKeys);

            //System.Diagnostics.Trace.WriteLine(actualString);
            Assert.AreEqual(sKeys, actualString, message + ": converting " + (int)eKeys + " which is " + eKeys.ToString());
        }
Example #7
0
        private void AssertStringParse(string sKeys, XKeys eKeys, CultureInfo culture, string message)
        {
            XKeys actualEnum = (XKeys)_converter.ConvertFromString(null, culture, sKeys);

            //System.Diagnostics.Trace.WriteLine(actualEnum);
            Assert.AreEqual((int)eKeys, (int)actualEnum, message + ": converting " + sKeys + " which is " + actualEnum.ToString());
        }
Example #8
0
        private void HandleTextBoxKeyDown(object sender, KeyEventArgs e)
        {
            XKeys keyData = Convert(e.KeyData);

            e.Handled          = true;
            e.SuppressKeyPress = true;

            // if control is read-only, ignore any user input
            if (this.ReadOnly)
            {
                return;
            }

            if (!_keyStrokeAccepted)
            {
                this.UpdateText(keyData);
            }

            XKeys key = GetKeyPressed(keyData);

            if (key != XKeys.None && !_currentKeys.Contains(key))
            {
                _currentKeys.Add(key);
            }
        }
Example #9
0
        /// <summary>
        /// Formats a key stroke as a human-readable string.
        /// </summary>
        /// <remarks>
        /// The default implementation separates modifiers from the key pressed,
        /// translates each modifier/key individually using <see cref="GetKeyName"/>,
        /// and combines them together using <see cref="KeySeparator"/>.
        /// A trailing <see cref="KeySeparator"/> is shown if this particular key stroke
        /// is not valid as determined by <see cref="IsValidKeyStroke"/>.
        /// A key stroke of <see cref="XKeys.None"/> is formatted as an empty string.
        /// </remarks>
        /// <param name="keyStroke">The key stroke to be formatted.</param>
        /// <returns>A human-readable string representing the key stroke.</returns>
        protected virtual string FormatKeyStroke(XKeys keyStroke)
        {
            if (keyStroke == 0)
            {
                return(string.Empty);
            }

            bool  control, alt, shift;
            bool  valid = IsValidKeyStroke(keyStroke);
            XKeys key   = GetKeyPressed(keyStroke, out control, out alt, out shift);

            List <string> keys = new List <string>(4);

            if (control)
            {
                keys.Add(GetKeyName(XKeys.Control));
            }
            if (alt)
            {
                keys.Add(GetKeyName(XKeys.Alt));
            }
            if (shift)
            {
                keys.Add(GetKeyName(XKeys.Shift));
            }

            // display a trailing '+' if the keystroke is invalid
            keys.Add(valid ? GetKeyName(key) : string.Empty);

            return(string.Join(_keySeparator ?? string.Empty, keys.ToArray()));
        }
Example #10
0
		public void TestInvalidCastToXKeys()
		{
			try
			{
				XKeys keys = (XKeys) new XMouseButtonCombo(XMouseButtons.Left | XMouseButtons.Right, ModifierFlags.None);
				Assert.Fail("Casting a multi-button combo as an XKeys should be invalid");
			}
			catch (InvalidCastException) {}
		}
Example #11
0
        public void Load(XmlElement e)
        {
            foreach (var item in e)
            {
                if (item.Name == "XKeys")
                {
                    XmlElement keys = item[KeyframeDataCollection.ElementName];
                    if (keys != null)
                    {
                        XKeys.Load(keys);
                    }
                }
                else if (item.Name == "YKeys")
                {
                    XmlElement keys = item[KeyframeDataCollection.ElementName];
                    if (keys != null)
                    {
                        YKeys.Load(keys);
                    }
                }
                else if (item.Name == "ZKeys")
                {
                    XmlElement keys = item[KeyframeDataCollection.ElementName];
                    if (keys != null)
                    {
                        ZKeys.Load(keys);
                    }
                }


                if (item.Name == "RXKeys")
                {
                    XmlElement keys = item[KeyframeDataCollection.ElementName];
                    if (keys != null)
                    {
                        RXKeys.Load(keys);
                    }
                }
                else if (item.Name == "RYKeys")
                {
                    XmlElement keys = item[KeyframeDataCollection.ElementName];
                    if (keys != null)
                    {
                        RYKeys.Load(keys);
                    }
                }
                else if (item.Name == "RZKeys")
                {
                    XmlElement keys = item[KeyframeDataCollection.ElementName];
                    if (keys != null)
                    {
                        RZKeys.Load(keys);
                    }
                }
            }
        }
Example #12
0
 public void TestInvalidMultipleKeyWithModifierStringParse()
 {
     try
     {
         // multiple non-modifier keys are also not allowed as you can't represent that using the flags enumeration
         XKeys keyCode = (XKeys)_converter.ConvertFromString("Alt+A+Enter");
         Assert.Fail("Expected an exception because you can't have multiple non-modifier keys");
     }
     catch (FormatException) {}
 }
Example #13
0
 public void TestInvalidKeyLeadingSeparatorStringParse()
 {
     try
     {
         // on the other hand, invalid key strings should throw exceptions during conversion
         XKeys keyCode = (XKeys)_converter.ConvertFromString("+Alt");
         Assert.Fail("Expected an exception because of a leading separator in the parse string");
     }
     catch (FormatException) {}
 }
Example #14
0
 public void TestInvalidKeyWithModifiersStringParse()
 {
     try
     {
         // on the other hand, invalid key strings should throw exceptions during conversion
         XKeys keyCode = (XKeys)_converter.ConvertFromString("Ctrl+NonExistentKey+Alt");
         Assert.Fail("Expected an exception because the parsed string has an invalid key name");
     }
     catch (FormatException) {}
 }
Example #15
0
 public void TestInvalidStringParse()
 {
     try
     {
         // on the other hand, invalid key strings should throw exceptions during conversion
         XKeys keyCode = (XKeys)_converter.ConvertFromString("\n");
         Assert.Fail("Expected an exception because the parsed string has an invalid character");
     }
     catch (FormatException) {}
 }
Example #16
0
        /// <summary>
        /// Raises the <see cref="ValidateKeyStroke"/> event.
        /// </summary>
        /// <param name="e">A <see cref="ValidateKeyStrokeEventArgs"/> that contains the event data.</param>
        protected virtual void OnValidateKeyStroke(ValidateKeyStrokeEventArgs e)
        {
            XKeys key = GetKeyPressed(e.KeyStroke);

            e.IsValid = !(key == XKeys.ControlKey || key == XKeys.AltKey || key == XKeys.ShiftKey);

            if (_validateKeyStrokeEventHandler != null)
            {
                _validateKeyStrokeEventHandler.Invoke(this, e);
            }
        }
Example #17
0
        // Is key pressed
        public bool isKeyPressed(XKeys key)
        {
            int  keyCode = (int)key;
            bool result  = !_keysChecked[keyCode] && _keysPressed[keyCode];

            if (_keysPressed[keyCode])
            {
                _keysChecked[keyCode] = true;
            }

            return(result);
        }
Example #18
0
        private XMouseEventArgs ConvertToXMouseEventArgs(uint gdkButton, Gdk.EventType type, Gdk.ModifierType state, double x, double y)
        {
            XMouseButtons mb = XMouseButtons.None;

            switch (gdkButton)
            {
            case 1:
                mb = XMouseButtons.Left;
                break;

            case 2:
                mb = XMouseButtons.Middle;
                break;

            case 3:
                mb = XMouseButtons.Right;
                break;
            }

            int clickCount = 0;

            switch (type)
            {
            case Gdk.EventType.ButtonPress:
                clickCount = 1;
                break;

            case Gdk.EventType.TwoButtonPress:
                clickCount = 2;
                break;

            case Gdk.EventType.ThreeButtonPress:
                clickCount = 3;
                break;
            }

            XKeys modifiers = XKeys.None;

            if ((state & Gdk.ModifierType.ShiftMask) != 0)
            {
                modifiers |= XKeys.Shift;
            }
            if ((state & Gdk.ModifierType.ControlMask) != 0)
            {
                modifiers |= XKeys.Control;
            }
            if ((state & Gdk.ModifierType.Mod1Mask) != 0)
            {   // ALT key
                modifiers |= XKeys.Alt;
            }

            return(new XMouseEventArgs(mb, clickCount, (int)x, (int)y, 0, modifiers));
        }
Example #19
0
 private static string GetKeyName(XKeys key, IDictionary <XKeys, string> map)
 {
     if (map.ContainsKey(key))
     {
         return(map[key]);
     }
     else if (Enum.IsDefined(typeof(XKeys), (int)key))
     {
         return(Enum.GetName(typeof(XKeys), (int)key));
     }
     return(string.Empty);
 }
Example #20
0
        public void TestStringParseWhitespaceIndifference()
        {
            // whitespace between individual keys in the string should not matter
            XKeys keyCode = (XKeys)_converter.ConvertFromString("Ctrl\t +Attn \t+Alt");

            Assert.AreEqual(XKeys.Control | XKeys.Alt | XKeys.Attn, keyCode, "Whitespace between key string elements should not matter");

            keyCode = (XKeys)_converter.ConvertFromString("Ctrl+ \tAlt+ Attn");
            Assert.AreEqual(XKeys.Control | XKeys.Alt | XKeys.Attn, keyCode, "Whitespace between key string elements should not matter");

            keyCode = (XKeys)_converter.ConvertFromString("Attn + \t Ctrl\t+  Alt");
            Assert.AreEqual(XKeys.Control | XKeys.Alt | XKeys.Attn, keyCode, "Whitespace between key string elements should not matter");
        }
Example #21
0
        public void TestStringParseOrderIndifference()
        {
            // ordering of individual keys in the string should not matter
            XKeys keyCode = (XKeys)_converter.ConvertFromString("Ctrl+Attn+Alt");

            Assert.AreEqual(XKeys.Control | XKeys.Alt | XKeys.Attn, keyCode, "Order of key string elements should not matter");

            keyCode = (XKeys)_converter.ConvertFromString("Ctrl+Alt+Attn");
            Assert.AreEqual(XKeys.Control | XKeys.Alt | XKeys.Attn, keyCode, "Order of key string elements should not matter");

            keyCode = (XKeys)_converter.ConvertFromString("Attn+Ctrl+Alt");
            Assert.AreEqual(XKeys.Control | XKeys.Alt | XKeys.Attn, keyCode, "Order of key string elements should not matter");
        }
Example #22
0
 /// <summary>
 /// Parses a string as an <see cref="XKeys"/> value using the specified <see cref="CultureInfo"/>.
 /// </summary>
 /// <param name="s">The string to be parsed.</param>
 /// <param name="culture">The <see cref="CultureInfo"/> for which the string should be parsed.</param>
 /// <param name="result">The <see cref="XKeys"/> value parsed from <paramref name="s"/> if the string was successfully parsed; <see cref="XKeys.None"/> otherwise.</param>
 /// <returns>True if the string was successfully parsed; False otherwise.</returns>
 public static bool TryParse(string s, CultureInfo culture, out XKeys result)
 {
     try
     {
         result = Parse(s, culture);
         return(true);
     }
     catch (FormatException)
     {
         result = XKeys.None;
         return(false);
     }
 }
Example #23
0
        /// <summary>
        /// 检测当前被按下的键值
        /// Enum.GetValues (https://msdn.microsoft.com/zh-cn/library/system.enum.getvalues.aspx)
        /// typeof (https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/typeof)
        /// </summary>
        /// <returns></returns>
        private XKeys GetCurDownKey()
        {
            XKeys vKey = XKeys.None;

            foreach (Int32 key in Enum.GetValues(typeof(XKeys)))
            {
                if (isKeyDown((XKeys)key))
                {
                    vKey = (XKeys)key;
                    break;
                }
            }
            return(vKey);
        }
Example #24
0
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == typeof(string))
     {
         List <string> keyNames          = new List <string>(4);
         XKeys         keyValue          = (XKeys)value;
         IDictionary <XKeys, string> map = GetKeyNamesMap(culture);
         if ((keyValue & XKeys.Control) == XKeys.Control)
         {
             string modifierName = GetKeyName(XKeys.Control, map);
             if (!string.IsNullOrEmpty(modifierName))
             {
                 keyNames.Add(modifierName);
             }
         }
         if ((keyValue & XKeys.Alt) == XKeys.Alt)
         {
             string modifierName = GetKeyName(XKeys.Alt, map);
             if (!string.IsNullOrEmpty(modifierName))
             {
                 keyNames.Add(modifierName);
             }
         }
         if ((keyValue & XKeys.Shift) == XKeys.Shift)
         {
             string modifierName = GetKeyName(XKeys.Shift, map);
             if (!string.IsNullOrEmpty(modifierName))
             {
                 keyNames.Add(modifierName);
             }
         }
         if ((keyValue & XKeys.KeyCode) != XKeys.None)
         {
             string keyName = GetKeyName(keyValue & XKeys.KeyCode, map);
             if (!string.IsNullOrEmpty(keyName))
             {
                 keyNames.Add(keyName);
             }
         }
         if (keyNames.Count == 0)
         {
             return(string.Empty);
         }
         return(string.Join(KeySeparator.ToString(), keyNames.ToArray()));
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
Example #25
0
        public XmlElement ToXmlElement()
        {
            XmlElement rootMotion = new XmlElement(ElementName);

            if (XKeys.Count > 0)
            {
                XmlElement xKeys = new XmlElement("XKeys");
                xKeys.AppendChild(XKeys.ToXmlElement());
                rootMotion.AppendChild(xKeys);
            }
            if (YKeys.Count > 0)
            {
                XmlElement yKeys = new XmlElement("YKeys");
                yKeys.AppendChild(YKeys.ToXmlElement());
                rootMotion.AppendChild(yKeys);
            }

            if (ZKeys.Count > 0)
            {
                XmlElement zKeys = new XmlElement("ZKeys");
                zKeys.AppendChild(ZKeys.ToXmlElement());
                rootMotion.AppendChild(zKeys);
            }

            if (RXKeys.Count > 0)
            {
                XmlElement rxKeys = new XmlElement("RXKeys");
                rxKeys.AppendChild(RXKeys.ToXmlElement());
                rootMotion.AppendChild(rxKeys);
            }

            if (RYKeys.Count > 0)
            {
                XmlElement ryKeys = new XmlElement("RYKeys");
                ryKeys.AppendChild(RYKeys.ToXmlElement());
                rootMotion.AppendChild(ryKeys);
            }

            if (RZKeys.Count > 0)
            {
                XmlElement rzKeys = new XmlElement("RZKeys");
                rzKeys.AppendChild(RZKeys.ToXmlElement());
                rootMotion.AppendChild(rzKeys);
            }

            return(rootMotion);
        }
Example #26
0
        /// <remarks>
        /// A reference implementation for ToString taken originally from GetTooltipTest(IClickAction) of
        /// ClearCanvas.Desktop.View.WinForms/ActiveToolbarButton.cs/r12907.
        /// This method now uses XKeysConverter, which is why we compare our results
        /// against this reference implementation.
        /// </remarks>
        private static string ReferenceToStringImplementation(XKeys keyStroke)
        {
            bool  ctrl    = (keyStroke & XKeys.Control) == XKeys.Control;
            bool  alt     = (keyStroke & XKeys.Alt) == XKeys.Alt;
            bool  shift   = (keyStroke & XKeys.Shift) == XKeys.Shift;
            XKeys keyCode = keyStroke & XKeys.KeyCode;

            StringBuilder builder = new StringBuilder();

            if (keyCode != XKeys.None)
            {
                if (ctrl)
                {
                    builder.Append("Ctrl");
                }

                if (alt)
                {
                    if (ctrl)
                    {
                        builder.Append("+");
                    }

                    builder.Append("Alt");
                }

                if (shift)
                {
                    if (ctrl || alt)
                    {
                        builder.Append("+");
                    }

                    builder.Append("Shift");
                }

                if (ctrl || alt || shift)
                {
                    builder.Append("+");
                }

                builder.Append(XKeysConverter.FormatInvariant(keyCode));
            }

            return(builder.ToString());
        }
Example #27
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string stringValue = (string)value;
                if (string.IsNullOrEmpty(stringValue))
                {
                    return(XKeys.None);
                }

                Match m = _keyStringParser.Match((string)value);
                if (!m.Success)
                {
                    throw new FormatException(string.Format("Input string was not in the expected format for {0}", typeof(XKeys).FullName));
                }

                XKeys modifiers = XKeys.None;
                XKeys keyCode   = XKeys.None;
                IDictionary <string, XKeys> map = GetKeyValuesMap(culture);
                for (int n = 1; n <= 4; n++)
                {
                    if (m.Groups[n].Length == 0)
                    {
                        break;
                    }

                    XKeys keyValue = GetKeyValue(m.Groups[n].Value, map);
                    if ((keyValue & XKeys.Modifiers) > 0)
                    {
                        modifiers |= keyValue;
                    }
                    else if ((keyValue & XKeys.KeyCode) > 0)
                    {
                        if (keyCode != XKeys.None)
                        {
                            throw new FormatException(string.Format("Values for {0} may have only one non-modifier key.", typeof(XKeys).FullName));
                        }
                        keyCode = keyValue;
                    }
                }
                return(modifiers | keyCode);
            }
            return(base.ConvertFrom(context, culture, value));
        }
 private void HandleCheckBoxCheckedChanged(object sender, EventArgs e)
 {
     if (!_updatingCheckBoxChecked)
     {
         XKeys keyModifiers = XKeys.None;
         if (_checkBoxCtrl.Checked)
         {
             keyModifiers |= XKeys.Control;
         }
         if (_checkBoxAlt.Checked)
         {
             keyModifiers |= XKeys.Alt;
         }
         if (_checkBoxShift.Checked)
         {
             keyModifiers |= XKeys.Shift;
         }
         this.KeyModifiers = keyModifiers;
     }
 }
Example #29
0
        public void KeyboardEventsHandler()
        {
            XKeyboardEventArgs e;
            XKeys xKeyDown = GetCurDownKey();

            if (xKeyDown != XKeys.None)
            {
                // 有新的按键被按下
                this.m_oldKey = xKeyDown;
                e             = new XKeyboardEventArgs(xKeyDown);
                this.OnKeyDown(e);
            }
            else if (this.m_oldKey != XKeys.None && !isKeyDown(this.m_oldKey))
            {
                // 有旧的按键被释放
                e = new XKeyboardEventArgs(this.m_oldKey);
                this.OnKeyUp(e);
                this.m_oldKey = XKeys.None;
            }
        }
Example #30
0
        private void DeserializeGroupPresets(PresetVoiLutCollection presets, XmlNodeList presetNodes)
        {
            foreach (XmlElement presetNode in presetNodes)
            {
                string keyStrokeAttribute = presetNode.GetAttribute("keystroke");
                XKeys  keyStroke          = XKeys.None;
                if (!String.IsNullOrEmpty(keyStrokeAttribute))
                {
                    keyStroke = (XKeys)_xkeysConverter.ConvertFromInvariantString(keyStrokeAttribute);
                }

                string factoryName = presetNode.GetAttribute("factory");

                IPresetVoiLutOperationFactory factory = PresetVoiLutOperationFactories.GetFactory(factoryName);
                if (factory == null)
                {
                    continue;
                }

                PresetVoiLutConfiguration configuration = PresetVoiLutConfiguration.FromFactory(factory);

                XmlNodeList configurationItems = presetNode.SelectNodes("configuration/item");
                foreach (XmlElement configurationItem in configurationItems)
                {
                    configuration[configurationItem.GetAttribute("key")] = configurationItem.GetAttribute("value");
                }

                try
                {
                    IPresetVoiLutOperation operation = factory.Create(configuration);
                    PresetVoiLut           preset    = new PresetVoiLut(operation);
                    preset.KeyStroke = keyStroke;
                    presets.Add(preset);
                }
                catch (Exception e)
                {
                    Platform.Log(LogLevel.Error, e);
                    continue;
                }
            }
        }
		/// <summary>
		/// Maps a single key value to an appropriate key name.
		/// </summary>
		/// <remarks>
		/// <para>
		/// The default implementation uses an <see cref="XKeysConverter"/>.
		/// </para>
		/// </remarks>
		/// <param name="key">A single value from the <see cref="XKeys"/> enumeration.</param>
		/// <returns>The key name for the given key.</returns>
		protected virtual string GetKeyName(XKeys key)
		{
			return TypeDescriptor.GetConverter(typeof (XKeys)).ConvertToString(key);
		}
Example #32
0
		/// <summary>
		/// Constructor.
		/// </summary>
		public KeyboardButtonMessage(XKeys keyData, ButtonActions buttonAction)
		{
			_buttonAction = buttonAction;
			_buttonShortcut = new KeyboardButtonShortcut(keyData);
		}
Example #33
0
		/// <summary>
		/// Formats a <see cref="XKeys"/> value as a string using the <see cref="CultureInfo.InvariantCulture"/>.
		/// </summary>
		/// <param name="value">The <see cref="XKeys"/> value to be formatted.</param>
		/// <returns>The string representation of the given <paramref name="value"/>.</returns>
		public static string FormatInvariant(XKeys value)
		{
			return Format(value, CultureInfo.InvariantCulture);
		}
Example #34
0
			public AbstractClickAction(string id, string path)
				: base(id, path)
			{
				_keyStroke = XKeys.None;
			}
				public KeyModifiersFlagPropertyDescriptor(XKeys modifier)
					: base(typeof (XKeys), Enum.GetName(typeof (XKeys), modifier), typeof (bool))
				{
					_modifier = modifier;
				}
		/// <summary>
		/// Gets the individual key that was pressed in the given key stroke without any modifier flags.
		/// </summary>
		/// <param name="keyStroke">The key stroke from which the pressed key is to be extracted.</param>
		/// <returns>A single value from the <see cref="XKeys"/> enumeration excluding
		/// <see cref="XKeys.Control"/>, <see cref="XKeys.Alt"/> and <see cref="XKeys.Shift"/> (i.e. not a bitwise combination).</returns>
		protected static XKeys GetKeyPressed(XKeys keyStroke)
		{
			return (keyStroke & ~XKeys.Modifiers);
		}
		/// <summary>
		/// Tests whether or not a given key combination is a valid key stroke by invoking the <see cref="ValidateKeyStroke"/> event.
		/// </summary>
		/// <param name="keyStroke">The key combination to be tested.</param>
		/// <returns>True if the key combination is a valid key stroke; False otherwise.</returns>
		protected bool IsValidKeyStroke(XKeys keyStroke)
		{
			ValidateKeyStrokeEventArgs e = new ValidateKeyStrokeEventArgs(keyStroke);
			this.OnValidateKeyStroke(e);
			return e.IsValid;
		}
		/// <summary>
		/// Tests whether or not a given key stroke is modified by the <see cref="XKeys.Alt"/> modifier key.
		/// </summary>
		/// <param name="keyStroke">The key stroke to be tested.</param>
		/// <returns>True if the key stroke is modified by <see cref="XKeys.Alt"/>; False otherwise.</returns>
		protected static bool IsKeyPressModifiedByAlt(XKeys keyStroke)
		{
			return (keyStroke & XKeys.Alt) == XKeys.Alt;
		}
Example #39
0
 public CachedKey(XKeys Sym, bool Shift)
     : this((uint)Sym, Shift) {
 }
Example #40
0
		/// <summary>
		/// Parses a string as an <see cref="XKeys"/> value using the specified <see cref="CultureInfo"/>.
		/// </summary>
		/// <param name="s">The string to be parsed.</param>
		/// <param name="culture">The <see cref="CultureInfo"/> for which the string should be parsed.</param>
		/// <param name="result">The <see cref="XKeys"/> value parsed from <paramref name="s"/> if the string was successfully parsed; <see cref="XKeys.None"/> otherwise.</param>
		/// <returns>True if the string was successfully parsed; False otherwise.</returns>
		public static bool TryParse(string s, CultureInfo culture, out XKeys result)
		{
			try
			{
				result = Parse(s, culture);
				return true;
			}
			catch (FormatException)
			{
				result = XKeys.None;
				return false;
			}
		}
Example #41
0
		/// <summary>
		/// Parses a string as an <see cref="XKeys"/> value using the <see cref="CultureInfo.InvariantCulture"/>.
		/// </summary>
		/// <param name="s">The string to be parsed.</param>
		/// <param name="result">The <see cref="XKeys"/> value parsed from <paramref name="s"/> if the string was successfully parsed; <see cref="XKeys.None"/> otherwise.</param>
		/// <returns>True if the string was successfully parsed; False otherwise.</returns>
		public static bool TryParseInvariant(string s, out XKeys result)
		{
			return TryParse(s, CultureInfo.InvariantCulture, out result);
		}
Example #42
0
		/// <summary>
		/// Formats a <see cref="XKeys"/> value as a string using the <see cref="CultureInfo.CurrentUICulture">current thread's UI CultureInfo</see>.
		/// </summary>
		/// <param name="value">The <see cref="XKeys"/> value to be formatted.</param>
		/// <returns>The string representation of the given <paramref name="value"/>.</returns>
		public static string Format(XKeys value)
		{
			return Format(value, CultureInfo.CurrentUICulture);
		}
		/// <summary>
		/// Constructor.
		/// </summary>
		public KeyboardButtonDownPreview(XKeys keyData)
		{
			_buttonShortcut = new KeyboardButtonShortcut(keyData);
		}
		/// <summary>
		/// Tests whether or not a given key stroke is modified by one or more of the modifier keys
		/// (<see cref="XKeys.Control"/>, <see cref="XKeys.Alt"/> and <see cref="XKeys.Shift"/>).
		/// </summary>
		/// <param name="keyStroke">The key stroke to be tested.</param>
		/// <returns>True if the key stroke is modified; False otherwise.</returns>
		protected static bool IsKeyPressModified(XKeys keyStroke)
		{
			return (keyStroke & XKeys.Modifiers) != 0;
		}
		/// <summary>
		/// Formats a key stroke as a human-readable string.
		/// </summary>
		/// <remarks>
		/// The default implementation separates modifiers from the key pressed,
		/// translates each modifier/key individually using <see cref="GetKeyName"/>, 
		/// and combines them together using <see cref="KeySeparator"/>.
		/// A trailing <see cref="KeySeparator"/> is shown if this particular key stroke
		/// is not valid as determined by <see cref="IsValidKeyStroke"/>.
		/// A key stroke of <see cref="XKeys.None"/> is formatted as an empty string.
		/// </remarks>
		/// <param name="keyStroke">The key stroke to be formatted.</param>
		/// <returns>A human-readable string representing the key stroke.</returns>
		protected virtual string FormatKeyStroke(XKeys keyStroke)
		{
			if (keyStroke == 0)
				return string.Empty;

			bool control, alt, shift;
			bool valid = IsValidKeyStroke(keyStroke);
			XKeys key = GetKeyPressed(keyStroke, out control, out alt, out shift);

			List<string> keys = new List<string>(4);
			if (control)
				keys.Add(GetKeyName(XKeys.Control));
			if (alt)
				keys.Add(GetKeyName(XKeys.Alt));
			if (shift)
				keys.Add(GetKeyName(XKeys.Shift));

			// display a trailing '+' if the keystroke is invalid
			keys.Add(valid ? GetKeyName(key) : string.Empty);

			return string.Join(_keySeparator ?? string.Empty, keys.ToArray());
		}
		/// <summary>
		/// Initializes a new instance of <see cref="ValidateKeyStrokeEventArgs"/>.
		/// </summary>
		/// <param name="keyStroke">The key stroke which is to be validated.</param>
		public ValidateKeyStrokeEventArgs(XKeys keyStroke)
		{
			this.KeyStroke = keyStroke;
			this.IsValid = true;
		}
		/// <summary>
		/// Gets the individual key that was pressed in the given key stroke without any modifier flags.
		/// </summary>
		/// <param name="keyStroke">The key stroke from which the pressed key is to be extracted.</param>
		/// <param name="control">True if the <see cref="XKeys.Control"/> modifier was pressed; False otherwise.</param>
		/// <param name="alt">True if the <see cref="XKeys.Alt"/> modifier was pressed; False otherwise.</param>
		/// <param name="shift">True if the <see cref="XKeys.Shift"/> modifier was pressed; False otherwise.</param>
		/// <returns>A single value from the <see cref="XKeys"/> enumeration excluding
		/// <see cref="XKeys.Control"/>, <see cref="XKeys.Alt"/> and <see cref="XKeys.Shift"/> (i.e. not a bitwise combination).</returns>
		protected static XKeys GetKeyPressed(XKeys keyStroke, out bool control, out bool alt, out bool shift)
		{
			control = IsKeyPressModifiedByControl(keyStroke);
			alt = IsKeyPressModifiedByAlt(keyStroke);
			shift = IsKeyPressModifiedByShift(keyStroke);
			return GetKeyPressed(keyStroke);
		}
Example #48
0
		/// <summary>
		/// Parses a string as an <see cref="XKeys"/> value using the <see cref="CultureInfo.CurrentUICulture">current thread's UI CultureInfo</see>.
		/// </summary>
		/// <param name="s">The string to be parsed.</param>
		/// <param name="result">The <see cref="XKeys"/> value parsed from <paramref name="s"/> if the string was successfully parsed; <see cref="XKeys.None"/> otherwise.</param>
		/// <returns>True if the string was successfully parsed; False otherwise.</returns>
		public static bool TryParse(string s, out XKeys result)
		{
			return TryParse(s, CultureInfo.CurrentUICulture, out result);
		}
		/// <summary>
		/// Tests whether or not a given key stroke is modified by the <see cref="XKeys.Shift"/> modifier key.
		/// </summary>
		/// <param name="keyStroke">The key stroke to be tested.</param>
		/// <returns>True if the key stroke is modified by <see cref="XKeys.Shift"/>; False otherwise.</returns>
		protected static bool IsKeyPressModifiedByShift(XKeys keyStroke)
		{
			return (keyStroke & XKeys.Shift) == XKeys.Shift;
		}
Example #50
0
		private static string GetKeyName(XKeys key, IDictionary<XKeys, string> map)
		{
			if (map.ContainsKey(key))
				return map[key];
			else if (Enum.IsDefined(typeof (XKeys), (int) key))
				return Enum.GetName(typeof (XKeys), (int) key);
			return string.Empty;
		}
		/// <summary>
		/// Tests whether or not a given key stroke is modified by the <see cref="XKeys.Control"/> modifier key.
		/// </summary>
		/// <param name="keyStroke">The key stroke to be tested.</param>
		/// <returns>True if the key stroke is modified by <see cref="XKeys.Control"/>; False otherwise.</returns>
		protected static bool IsKeyPressModifiedByControl(XKeys keyStroke)
		{
			return (keyStroke & XKeys.Control) == XKeys.Control;
		}
		private void UpdateText(XKeys keyStroke)
		{
			base.Text = _textBox.Text = this.FormatKeyStroke(keyStroke);
			_textBox.SelectionStart = _textBox.TextLength;
		}
		/// <summary>
		/// Resets the <see cref="KeyStroke"/> property to its default value.
		/// </summary>
		public virtual void ResetKeyStroke()
		{
			this.KeyStroke = XKeys.None;
		}
		public AbstractActionModelTreeLeafClickAction(IClickAction clickAction) : base(clickAction)
		{
			_keyStroke = clickAction.KeyStroke;
		}
Example #55
0
		/// <remarks>
		/// A reference implementation for ToString taken originally from GetTooltipTest(IClickAction) of
		/// ClearCanvas.Desktop.View.WinForms/ActiveToolbarButton.cs/r12907.
		/// This method now uses XKeysConverter, which is why we compare our results
		/// against this reference implementation.
		/// </remarks>
		private static string ReferenceToStringImplementation(XKeys keyStroke)
		{
			bool ctrl = (keyStroke & XKeys.Control) == XKeys.Control;
			bool alt = (keyStroke & XKeys.Alt) == XKeys.Alt;
			bool shift = (keyStroke & XKeys.Shift) == XKeys.Shift;
			XKeys keyCode = keyStroke & XKeys.KeyCode;

			StringBuilder builder = new StringBuilder();
			if (keyCode != XKeys.None)
			{
				if (ctrl)
					builder.Append("Ctrl");

				if (alt)
				{
					if (ctrl)
						builder.Append("+");

					builder.Append("Alt");
				}

				if (shift)
				{
					if (ctrl || alt)
						builder.Append("+");

					builder.Append("Shift");
				}

				if (ctrl || alt || shift)
					builder.Append("+");

				builder.Append(XKeysConverter.FormatInvariant(keyCode));
			}

			return builder.ToString();
		}
		public bool IsValidKeyStroke(XKeys keyStroke)
		{
			return this.RequestValidation("KeyStrokePreview", keyStroke);
		}
Example #57
0
			public AbstractClickAction(IClickAction concreteAction)
				: base(concreteAction)
			{
				_keyStroke = concreteAction.KeyStroke;
			}
		protected virtual void OnKeyStrokeChanged()
		{
			this.NotifyItemChanged();

			this.Action.KeyStroke = _keyStroke;
		}
Example #59
0
			public AbstractClickAction(string id, string path, IResourceResolver resourceResolver)
				: base(id, path, resourceResolver)
			{
				_keyStroke = XKeys.None;
			}
Example #60
0
		/// <summary>
		/// Formats a <see cref="XKeys"/> value as a string using the specified <see cref="CultureInfo"/>.
		/// </summary>
		/// <param name="value">The <see cref="XKeys"/> value to be formatted.</param>
		/// <param name="culture">The <see cref="CultureInfo"/> for which the value should be formatted.</param>
		/// <returns>The string representation of the given <paramref name="value"/>.</returns>
		public static string Format(XKeys value, CultureInfo culture)
		{
			return Default.ConvertToString(null, culture, value);
		}