gpt4 book ai didi

wpf - 区分正常的 "ENTER"和数字键盘 "ENTER"按键?

转载 作者:行者123 更新时间:2023-12-04 20:53:16 29 4
gpt4 key购买 nike

在我的 PreviewKeyDown() handler 如何区分数字键盘上的 ENTER 键和主板上的 ENTER 键?

两个键返回相同的值 Key.EnterKeyEventArgs.Key .

我能找到的最接近这个问题的答案在这里:What's the difference between Key.Enter and Key.Return? ,但不幸的是,这仅在应用程序完全受信任时才有效。

我想要一个没有这个限制的解决方案。

最佳答案

link ,示例实现。以下。

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)
{
Log("Could not get the internal IsExtendedKey property from KeyEventArgs. Unable to detect numpad keypresses.", ex);
}

return false;
}
备注如果你想检查常规的 EnterKey 那么显然你应该打电话 e.Key == Key.Enter && !IsNumpadEnterKey(e)

关于wpf - 区分正常的 "ENTER"和数字键盘 "ENTER"按键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8059177/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com