在UWP中触摸移动虚拟鼠标会导致光标闪烁

时间:2019-05-26 05:06:06

标签: c# uwp touch

我正在尝试模拟Windows 10的虚拟触摸板的行为,即当用户触摸应用程序内的控件并四处移动手指时,系统光标将反映其移动。但是,我注意到在同时处理触摸和鼠标输入的系统之间似乎存在一些冲突,其中触摸输入要隐藏光标,而鼠标输入要显示光标。

我首先在VS-2019中构建样板UWP App,这是我的MainPage.xaml,我唯一要更改的就是为Grid提供 Name =“ Touchpad” 属性,因此我可以跟踪其中的指针事件:

<Page
    x:Class="Playground.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Playground"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid Background="Black" Name="Touchpad">

    </Grid>
</Page>

我正在使用Windows.UI.Input.Preview.Injection移动鼠标光标。因为这是受限功能,所以我对项目进行了更改,如下所示:https://docs.microsoft.com/en-us/uwp/api/windows.ui.input.preview.injection#remarks

public sealed partial class MainPage : Page
    {
        private Point lastPosition;
        private InputInjector inputInjector = InputInjector.TryCreate();

        public MainPage()
        {
            this.InitializeComponent();
            ApplicationView.PreferredLaunchViewSize = new Size(480, 480);
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

            Touchpad.PointerPressed += new PointerEventHandler(Touchpad_PointerPressed);
            Touchpad.PointerReleased += new PointerEventHandler(Touchpad_PointerReleased);
            Touchpad.PointerMoved += new PointerEventHandler(Touchpad_PointerMoved);
        }

        private void Touchpad_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            e.Handled = true;
            PointerPoint pointer = e.GetCurrentPoint(Touchpad);

            Point currentPosition = pointer.Position;

            if (pointer.PointerDevice.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch &&
                lastPosition != currentPosition &&
                pointer.Properties.IsPrimary == true)
            {
                Point delta = new Point(currentPosition.X - lastPosition.X, currentPosition.Y - lastPosition.Y);
                InjectedInputMouseInfo mouseInfo = new InjectedInputMouseInfo();
                mouseInfo.MouseOptions = InjectedInputMouseOptions.Move;
                mouseInfo.DeltaX = (int)delta.X;
                mouseInfo.DeltaY = (int)delta.Y;                
                if (inputInjector != null)
                {
                    inputInjector.InjectMouseInput(new[] { mouseInfo });
                }
                lastPosition = currentPosition;
            }
        }

        private void Touchpad_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            e.Handled = true;
            lastPosition = new Point(0,0);
        }

        private void Touchpad_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            e.Handled = true;

            PointerPoint pointer = e.GetCurrentPoint(Touchpad);
            if (pointer.PointerDevice.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch &&
                pointer.Properties.IsPrimary == true)
            {
                lastPosition = pointer.Position;
                Window.Current.CoreWindow.PointerCursor = null;
            }
        }
    }

我注意到,在四处移动手指时,似乎有两个光标(或者一个真的来回快速地跳动)-一个在应用程序本身中,一个在系统/桌面中。我添加了Window.Current.CoreWindow.PointerCursor = null;,它已成功将光标隐藏在应用程序内部,但是注入驱动的光标正在闪烁。

据我所知,Win10中虚拟触摸板的反馈非常流畅,据我所知,它也是一个UWP应用...

0 个答案:

没有答案