After a long and fruitless search here, this is my problem. I have a WPF UserControl that I display in a WinForms Form via an ElementHost. The development is almost done, but there are still two minor side effects.
First, if I open the ContextMenu, I expected that I can choose the items via the Cursor-Keys Up/Down. But this keypresses seem to be missing. The second is, that, if I query the Keyboard.Modifiers explicitly to determine if the Ctrl-Key is pressed, Keyboard.Modifiers always returns 'None'. I far as I read here, I assume that it has something to do with the Keyboard or Focusstates. I often read here as solution to call Integration.ElementHost.EnableModelessKeyboardInterop(window) but I do not have a WPF-Window, I just have a WPF-UserControl.
I created a new simplified project to reduced complexity to a minimum.
XAML
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="300">
<TextBlock Text="This is a Demo-Text">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="First" Click="MenuItem_Click"></MenuItem>
<MenuItem Header="Second"></MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</UserControl>
Code behind
Public Class UserControl1
Private Sub MenuItem_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
If Keyboard.Modifiers = ModifierKeys.None Then
Debug.WriteLine("Modifiers = None") ' <= I am always here, even if I press Ctrl
Else
Debug.WriteLine("Modifiers = Not None")
End If
End Sub
End Class
I put this Control into an ElementHost inside a WinForms-Window. If I have anything else inside the WinForms-Window (of course I have) that can capture the focus, I still can open the ContextMenu via a right click on the WPF-Control, but pressing the Cursor keys up or down does not select any of the two items. If I click the first item and hold the Ctrl-Key, Keyboard.Modifiers still returns Modifiers.None