dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("key", (int)key, typeof(Key))
Error message
InvalidEnumArgumentException("key", (int)key, typeof(Key)) What it means
The KeyEventArgs constructor validates that the supplied Key is a keyboard-supported key (Keyboard.IsValidKey). Passing a key outside the valid range or an undefined Key member throws InvalidEnumArgumentException naming parameter 'key'.
Solutions
- Pass only valid Key values; check Keyboard.IsValidKey(key) before constructing
- Fix the int-to-Key conversion that produced the invalid value (e.g. off-by-one or wrong virtual-key mapping)
- For synthetic input, use valid keys from the Input/KeyboardSupport keys tables
Example fix
// before
var args = new KeyEventArgs(kb, src, timestamp, (Key)5000);
// after
Key k = (Key)5000;
if (Keyboard.IsValidKey(k))
var args = new KeyEventArgs(kb, src, timestamp, k); Defensive patterns
Strategy: validation
Validate before calling
bool keyOk = Keyboard.IsValidKey(key);
Type guard
bool IsValidKey(Key k) => Keyboard.IsValidKey(k);
Try / catch
try { var e = new KeyEventArgs(kb, src, ts, key); } catch (InvalidEnumArgumentException) { /* invalid key from interop */ } Prevention
- Validate all interop-derived keys with Keyboard.IsValidKey before use
- Never cast arbitrary integers to Key without range checks
When it happens
Trigger: Constructing new KeyEventArgs(keyboard, inputSource, timestamp, key) with a Key value not in Keyboard's valid key range (e.g. cast from arbitrary int, Key.None misuse, or synthetic key from external input).
Common situations: Raising simulated keyboard events in tests or automation; translating raw scan codes/virtual keys from Win32 interop into Key; corrupt persisted key data.
Understand the failure class
Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.
Related errors
- InvalidEnumArgumentException("key", (int)key, typeof(Key))
- FileAccess value is not valid.
- Invalid priority value.
- origin
- SR.Automation_InvalidEventId
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/fecfc2b25a308a49.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyEventArgs.cs:32
/// </summary>
/// <param name="keyboard">
/// The logical keyboard device associated with this event.
/// </param>
/// <param name="inputSource">
/// The input source that provided this input.
/// </param>
/// <param name="timestamp">
/// The time when the input occurred.
/// </param>
/// <param name="key">
/// The key referenced by the event.
/// </param>
public KeyEventArgs(KeyboardDevice keyboard, PresentationSource inputSource, int timestamp, Key key) : base(keyboard, timestamp)
{
ArgumentNullException.ThrowIfNull(inputSource);
if (!Keyboard.IsValidKey(key))
throw new System.ComponentModel.InvalidEnumArgumentException("key", (int)key, typeof(Key));
_inputSource = inputSource;
_realKey = key;
_isRepeat = false;
// Start out assuming that this is just a normal key.
MarkNormal();
}
/// <summary>
/// The input source that provided this input.
/// </summary>
public PresentationSource InputSource
{
get
{
View on GitHub (pinned to 81131a70a4)