Distinguish between normal "ENTER" and numeric keypad "ENTER"?

In my PreviewKeyDown() handler, how do I know the difference between the ENTER key on the numeric keypad and the ENTER key on the main board?

Both keys return the same Key.Enter value for KeyEventArgs.Key .

The closest answer I can find on this is here: What is the difference between Key.Enter and Key.Return? , but unfortunately this only works if the application is fully trusted.

I need a solution without this limitation.

+4
source share
3 answers

See link , example impl. below.

 private static bool IsNumpadEnterKey(KeyEventArgs e) { if (e.Key != Key.Enter) return false; // To understand the following UGLY implementation please check this MSDN link. Suggested workaround to differentiate between the Return key and Enter key. // https://social.msdn.microsoft.com/Forums/vstudio/en-US/b59e38f1-38a1-4da9-97ab-c9a648e60af5/whats-the-difference-between-keyenter-and-keyreturn?forum=wpf try { return (bool)typeof(KeyEventArgs).InvokeMember("IsExtendedKey", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance, null, e, null); } catch (Exception ex) { if (AiLoggingService.IsErrorEnabled) AiLoggingService.LogError("Could not get the internal IsExtendedKey property from KeyEventArgs. Unable to detect numpad keypresses.", ex); } return false; } 

Nb if you want to check the regular EnterKey, then obviously you should call
e.Key == Key.Enter && !IsNumpadEnterKey(e)

+3
source

Sorry if I'm useless, but I don't think this is possible. Both ENTER keys return the same thing, so there is no real way to tell.

0
source

The scan code is different for each key. You will need to see it.

0
source

Source: https://habr.com/ru/post/1380305/


All Articles