locked
Set focus on SwapChainPanel control

    Question

  • Hello.

    Could someone say how to set focus on SwapchainPanel control without placing controls inside swapchainpanel?

    There is no method Focus() or somethong like this or this is completely impossible?

    Monday, August 11, 2014 10:16 PM

Answers

  • Hi Black_Joker,

    Correct, there is no focus method for SwapChainPanel, it inherit from Grid which does not have a focus method.

    You can also ref to the documentation: SwapChainPanel class

    The SwapChainPanel class does not inherit from the Control class, so you can't programmatically focus it directly for purposes of capturing key events. Consider setting the focus to a focusable element inside the panel and letting the key event bubble.

    --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.

    Tuesday, August 12, 2014 2:33 AM
    Moderator
  • Well, let's say if you are so luck and you have a SwapChainPanel with foucs method, you can set focus to it programmatically, meanwhile there is a textbox outside/inside swapchain, how will you manage the keyevent?

    The solution I provide is to treat the button as SwapChainPanel and if you need focus on SwapChainPanel, focus to that 0 size button, the button will route the key event to its parent SwapChainPanel.

    --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, August 13, 2014 7:39 AM
    Moderator

All replies

  • Hi Black_Joker,

    Correct, there is no focus method for SwapChainPanel, it inherit from Grid which does not have a focus method.

    You can also ref to the documentation: SwapChainPanel class

    The SwapChainPanel class does not inherit from the Control class, so you can't programmatically focus it directly for purposes of capturing key events. Consider setting the focus to a focusable element inside the panel and letting the key event bubble.

    --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.

    Tuesday, August 12, 2014 2:33 AM
    Moderator
  • But In my case its not very convenient, because I use SwapchainPanel for displaying DirectX backbuffer. So, actually I want to capture keyevents with help of DirectInput, but this also not possible while focus is not on SwapChainPanel.

    So, if there is no possibility to set focus programmatically, which control would be better to set in my case? It must not capture space/enter butons and must be invisible.

    Tuesday, August 12, 2014 7:02 AM
  • Hi Black_Joker,

    Seems there is no other way to route the key event to the SwapChainPanel.

    I would put a button inside with a 0 height and 0 width, you would not see the button from your screen but you can capture the event:

            <SwapChainPanel x:Name="scp" Margin="127,128,464,278" KeyDown="scp_KeyDown">
                <Button x:Name="bt" Content="Button" HorizontalAlignment="Left" Height="0"  VerticalAlignment="Top" Width="0" KeyDown="bt_KeyDown"/>
            </SwapChainPanel>

            public MainPage()
            {
                this.InitializeComponent();
                bt.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                
            }
    
            private void bt_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                e.Handled = false;
            }
    
            private void scp_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                // this line will be reached
            }

    --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.

    Tuesday, August 12, 2014 10:01 AM
    Moderator
  • But If I have also other controls inside and outside Swapchain? Ho could I correctly set focus back? For ex. if I have a textbox outside and especially inside swapchain? It will always keep focus and interfereю So, I will need to move focus to other element explicit....

    Am I right with it?

    Wednesday, August 13, 2014 7:28 AM
  • Well, let's say if you are so luck and you have a SwapChainPanel with foucs method, you can set focus to it programmatically, meanwhile there is a textbox outside/inside swapchain, how will you manage the keyevent?

    The solution I provide is to treat the button as SwapChainPanel and if you need focus on SwapChainPanel, focus to that 0 size button, the button will route the key event to its parent SwapChainPanel.

    --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, August 13, 2014 7:39 AM
    Moderator
  • OK, I understood you. Thanks for help.
    Wednesday, August 13, 2014 9:06 AM