Left mouse button click detect on winform using Reactive extensions IObservable on events

*爱你&永不变心* 提交于 2020-01-06 14:01:31

问题


Just started reading on Reactive extensions. I am trying to watch a simple left mouse button click on my winform. Meaning anywhere there is a click (on any control on the form including the form) I just want to display a message "Click detected". So far I have

var mouseDown = Observable.FromEvent<MouseButtonEventArgs>(frmMain, "MouseDown");
//missing code please fill here 
mouseDown.Subscribe(() => Debug.WriteLine("left click detected.");

I know the first line will detect any mouse event. I want just the left mouse button click. Please post working code so I can understand this better. Right now in a tailspin with buzzwords I have never used before like .takeuntil etc.. Further refining my question. What is the Rx equivalent of

protected override void WndProc(ref Message m)
{
    Console.Writeline("{0}", m.Msg);
}

That should observe every observable mouse or keyboard event. thank you


回答1:


Sorry, I'm not sure if thats working code (can't try it now) but it should get you started.

var mouseDown = Observable.FromEvent<MouseButtonEventArgs>(frmMain, "MouseDown")
                      .Where(x => x.LeftButton == MouseButtonState.Pressed);

mouseDown.Subscribe(() => Debug.WriteLine("left click detected.");


来源:https://stackoverflow.com/questions/6298496/left-mouse-button-click-detect-on-winform-using-reactive-extensions-iobservable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!