Reputation: 5567
I've written a really tiny WPF + Rx program in VS 2010, and it has some unexpected behavior -- seemingly picking up alt-key events in the mouse-event observables. I'd be grateful for advice on any of
First, what it does correctly is fire mouse move observers starting from a mouse down and ending with a mouse up. The particular test observer I have just puts the proper mouseDown + mouseUp coordinates in the TitleBar of the Window (see code + XAML below).
The strangeness starts if I press and release the alt key while the mouse is down. That seems immediately to stop the mouseMove event observers. Now, if I release the mouseButton, the mouseMove event observers start up again, and continue until I either generate a new mouseDown or press and release alt again. Further more, if I go through this sequence, namely
mouseDown
mouseMove
alt-key down
alt-key up
mouseUp
alt-key down
alt-key up
and then, the normal
mouseDown
mouseMove
the print in the Title bar has some odd flashing, telling me that there are other, extraneous events getting tracked and pumped through my observer lambda.
I haven't been able to make this fail with keys other than alt, deepening the mystery.
The desired behavior could be
But, for sure, having this behavior and, worse, having no clue how to proceed to understand it, is really bad. I'll be grateful for any ideas!
(NOTE: add references to System.coreex, System.Reactive, and System.Interactive)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace DownAndMoveTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
(from mDown in this.GetLeftMouseDownObservable().
Do(e => this.Title = "DOWN: " + e.EventArgs.GetPosition(this).ToString())
let mDownP = mDown.EventArgs.GetPosition(this)
from mMove in this.GetMouseMoveObservable().TakeUntil(this.GetLeftMouseUpObservable())
select new
{
D = mDownP,
M = mMove.EventArgs.GetPosition(this)
}).Subscribe(ps =>
{
this.Title = String.Format(
"Window MouseDown({0:G}, {1:G}), MouseMove({2:G}, {3:G})",
ps.D.X, ps.D.Y,
ps.M.X, ps.M.Y);
});
}
}
public static partial class UIElementExtensions
{
public static IObservable<IEvent<MouseButtonEventArgs>>
GetLeftMouseDownObservable(this UIElement uiElement)
{
return Observable.FromEvent<MouseButtonEventHandler, MouseButtonEventArgs>(
h => new MouseButtonEventHandler(h),
h => uiElement.MouseLeftButtonDown += h,
h => uiElement.MouseLeftButtonDown -= h);
}
public static IObservable<IEvent<MouseEventArgs>>
GetMouseMoveObservable(this UIElement uiElement)
{
return Observable.FromEvent<MouseEventHandler, MouseEventArgs>(
h => new MouseEventHandler(h),
h => uiElement.MouseMove += h,
h => uiElement.MouseMove -= h);
}
public static IObservable<IEvent<MouseButtonEventArgs>>
GetLeftMouseUpObservable(this UIElement uiElement)
{
return Observable.FromEvent<MouseButtonEventHandler, MouseButtonEventArgs>(
h => new MouseButtonEventHandler(h),
h => uiElement.MouseLeftButtonUp += h,
h => uiElement.MouseLeftButtonUp -= h);
}
}
}
and its XAML
<Window x:Class="DownAndMoveTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
Upvotes: 0
Views: 526
Reputation: 84754
Does your application have a main menu? Pressing the alt key focuses the menu bar, and pressing alt again returns focus to whatever had focus before. This would explain the functionality you are seeing. (It does not look like an Rx issue)
Upvotes: 1