Types of user input in UWP:
User input events can be grouped into three categories:
In UWP the UIElement class is a base class for most of the objects that have visual appearance and that can process user input. This is a list of events defined by UIElement:
The pointer events abstract touch, mouse, and pen input. They are:
Pointer events:
Properties and methods of the pointer event argument (PointerRoutedEventArgs):
The pointer abstraction involves the following classes:
Instead of low-level pointer events it is better to use gesture and manipulation events for consistent experience across all applications.
Example: Draw on Canvas by handling its PointerPressed, PointerMoved, and PointerReleased events:
<Page x:Class="WBS.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Background="White"> <Grid.RowDefinitions> <RowDefinition Height="0.1*" /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Text="Drawing on a Canvas by handling its PointerPressed, PointerMoved, and PointerReleased events." Foreground="Black" FontSize="25" Margin="20" /> <Canvas x:Name="Canvas1" Grid.Row="1" Background="Beige" PointerPressed="Canvas1_PointerPressed" PointerMoved="Canvas1_PointerMoved" PointerReleased="Canvas1_PointerReleased" /> </Grid> </Page>
using Windows.Foundation; using Windows.UI; using Windows.UI.Input; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Shapes; namespace WBS { public sealed partial class MainPage : Page { private bool isDrawing = false; public MainPage4() { this.InitializeComponent(); } private void Canvas1_PointerPressed(object sender, PointerRoutedEventArgs args) { PointerPoint point = args.GetCurrentPoint(Canvas1); DrawEllipse(point.Position, Colors.Red); isDrawing = true; args.Handled = true; } private void Canvas1_PointerMoved(object sender, PointerRoutedEventArgs args) { if (isDrawing) { PointerPoint point = args.GetCurrentPoint(Canvas1); DrawEllipse(point.Position, Colors.Green); } args.Handled = true; } private void Canvas1_PointerReleased(object sender, PointerRoutedEventArgs args) { PointerPoint point = args.GetCurrentPoint(Canvas1); DrawEllipse(point.Position, Colors.Blue); isDrawing = false; args.Handled = true; } private void DrawEllipse(Point pos, Color col) { const double radius = 20.0; Ellipse circle = new Ellipse { Width = radius * 2, Height = radius * 2, Fill = new SolidColorBrush(col) }; Canvas.SetLeft(circle, pos.X - radius); Canvas.SetTop(circle, pos.Y - radius); Canvas1.Children.Add(circle); } } }