Windows 8里的标准化输入

http://blogs.msdn.com/b/windowsappdev/archive/2012/07/02/modernizing-input-in-windows-8.aspx

支持的输入类型:(mouse, touch, pen, touchpads)

输入来源(slate(平板), all-in-one(一体机), desktop, laptops(笔记本), convertibles)

The layers at the top of the input platform forcus on mainline scenarios, and lower layers progressively add flexibility and power

Finally, the Windows Runtime input APIs are at the bottom layer of the stack. These APIs (GestureRecognizer, PointerPoint, PointerDevice) provide complete flexibility and control, letting you have full access to the raw input data and its associated properties

there was “pointing input” like mouse, touch, and pen, “text input”, like keyboards and handwriting or speech recognition, and so forth

 

Take for example a very basic painting app – it wants to handle down, move, and up events from various inputs. If you want it to respond to touch, pen, and mouse, a naïve platform might force you to write 9 separate and redundant event handlers. Here’s what you might start with in this naïve platform:

// BAD! Don’t do this in your code.
class NaivePlatform
{
Dictionary<uint, Polyline> strokes = new Dictionary <uint, Polyline>();
Canvas PaintArea = new Canvas();

void OnMouseDown(MouseEventArgs e) { CommonDownHandler(e.Id, e.Position); }
void OnMouseMove(MouseEventArgs e) { CommonMoveHandler(e.Id, e.Position); }
void OnMouseUp(MouseEventArgs e) { CommonUpHandler(e.Id, e.Position); }

void OnPenDown(PenEventArgs e) { CommonDownHandler(e.Id, e.Position); }
void OnPenMove(PenEventArgs e) { CommonMoveHandler(e.Id, e.Position); }
void OnPenUp(PenEventArgs e) { CommonUpHandler(e.Id, e.Position); }

void OnTouchDown(TouchEventArgs e) { CommonDownHandler(e.Id, e.Position); }
void OnTouchMove(TouchEventArgs e) { CommonMoveHandler(e.Id, e.Position); }
void OnTouchUp(TouchEventArgs e) { CommonUpHandler(e.Id, e.Position); }

void CommonDownHandler(uint pointerId, Point position)
{
// Create a new stroke for this pointer and set its basic properties
var stroke = new Polyline();
stroke.Points.Add(position);
stroke.Stroke = new SolidColorBrush(Colors.Red);
stroke.StrokeThickness = 3;

// Add the stroke to dictionary so we can update it in CommonMoveHandler
strokes[pointerId] = stroke;

// Add the stroke to the canvas so that it is rendered
PaintArea.Children.Add(stroke);
}

void CommonMoveHandler(uint pointerId, Point position)
{
try
{
// Update the stroke associated to this pointer with the new point
strokes[pointerId].Points.Add(position);
}
catch (KeyNotFoundException)
{
// this pointer is not painting - ignore it
}
}

void CommonUpHandler(uint pointerId, Point position)
{
// This stroke is completed, so remove it from the dictionary.
// It will still be rendered because we are not removing it from the canvas.
strokes.Remove(pointerId);
}
}
上面的代码非常混乱,我们不需要根据输入的类型是mouse 还是pen ,我们应该采用输入的是pointing类型还是text

In the pointer version of the painting app, you instead have a simple set of down, move, and up handlers:

// GOOD! Do this instead of the previous code.
void OnPointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
// Retrieve current point, in the coordinate system of the painting area
var currentPoint = e.GetCurrentPoint(PaintArea);

// Create new stroke for this pointer and set its basic properties
var stroke = new Windows.UI.Xaml.Shapes.Polyline();
stroke.Points.Add(currentPoint.Position);
stroke.Stroke = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Red);
stroke.StrokeThickness = 3;

// Add the stroke to dictionary so we can update it in PointerMoved event handler
strokes[currentPoint.PointerId] = stroke;

// Add the stroke to the painting area so that it is rendered
PaintArea.Children.Add(stroke);
}

void OnPointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
// Retrieve current point, in the coordinate system of the painting area
var currentPoint = e.GetCurrentPoint(PaintArea);

try
{
// Update the stroke associated to this pointer with the new point
strokes[currentPoint.PointerId].Points.Add(currentPoint.Position);
}
catch (System.Collections.Generic.KeyNotFoundException)
{
// this pointer is not painting - ignore it
}
}

void OnPointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
// Retrieve current point, in the coordinate system of the painting area
var currentPoint = e.GetCurrentPoint(PaintArea);

// This stroke is completed, remove it from the dictionary.
// It will still be rendered because we are not removing it from PaintArea.
strokes.Remove(currentPoint.PointerId);
}

posted on 2012-09-04 09:27  GIS-MAN  阅读(281)  评论(0编辑  收藏  举报

导航