我有一个代表键盘输入流(KeyUp或KeyDown)的流IObservable<InputEvent> eventStream
。通过应用 Window运算符,我可以隔离保持输入的持续时间:
public static IObservable<InputEvent> WhileHeld(this IObservable<InputEvent> source, String key) {
var k = source.Where(i => i.Key == key);
return source.Window(k.Press(), _ => k.Release()).Switch().Where(i => i.Key != key);
}
我现在要做的是找到多个窗口的“重叠”。例如:
var ctrlHeld = eventStream.WhileHeld("ctrl");
var shiftHeld = eventStream.WhileHeld("shift");
我想应用运算符来查找这两个序列之间的重叠,如下面的大理石图所示
.
是新闻,'
是发布大理石:
K |--.---.-.-.-'-'---.-.-.-'-'---.-.-'-.-.-.-'
i |--a---C-S-b-S-C---C-S-c-C-S---C-S-S-d-S-e-C-
C |--------S-b-S-------S-c---------S-S-d-S-e---
S |----------b-----------c-C---------------e-C
r |----------b-----------c-----------------e---
这样的运营商是否存在?或者它将如何组成?
编辑:
帮助可视化实际的事件流(我意识到上面的大理石图有点复杂)。这是我的事件流测试代码:
eventStream.Pump("a", EventType.Down); // should not propagate
eventStream.Pump("ctrl", EventType.Down);
eventStream.Pump("shift", EventType.Down);
eventStream.Pump("b", EventType.Down); // should propagate
eventStream.Pump("shift", EventType.Up);
eventStream.Pump("ctrl", EventType.Up);
eventStream.Pump("ctrl", EventType.Down);
eventStream.Pump("shift", EventType.Down);
eventStream.Pump("c", EventType.Down); // should propagate
eventStream.Pump("ctrl", EventType.Up);
eventStream.Pump("shift", EventType.Up);
eventStream.Pump("ctrl", EventType.Down);
eventStream.Pump("shift", EventType.Down);
eventStream.Pump("shift", EventType.Up);
eventStream.Pump("d", EventType.Down); // should not propagate
eventStream.Pump("shift", EventType.Down);
eventStream.Pump("e", EventType.Down); // should propagate
eventStream.Pump("ctrl", EventType.Up);
答案 0 :(得分:1)
(注意:将在电脑前测试/修改一次)
我认为您可以使用GroupJoin
获得“重叠窗口”行为,如下所示:
var ctrlShift = ctrlHeld.GroupJoin(
shiftHeld,
e => eventStream.Key("ctrl").Release(), // "left duration" selector
e => eventStream.Key("shift").Release(), // "right duration" selector
(a,b) => b.Key(a.Key).Press())
.Switch();
但我永远无法从单独的视线中获得GroupJoin
语法,所以很可能这不是它......基本上,这个想法是:
(编辑:进一步思考,你可以在持续时间内重复使用窗口选择器......)
var ctrlShift = ctrlHeld.GroupJoin(
shiftHeld,
e => ctrlHeld, // "left duration" selector
e => shiftHeld, // "right duration" selector
(a,b) => b.Key(a.Key).Press())
.Switch();
啊哈,我知道有一个链接解释了这个:
答案 1 :(得分:1)
我最终提出的解决方案提供了x.WhileHeld(ctrl).WhileHeld(shift)
的组合语法。诀窍是传入来自原始源的IObservable<InputEvent>
,而不是string
,然后将其显示为:
public static IObservable<InputEvent> WhileHeld(this IObservable<InputEvent> source, IObservable<InputEvent> held) {
return source.Window(held.Press(), _ => held.Release()).Switch();
}
...
var x = InputStream.Where(i => i.Key == "X");
var shift = InputStream.Where(i => i.Key == "Shift");
var ctrl = InputStream.Where(i => i.Key == "Ctrl");
// gets all key presses of X while shift and control are held
var ctrlShiftX = x.WhileHeld(shift).WhileHeld(ctrl);
这意味着捕获了持有密钥的第一个和最后一个按键,但我认为这不一定是坏事。
答案 2 :(得分:0)
我到目前为止最接近的是:
var ctrlShift = Observable.CombineLatest(ctrlHeld, shiftHeld)
.SelectMany(list =>
{
if (list.Any(o => o != list[0]))
return Observable.Empty<InputEvent>();
else
return Observable.Return(list[0]);
});
它完成了这项工作,但它有点难看。如果有任何答案比这更简洁或富有表现力,我会接受它