简体   繁体   English

C#检测鼠标是否向左移动5次并向右移动5次

[英]C# Detect if Mouse moves 5 times to left and 5 times to right

Is there a way to detect this? 有没有办法检测到这一点?

Imagine you would have an application where you are doing something and there is a Backgroundthread which is listening and waiting until the mouse will be moved - (no matter how far): 想象一下,如果您有一个正在做某事的应用程序,并且有一个Backgroundthread正在监听并等待,直到鼠标将被移动-(无论距离多远):

Left
Right
Left
Right
Left
Right
Left
Right
Left
Right

Which fires a trigger or calls a specific method. 触发触发器或调用特定方法。

Any ideas? 有任何想法吗?

Don't see, from the question provided perspective to use another thread for this. 从提供的问题的角度来看,没有看到为此使用另一个线程。 Just listen for MouseMove event over whatevere is it, and detect mouse move direction. 只需监听MouseMove事件,然后检测鼠标的移动方向即可。

You can do that by substracting corrdinates recived by the event handler itself. 您可以通过减去事件处理程序本身获取的corcordinates来做到这一点。

The thing here comes to decision of tollerance you are going to have in detection algo. 这里的事情取决于您在检测算法中将要决定的收费标准

To be more clear: 更清楚地说:

If the difference of X value is bigger then (say) 1 , count that as move to right , if it less then -1 , count that like move to left . 如果 X值之差较大(例如) 1 ,则算为向右移动,如果X值之差小于-1 ,则算为向左移动。

Something like this. 这样的事情。

Get the current position of mouse pointer. 获取鼠标指针的当前位置。 Move to new location. 移到新位置。 If the new location's X-Cordinate is lesser than the previuos position's X-Cordinate then it means mouse has moved left. 如果新位置的X坐标小于上一个位置的X坐标,则表示鼠标已向左移动。 Similary for moving right. 向右移动相似。 Every time the new position will become base position. 每次新的职位将成为基本职位。 You can keep a counter and if count is 5 do what ever you wish. 您可以保留一个计数器,如果计数为5,则可以执行任何操作。

Use the MouseMove event attached to your window. 使用窗口附带的MouseMove事件。 Store the mouse position each time it fires and compare the current position to the last position to determine if you moved left, right, or not all all (ie up or down). 每次触发时存储鼠标位置,并将当前位置与最后一个位置进行比较,以确定是否向左,向右或不是全部移动(即向上或向下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM