none
Polygon Keyboard Focus

    Question

  • Can a polygon be assigned keyboard focus on a PointerPressed event?

    I can get the pointer captured by the polygon

    myPolygon.CapturePointer(e.Pointer);

    but even so

    myPolygon.Focus...

    does not appear to be an option... 

     

    Assuming the answer is no, I can't seem to get a Polygon KeyDown event to fire:

    myPolygon.KeyDown += new KeyEventHandler(myPolygon_KeyDown);

    is not invoked when pressing a key during pointer capture, presumably for lack of focus.

     

    Alternatively I try to control the polygon indirectly via the parent canvas:

    myCanvas.KeyDown += new  KeyEventHandler(myCanvas.KeyDown);

    which works in SilverLight but again (here in Win Store App) pressing a key doesn't invoke the handler.

     

    Previous posts would lead me to believe

    Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;

    should work, but I don't know how to monitor the keydown events with this...

    Is there a code example for implementing this strategy?  Or a brief explanation?

     

    Thanks,,,


    torpedo99

    Sunday, December 30, 2012 3:50 PM

Answers

  • An example:

    <Page
        x:Class="App21.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App21"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Button Content="Go"/>
            <ContentControl Grid.Row="1" KeyDown="c_KeyDown_1" PointerPressed="c_PointerPressed_1">
                <Polygon Points="300,200 400,125 400,275 300,200" Stroke="Red" StrokeThickness="1">
                    <Polygon.Fill>
                        <SolidColorBrush Color="Yellow"/>
                    </Polygon.Fill>
                </Polygon>
            </ContentControl>
        </Grid>
    </Page>

    ---

    using System;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Input;
    namespace App21
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
            private void c_KeyDown_1(object sender, KeyRoutedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("c_KeyDown_1:{0}", DateTime.Now);
            }
            private void c_PointerPressed_1(object sender, PointerRoutedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("c_PointerPressed_1:{0}", DateTime.Now);
                // ...
                ((Control)sender).Focus(Windows.UI.Xaml.FocusState.Programmatic);
                e.Handled = true; // compulsory
            }
        }
    }

    Note :

    - "Can a polygon be assigned keyboard focus" : no.
    - The concept consists in working with the 'Control' framework element in order to properly operate focus and keyboard. Neither Polygon/Shape nor Canvas/Panel inherit from Control.

    Sunday, December 30, 2012 5:44 PM

All replies

  • the Pointer events if for mouse not for keyboard. For catch the keyboard events you should look for keyUp event or KeyDown event, like you did.

    Maybe this don´t raise, because don´t have focus, not? Try to subscribe that events in the page ...


    Sara Silva
    My blog | My Windows 8 Store Apps Samples | More Samples
    Follow me in Twitter @saramgsilva

    My Windows 8 Store Apps: Female Pill | Galinho (Tic tac Toe) | 24 | My Snake
    My Windows Phon



    Sunday, December 30, 2012 4:19 PM
  • An example:

    <Page
        x:Class="App21.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App21"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Button Content="Go"/>
            <ContentControl Grid.Row="1" KeyDown="c_KeyDown_1" PointerPressed="c_PointerPressed_1">
                <Polygon Points="300,200 400,125 400,275 300,200" Stroke="Red" StrokeThickness="1">
                    <Polygon.Fill>
                        <SolidColorBrush Color="Yellow"/>
                    </Polygon.Fill>
                </Polygon>
            </ContentControl>
        </Grid>
    </Page>

    ---

    using System;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Input;
    namespace App21
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
            private void c_KeyDown_1(object sender, KeyRoutedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("c_KeyDown_1:{0}", DateTime.Now);
            }
            private void c_PointerPressed_1(object sender, PointerRoutedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("c_PointerPressed_1:{0}", DateTime.Now);
                // ...
                ((Control)sender).Focus(Windows.UI.Xaml.FocusState.Programmatic);
                e.Handled = true; // compulsory
            }
        }
    }

    Note :

    - "Can a polygon be assigned keyboard focus" : no.
    - The concept consists in working with the 'Control' framework element in order to properly operate focus and keyboard. Neither Polygon/Shape nor Canvas/Panel inherit from Control.

    Sunday, December 30, 2012 5:44 PM
  • Thanks for getting me on track.

    This works:

    <ContentControl KeyDown = "myKeyDown"/>

    ----> code behind:  key stroke in app window successfully invokes the event handler

    But doesn't seem to work with a Canvas:

    <Canvas KeyDown = "myKeyDown"/>

    ----> code behind:  key stroke in app window fails to invoke the event handler

    If I am correct why is this?


    torpedo99

    Sunday, December 30, 2012 9:10 PM
  • Your scenario requires setting Focus. This operation is provided for framework elements of type 'Control', not for 'Canvas/Panel'.
    Monday, December 31, 2012 8:23 AM