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

时间:2015-05-18 10:39:48

标签: c# wpf button event-handling mouseenter

我想只为鼠标操作处理按钮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
    ...
}

1 个答案:

答案 0 :(得分:1)

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

if (InputManager.Current.MostRecentInputDevice is KeyboardDevice);

如下所述:

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