Search code examples
c#wpfbuttonevent-handlingmouseenter

WPF MouseEnter event is fired if spacebar is pressed


I want to handle a buttons MouseEnter event only for mouse actions. So I have to detect if MouseEnter is triggered by mouse or by keyboard.

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

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

Solution

  • you can check most recent input device

    if (InputManager.Current.MostRecentInputDevice is KeyboardDevice);
    

    as been discussed here:

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