aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

GamepadTrigger to Open Button Context Menu with Gamepad

15 Oct 2024, 19:56

I have a button with a context menu that can be accessed via right clicking and I am trying to have it work with gamepad input as well. Adding an event trigger for the mouse click event does seem to work (as in the example below) but adding a GamepadTrigger seems to be ineffective. It also doesn't seem to matter what button I use for the GamepadTrigger.

Is there an intended way to bind a gamepad button to an event like opening the context menu? Ideally, the approach is in xaml since this button is part of a ControlTemplate and has no code behind.
<ControlTemplate x:Key="MyControlTemplate">
        <Grid x:Name="Root">
            <b:Interaction.Triggers>
                <!-- Various DataTriggers to change visibility of context menu items -->
            </b:Interaction.Triggers>

            <Button x:Name="MyButton" Style="{StaticResource Button.Style}" IsEnabled="True">
                <b:Interaction.Triggers>
                    <b:EventTrigger EventName="Click">
                        <b:ChangePropertyAction TargetObject="{Binding ContextMenu, ElementName=MyButton}" PropertyName="PlacementTarget" Value="{Binding ElementName=MyButton, Mode=OneWay}"/>
                        <b:ChangePropertyAction TargetObject="{Binding ContextMenu, ElementName=MyButton}" PropertyName="IsOpen" Value="True"/>
                    </b:EventTrigger>
                    <noesis:GamepadTrigger Button="Accept" ActiveOnFocus="True">
                        <b:ChangePropertyAction TargetObject="{Binding ContextMenu, ElementName=MyButton}" PropertyName="PlacementTarget" Value="{Binding ElementName=MyButton, Mode=OneWay}"/>
                        <b:ChangePropertyAction TargetObject="{Binding ContextMenu, ElementName=MyButton}" PropertyName="IsOpen" Value="True"/>
                    </noesis:GamepadTrigger>
                </b:Interaction.Triggers>
                <Button.ContextMenu>
                    <ContextMenu>
                        <!-- some menu items -->
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>
        </Grid>
    </ControlTemplate>

I tried to search the forum for similar issues and I wasn't able to find anything. My apologies if this was already addressed somewhere and I missed it!
 
aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

Re: GamepadTrigger to Open Button Context Menu with Gamepad

15 Oct 2024, 20:33

The gamepad "A" button is able to trigger the button click event when the button is focused but if I only have the GamepadTrigger nothing seems to happen.

Edit: Not resolved, still seeing the issue.
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: GamepadTrigger to Open Button Context Menu with Gamepad

21 Oct 2024, 13:06

Hi,

Gamepad input is treated as keyboard key presses, and the GamepadTrigger just hooks to the KeyDown/Up events to check if it should fire. Key events are raised from the control with focus up the UI tree, so if the GamepadTrigger has the ActiveOnFocus="True" it will only fire if it is attached to the control having focus.

In the xaml you pasted it is redundant to add a GamepadTrigger because the button will automatically fire the Click event when you hit the "Accept" button in the gamepad while the Button has focus. If you want the GamepadTrigger to fire if the Button doesn't have focus, then you need to leave ActiveOnFocus="False", so the trigger registers against the root of the UI tree.

Have you tried that?
 
aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

Re: GamepadTrigger to Open Button Context Menu with Gamepad

23 Oct 2024, 17:30

Sorry for the confusion, the click event does work with the gamepad but I specifically want to avoid the click event since this is intended to be a gamepad only interaction. I have tried to use the `GamePadTrigger` with both `ActiveOnFocus="True"` and `ActiveOnFocus="False"` but in both cases the button is not activated.

I believe that something else may be blocking the GamepadTrigger from receiving the input. Are there other UI Elements that can block controller input?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: GamepadTrigger to Open Button Context Menu with Gamepad

24 Oct 2024, 10:15

There are many controls that handle the gamepad Accept input when they are focused (like all kind of Buttons, ListBoxes, TreeViews, ComboBoxes, Sliders, TextBoxes, MenuItems...) because it is used to activate their main logic (clicking in buttons, selection in lists, enter focus engagement for sliders or text boxes, etc.).

Just to understand your sceneario, you need to open a ContextMenu of a Button using the gamepad Accept button, even when the focus is not placed on that button? or only when the focus is placed in that button?
 
aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

Re: GamepadTrigger to Open Button Context Menu with Gamepad

08 Nov 2024, 21:28

Sorry for the delayed response!

My goal is to have a button which opens a context menu when it is right clicked or when it is focused with a gamepad and a gamepad button is pressed (ex. 'A' or 'X'). It seems like the logic to open the context menu works correctly because I can have it run based on the click event. In this case focusing the button and pressing 'A' works as expected:
<Button x:Name="TemplateButton" Focusable="True">
           <b:Interaction.Triggers>
               <b:EventTrigger EventName="Click">
                   <b:ChangePropertyAction TargetObject="{Binding ContextMenu, ElementName=TemplateButton}" PropertyName="IsOpen" Value="True"/>
               </b:EventTrigger>
           </b:Interaction.Triggers>
           <Button.ContextMenu> ... </Button.ContextMenu>
</Button>
However, when I switch this to use a GamepadTrigger the context menu no longer opens:
<Button x:Name="TemplateButton" Focusable="True">
           <b:Interaction.Triggers>
               <noesis:GamepadTrigger Button="Accept" ActiveOnFocus="True">
                   <b:ChangePropertyAction TargetObject="{Binding ContextMenu, ElementName=TemplateButton}" PropertyName="IsOpen" Value="True"/>
               </noesis:GamepadTrigger>
           </b:Interaction.Triggers>
           <Button.ContextMenu> ... </Button.ContextMenu>
</Button>
I am able to confirm that the button does in fact get focus. Also, the direct predecessors of this button in the hierarchy are only ContentControls, ItemsControls, Grids, StackPanels, Expanders, and other containers that aren't binding inputs (as far as I can tell)
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: GamepadTrigger to Open Button Context Menu with Gamepad

12 Nov 2024, 10:32

As I mentioned the Accept button is handled by the focused Button to raise the Click event, so the GamepadTrigger won't receive the button press event.

I have tested other gamepad buttons work as expected and open the ContextMenu, though. Could that be an option?
<Button x:Name="TemplateButton" Focusable="True">
  <b:Interaction.Triggers>
    <noesis:GamepadTrigger Button="Context1" ActiveOnFocus="True">
      <b:ChangePropertyAction TargetObject="{Binding ContextMenu, ElementName=TemplateButton}" PropertyName="IsOpen" Value="True"/>
    </noesis:GamepadTrigger>
  </b:Interaction.Triggers>
  <Button.ContextMenu> ... </Button.ContextMenu>
</Button>

Who is online

Users browsing this forum: No registered users and 5 guests