如果按下空格键,将触发 WPF MouseEnter 事件

WPF MouseEnter event is fired if spacebar is pressed

我只想处理鼠标操作的按钮 MouseEnter 事件。所以我必须检测 MouseEnter 是由鼠标还是键盘触发的。

private void button_onMouseEnter(object sender, MouseEventArgs e)
{
    Button button = (Button)sender;

    if (!button.IsMouseOver)            return; // not working
    if (!button.IsMouseDirectlyOver)    return; // not working
    ...
}

您可以查看最近的输入设备

if (InputManager.Current.MostRecentInputDevice is KeyboardDevice);

正如这里所讨论的那样:

How to tell if a button click event was triggered by keyboard or mouse in WPF?