In WPF I would like to subscribe to the mouse position points only while a key is held down. I then want to set the captured points onto a property, only when the key is released (i.e. when I have a full set of the captured points) and continue listening for the next key down/up combination to create another capture of mouse positions, etc.
My interpretation of the above is that I need to trigger a sequence when a key is down and stop taking when a key is released, but I want the OnNext to receive a set of mouse points.
From a fair amount of reading (I'm new to Rx) I've put together the following pseudo/real sample:
var keyDownSeq = Observable.FromEvent(...);
var keyUpSeq = Observable.FromEvent(...);
var mouseMoveSeq = Observable.FromEvent(...);
var mouseMovesWhileKeyDown = keyDownSeq
.Where(keyEventArgs => keyEventArgs.IsRepeat == false) //WPF fires the same KeyDown repeatedly
.Where(keyEventArgs => keyEventArgs.Key == Key.Space)
.Select(_ => mouseMoveSeq
.TakeUntil(keyUpSeq)
.ToList())
.Subscribe(listOfMousePoints => MyProperty = listOfMousePoints);
Will the above do what I think it will and create a list of mouse points that were encountered while the Space bar is held down? Do I need to call ToList() where I do, or should I do this in the Subscribe?
If I remove the second Where clause (allowing any key(s) to be pressed to begin a capture) how can I prevent a second, or third, key being held down and causing duplicates in the resulting sequence?
Thank you.
Edit
Would it be completely incorrect to do the following using a local variable?
- Set a local variable to the KeyDown sequence value in the Select()
- Reset the local variable to null when the KeyUpSeq encounters the same key
- Filter the KeyDownSeq to ignore all values while this variable has value
- Filter the KeyUpSeq to ignore all KeyUp values that don't match the local variable
Does Rx have a concept of such a local state variable?