{"record":{"id":"2636c238472e5c9b","repo":"dotnet/wpf","slug":"sr-keygesture-invalid-modifiers-key","errorCode":null,"errorMessage":"SR.KeyGesture_Invalid (modifiers, key)","messagePattern":"SR\\.KeyGesture_Invalid \\(modifiers, key\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyGesture.cs","lineNumber":100,"sourceCode":"        /// Private constructor that does the real work.\n        /// </summary>\n        /// <param name=\"key\">Key</param>\n        /// <param name=\"modifiers\">Modifiers</param>\n        /// <param name=\"displayString\">display string</param>\n        /// <param name=\"validateGesture\">If true, throws an exception if the key and modifier are not valid</param>\n        private KeyGesture(Key key, ModifierKeys modifiers, string displayString, bool validateGesture)\n        {\n            if(!ModifierKeysConverter.IsDefinedModifierKeys(modifiers))\n                throw new InvalidEnumArgumentException(\"modifiers\", (int)modifiers, typeof(ModifierKeys));\n\n            if(!IsDefinedKey(key))\n                throw new InvalidEnumArgumentException(\"key\", (int)key, typeof(Key));\n\n            ArgumentNullException.ThrowIfNull(displayString);\n\n            if (validateGesture && !IsValid(key, modifiers))\n            {\n                throw new NotSupportedException(SR.Format(SR.KeyGesture_Invalid, modifiers, key));\n            }\n\n            _modifiers = modifiers;\n            _key = key;\n            _displayString = displayString;\n        }\n#endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n#region Public Methods\n        /// <summary>\n        /// Modifier\n        /// </summary>\n        public ModifierKeys Modifiers","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/KeyGesture.cs#L82-L118","documentation":"Even with defined Key and ModifierKeys values, KeyGesture throws NotSupportedException with SR.KeyGesture_Invalid when validateGesture is true and IsValid(key, modifiers) fails. WPF requires function keys (F1-F24) or a key paired with a non-None modifier (with limited exceptions like Esc, Del, and other navigation keys which may be standalone). Pure letter/digit keys with no modifier are not valid gestures.","triggerScenarios":"new KeyGesture(Key.A) — a letter with no modifiers; new KeyGesture(Key.D1, ModifierKeys.None); similar no-modifier non-function keys via any public constructor.","commonSituations":"Allowing users to configure a shortcut with just a letter key, or building gestures from parsed strings like 'A' without modifiers.","solutions":["Require a modifier for non-function keys: new KeyGesture(Key.A, ModifierKeys.Control)","Use function keys (F1-F12) or valid standalone keys (Esc, Del, etc.) when no modifier is desired","Validate with KeyGesture.IsValid(key, modifiers) before constructing and show a friendly error to users"],"exampleFix":"// before\nvar gesture = new KeyGesture(Key.A); // invalid: no modifier\n// after\nvar gesture = new KeyGesture(Key.A, ModifierKeys.Control);","handlingStrategy":"validation","validationCode":"if (!KeyGesture.IsValid(key, modifiers)) throw new ArgumentException(\"Key+modifier combination is not a valid gesture.\");","typeGuard":"static bool IsConfigurableGesture(Key k, ModifierKeys m) => KeyGesture.IsValid(k, m);","tryCatchPattern":"try { var g = new KeyGesture(key, modifiers); } catch (NotSupportedException ex) { /* invalid combination, e.g. bare letter key */ }","preventionTips":["Require a modifier for letter/digit keys","Use function keys (F1-F12) or allowed standalone keys (Esc, Del, etc.) without modifiers","Validate user-configured shortcuts with KeyGesture.IsValid before constructing"],"tags":["wpf","input","keygesture","shortcut-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}