Disable arrow keys for ScrollViewer

Kenny Young 21 Reputation points
2020-12-19T00:06:41.107+00:00

I've got a UWP XAML app where I am finding that my ScrollViewer (which I am using to get pinch zoom support) is picking up keyboard events for arrow keys and scrolling around, but I do not want it to.

[Confusing me further is that I've got a descendant of that ScrollViewer (a MyView in the markup below) that has focus, and that descendant does not get the keyboard events that I wish it would get - but I've managed to work around this via a global keyboard event listener.]

I'd appreciate some advice on how to either determine why I can't get keyboard events on the focused element, or how to make the ScrollViewer stop scrolling.

Here's my markup:

<ScrollViewer x:Name="Zoom" Grid.Column="1" Grid.Row="1" ZoomMode="Enabled" HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Grid x:Name="MyWrapper" HorizontalAlignment="Center" VerticalAlignment="Center" Width="1000" Height="1000">
        <Grid x:Name="ImageDestination"/>
        <ItemsControl x:Name="MyItemsControl" ItemsSource="{Binding MyItemsSource}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid TabFocusNavigation="Cycle"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:MyView IsTabStop="True" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="{Binding Margin}" Width="{Binding Size.Width}" Height="{Binding Size.Height}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</ScrollViewer>

Thanks,
Kenny

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 33,491 Reputation points Microsoft Vendor
    2020-12-21T06:54:15.91+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You could handle the UIElement.PreviewKeyDown Event to prevent the arrow keys.

    Like this:

    <ScrollViewer x:Name="Zoom"  PreviewKeyDown="Zoom_PreviewKeyDown"  Grid.Column="1" Grid.Row="1" ZoomMode="Enabled" HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">  
    

    Code behind:

     private void Zoom_PreviewKeyDown(object sender, KeyRoutedEventArgs e)  
            {  
                if (e.Key == Windows.System.VirtualKey.Up || e.Key == Windows.System.VirtualKey.Down || e.Key == Windows.System.VirtualKey.Left || e.Key == Windows.System.VirtualKey.Right)  
                {  
                    e.Handled = true;  
                }  
            }  
    

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kenny Young 21 Reputation points
    2020-12-21T19:17:50.027+00:00

    Thanks - I tried a lot of stuff but forgot to try this one. :-) Much appreciated!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.