设置boolean
标志基于在按键事件中按下的键。
在OnMouseMove
记录鼠标位置,如果为null。否则,计算行进距离,并根据已设置的加速或减速标志对其进行放大或减弱。
要抑制或放大,一旦X和Y从最后一点发生变化,乘以2或除以2 ...(您可以选择自己的数字),现在将新的YX变化添加到当前鼠标XY坐标并设置鼠标位置。
下面是MouseMove
的样子,以及一些需要的私有变量。在我的例子中,你必须包含Forms
作为参考。我没有在我的Include语句中包含Forms,因为它会在WPF应用程序中分解IntelliSense。你仍然需要保持这些_speedUp
和_slowDown
变量与KeyDown
事件
private bool entering = true;
private Point _previousPoint;
private bool _speedUp;
private bool _slowDown;
private double _speedMod = 2;
private double _slowMod = .5;
private void OnMouseMove(object sender, MouseEventArgs e)
{
Point curr = new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
if (entering)
{
_previousPoint = curr;
entering = false;
}
if (_previousPoint == curr)
return; // The mouse hasn't really moved
Vector delta = curr - _previousPoint;
if (_slowDown && !_speedUp)
delta *= _slowMod;
else if (_speedUp && !_slowDown)
delta *= _speedMod;
else
{
_previousPoint = curr;
return; //no modifiers... lets not do anything
}
Point newPoint = _previousPoint + delta;
_previousPoint = newPoint;
//Set the point
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)newPoint.X, (int)newPoint.Y);
}
编辑:我把按键事件在我的窗口定义,以及它工作得很好。虽然正如该主题的评论中指出的那样,使用Keyboard.IsKeyDown
要简单得多。我还编辑上面以不引起奇怪的跳跃代码发出
private void Window_KeyDown(object sender, KeyEventArgs e)
{
_slowDown = true;
if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
_slowDown = true;
else if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
_speedUp = true;
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
_slowDown = false;
else if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
_speedUp = false;
}
private void Window_MouseLeave(object sender, MouseEventArgs e)
{
entering = true;
}
也许这将帮助:http://stackoverflow.com/questions/5750722/how-to-detect-modifier-key-states-in-wpf – dkozl
@dkozl做了诡计,你可以张贴这个作为答案请。 – Jimmy