設置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