Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I continue to struggle with my UWP framework...

I can drag XAML-things around with my mousepointer.

I can also add KeyDown events to the Page, and these work fine.

However, whenever I have clicked somewhere with the mouse, the KeyDown event doesn't fire anymore (I can see that it's not even reached, when trying with breakpoints).


And so, it seems like the mouseclick is taking over focus somehow. Is there a way to kill the mouseclick event or similar?

I wrote the code below, but it doesn't do any good.

C#
public MainPage()
{
    this.KeyDown += Page_KeyDown;

    this.InitializeComponent();



Here is also the beginning of my XAML code:

HTML
<Page
    x:Class="Spritehand.PhysicsHelper.Demos.UWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Spritehand.PhysicsHelper.Demos.UWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ph="using:Spritehand.FarseerHelper"
    mc:Ignorable="d" KeyDown="Page_KeyDown">

    <Grid x:Name="LayoutRoot" removed="CornflowerBlue">
        <Canvas x:Name="cnvGameOuter"  >
            <ph:PhysicsCanvas x:Name="cnvGame" removed="Green" GravityHorizontal="0" GravityVertical="0" DebugMode="False"  Width="1366" Height="768" MousePickEnabled="true">

                <local:ucBlock Canvas.Left="150" Canvas.Top="10" />


What I have tried:

I have tried to trace the fault using breakpoints and I have come up with the error described above.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jun-16 13:15pm    
Not clear. The term focus is only related to keyboard focus. There is no any other "focus"; there is nothing directly related to mouse click. A click may or may not cause focusing. You handle keyboard events only of the currently focusing controls. If you don't want it, you have to either disable focusing, or handle "preview" keyboard events. It means that the routed event is handled on its way in the tunneling routing strategy, when it is dispatched to the topmost parent element (such as your page) and not to the child which ultimately completes event handling.
—SA
petter2012 18-Jun-16 13:33pm    
Yes, thanks, this is what happens:

First, the Page has (probably) focus and when I click my keyboard keys (Up Arrow for making the player go up for example) it works fine.
Then, I click somewhere in the game window with the mouse.
Now, the keys don't work anymore. I hope that's clearer.

That's why I tried the this.KeyDown code. I tried Page.KeyPreview = True, but KeyPreview doesn't seem to be available in UWP (?).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900