区分正常的“ENTER”和“ENTER”和数字键盘“ENTER”按键?

发布于 2024-12-14 12:34:25 字数 392 浏览 2 评论 0原文

在我的 PreviewKeyDown() 处理程序中,如何区分数字键盘上的 ENTER 键和主板上的 ENTER 键?

两个键都为 KeyEventArgs.Key 返回相同的值 Key.Enter

我能找到的最接近这个问题的答案在这里: Key.Enter 和 Key.Return 之间有什么区别?,但不幸的是,这仅在应用程序完全受信任的情况下才有效。

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

In my PreviewKeyDown() handler how do I tell the difference between the ENTER key on number-pad and the ENTER key on the main board?

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

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

I'd like a solution without this restriction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹 2024-12-21 12:34:25

请参阅链接,示例实现。以下。

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)

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

  return false;
}

N.b. if you want to check for regular EnterKey then obviously you should call
e.Key == Key.Enter && !IsNumpadEnterKey(e)

白芷 2024-12-21 12:34:25

每个键的扫描码都不同。你必须能够看到这一点。

The scan code is different for every key. You will have to be able to see that.

不交电费瞎发啥光 2024-12-21 12:34:25

抱歉,如果我没用,但我认为这是不可能的。两个 ENTER 键返回相同的内容,因此没有真正的方法来区分。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文