locked
C# Metro - KeyDown not working

    Question

  • I'm trying to move a image left or right, depending on left or right key is pressed on the keyboard.

    However my code does not work, and I don't know why...

        <Grid x:Name="HomePage" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <Canvas x:Name="screen" HorizontalAlignment="Stretch" Height="Auto" Margin="10,0,-10,0" VerticalAlignment="Stretch" Width="Auto" Grid.RowSpan="2">
                <Image x:Name="player" Height="100" Canvas.Left="572" Canvas.Top="452" Width="100" Source="Image/floth-crystal-smiley100.png" KeyDown="on_KeyDown" KeyUp="on_KeyUp" />
            </Canvas>
        </Grid>

    protected override void OnNavigatedTo(NavigationEventArgs e)
            {
     
                DispatcherTimer timer = new DispatcherTimer();
                timer.Tick += timer_tick;
                timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
                timer.Start();
            }
    
            private void timer_tick(object sender, object e)
            { 
                double position_x = Canvas.GetLeft(player); 
    
                if (left==true)
                {
                    position_x -= 5;
                    Canvas.SetLeft(player, position_x);
                }
                if (right==true)
                {
                    position_x += 5;
                    Canvas.SetLeft(player, position_x);
                }
            }
    
            bool left;
            bool right;
    
    
            private void on_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Left)
                {
                    left = true;
                }
                if (e.Key == Windows.System.VirtualKey.Right)
                {
                    right = true;
                }
            }
    
            private void on_KeyUp(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Left)
                {
                    left = false;
                }
                if (e.Key == Windows.System.VirtualKey.Right)
                {
                    right = false;
                }
            }

    Tuesday, June 10, 2014 8:55 PM

Answers

  • Hi ImNew54,

    The reason is your on_KeyDown or on_KeyUp event does not fired, to simply test it, you could set a breakpoint at your code and when you press the left or right key, the event wont fire.

    To make your code work, you should look at the documentation: Keyboard interactions.section: Shortcut keys example, where you can find how to programmatically set the focus and how to listen to the key press event.

    If you still have question on how to implement this, feel free to ask. :)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, June 11, 2014 2:00 AM
    Moderator
  • I solved it by adding a button and giving it Focus all the time.

    Xaml:

        <Grid x:Name="HomePage" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" KeyDown="on_KeyDown" KeyUp="on_KeyUp">
            <Canvas x:Name="screen" HorizontalAlignment="Stretch" Height="Auto" Margin="10,0,-10,0" VerticalAlignment="Stretch" Width="Auto" Grid.RowSpan="2">
                <Image x:Name="player" Height="100" Canvas.Left="572" Canvas.Top="452" Width="100" Source="Image/floth-crystal-smiley100.png"/>
                <Button x:Name="button1" Content="Button" Canvas.Left="118" Canvas.Top="670" Click="button1_Click"/>
            </Canvas>
        </Grid>

    C#:

     private void timer_tick(object sender, object e)
            { 
                double position_x = Canvas.GetLeft(player);
                //---------------------------
                //Setting focus to the button
                button1.Focus(Windows.UI.Xaml.FocusState.Programmatic);
    
                if (left==true)
                {
                    position_x -= 5;
                    Canvas.SetLeft(player, position_x);
                }
                if (right==true)
                {
                    position_x += 5;
                    Canvas.SetLeft(player, position_x);
                }
            }

    Wednesday, June 11, 2014 11:29 AM

All replies

  • Hi ImNew54,

    The reason is your on_KeyDown or on_KeyUp event does not fired, to simply test it, you could set a breakpoint at your code and when you press the left or right key, the event wont fire.

    To make your code work, you should look at the documentation: Keyboard interactions.section: Shortcut keys example, where you can find how to programmatically set the focus and how to listen to the key press event.

    If you still have question on how to implement this, feel free to ask. :)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, June 11, 2014 2:00 AM
    Moderator
  • I solved it by adding a button and giving it Focus all the time.

    Xaml:

        <Grid x:Name="HomePage" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" KeyDown="on_KeyDown" KeyUp="on_KeyUp">
            <Canvas x:Name="screen" HorizontalAlignment="Stretch" Height="Auto" Margin="10,0,-10,0" VerticalAlignment="Stretch" Width="Auto" Grid.RowSpan="2">
                <Image x:Name="player" Height="100" Canvas.Left="572" Canvas.Top="452" Width="100" Source="Image/floth-crystal-smiley100.png"/>
                <Button x:Name="button1" Content="Button" Canvas.Left="118" Canvas.Top="670" Click="button1_Click"/>
            </Canvas>
        </Grid>

    C#:

     private void timer_tick(object sender, object e)
            { 
                double position_x = Canvas.GetLeft(player);
                //---------------------------
                //Setting focus to the button
                button1.Focus(Windows.UI.Xaml.FocusState.Programmatic);
    
                if (left==true)
                {
                    position_x -= 5;
                    Canvas.SetLeft(player, position_x);
                }
                if (right==true)
                {
                    position_x += 5;
                    Canvas.SetLeft(player, position_x);
                }
            }

    Wednesday, June 11, 2014 11:29 AM
  • Hi ImNew54,

    Nice to see you have a solution.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, June 12, 2014 1:01 AM
    Moderator