Page 1 of 1

KeyTrigger does not work

Posted: 14 Nov 2020, 05:11
by Steback
Hi, I am trying to use a command when a key is pressed. But the KeyTrigger does not work. I saw some samples and other posts but nothing. I am very new to Noesis so I don't know why it doesn't work.

Here is the xaml code
<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
    xmlns:local="clr-namespace:GameGUI"
    x:Class="GameGUI.Loading"
    x:Name="LoadingControl" Focusable="True" IsTabStop="False">
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding State}" Comparison="NotEqual" Value="{x:Static local:State.Loading}">
            <ei:ChangePropertyAction PropertyName="Visibility" Value="Hidden" />
        </ei:DataTrigger>
        <ei:DataTrigger Binding="{Binding State}" Comparison="Equal" Value="{x:Static local:State.Loading}">
            <ei:ChangePropertyAction PropertyName="Visibility" Value="Visible" />
        </ei:DataTrigger>
        <ei:KeyTrigger Key="Esc" ActiveOnFocus="False">
            <i:InvokeCommandAction Command="{Binding Game}"/>
        </ei:KeyTrigger>
    </i:Interaction.Triggers>
    <Grid x:Name="LayoutRoot">
        <Viewbox>
            <StackPanel Margin="30">
                <TextBlock Foreground="White" Text="Loading..."/>
                <TextBlock Foreground="White" Text="Press Esc key to start"/>
            </StackPanel>
        </Viewbox>
    </Grid>
</UserControl>
Those are the components that I included. Prehaps I forgot include another.
    Noesis::TypeOf<NoesisApp::Interaction>();

    Noesis::RegisterComponent<NoesisApp::DataTrigger>();
    Noesis::RegisterComponent<NoesisApp::TriggerCollection>();
    Noesis::RegisterComponent<NoesisApp::KeyTrigger>();
    Noesis::RegisterComponent<NoesisApp::InvokeCommandAction>();
    Noesis::RegisterComponent<NoesisApp::ChangePropertyAction>();
    Noesis::RegisterComponent<NoesisApp::EventTrigger>();
    Noesis::RegisterComponent<NoesisApp::PropertyChangedTrigger>();
    Noesis::RegisterComponent<NoesisApp::TriggerActionCollection>();
    Noesis::RegisterComponent<NoesisApp::SetFocusAction>();
    Noesis::RegisterComponent<Noesis::EnumConverter<NoesisApp::ComparisonConditionType>>();
    Noesis::RegisterComponent<Noesis::EnumConverter<NoesisApp::KeyTriggerFiredOn>>();

Re: KeyTrigger does not work

Posted: 15 Nov 2020, 13:02
by sfernandez
For a KeyTrigger to work (and the same happens for KeyBindings) the KeyDown routed event should be raised by some element inside the control defining the trigger or the control itself. This means that the keyboard focus should be set to any element inside the control or the control itself.

Image
The order of routed events processing is as follows:
  1. PreviewKeyDown (tunnel) on root element.
  2. PreviewKeyDown (tunnel) on intermediate element #1.
  3. PreviewKeyDown (tunnel) on source focused element #2.
  4. KeyDown (bubble) on source focused element #2.
  5. KeyDown (bubble) on intermediate element #1.
  6. KeyDown (bubble) on root element.

Re: KeyTrigger does not work

Posted: 17 Nov 2020, 11:30
by sfernandez
I see you have marked the post as solved, did you finally make it work?

Re: KeyTrigger does not work

Posted: 18 Nov 2020, 02:07
by Steback
Yes, it finally worked. Thanks for the help!

Re: KeyTrigger does not work

Posted: 18 Nov 2020, 10:14
by sfernandez
Great, thanks for the update.