How to block keyboard input from Popup control panel

I want to get keyboard input from a Popup control that acts as the root visual for controlling a touch screen keyboard. I want the control to support keyboard input as well as touch screen. I hook events (PreviewKeyDown and KeyDown) and they never fire.

+3
source share
1 answer

A is Popupnot configurable by default, and even if it is customizable, you should have something else that you can focus on the popup, and then focus it or give it focus so that it receives a keyboard event.

In other words, if you want a keyboard event from to Popupuse Focusable="True"and place a controlled control like TextBoxeither Buttonor ListBoxand or to allow the user to give him focus by clicking on it or manually use it Focus()from the code. If you do all this, then it PreviewKeyDownshould work for Popup.

Here is a small demo program with a toggle button that opens Popup, and it shows that the slider is incremented whenever we get an event PreviewKeyDownon Popup:

<Grid>
    <StackPanel>
        <Slider Name="slider1"/>
        <ToggleButton x:Name="toggleButton1" 
                      Content="Open Popup"/>
    </StackPanel>
    <Popup PlacementTarget="{Binding ElementName=toggleButton1}" 
           IsOpen="{Binding IsChecked, ElementName=toggleButton1, Mode=OneWay}" 
           Focusable="True">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="PreviewKeyDown">
                <ei:ChangePropertyAction TargetObject="{Binding ElementName=slider1}" 
                                         PropertyName="Value" 
                                         Value="1" 
                                         Increment="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <TextBox Background="White" 
                 Focusable="True">
            <TextBox.Text>Sample Popup content.</TextBox.Text>
        </TextBox>
    </Popup>
</Grid>
+1
source

Source: https://habr.com/ru/post/1787807/


All Articles