0

I have a TextBox UserControl which shows a OnScreenKeyboard by clicking it. The CodeBehind of my TextBox which starts the OnScreenKeyboard looks like this:

private void TextBoxText_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            TextBox textbox = sender as TextBox;
            US_Keyboard keyboardWindow = new US_Keyboard(textbox, Window.GetWindow(this));
            if (keyboardWindow.ShowDialog() == true)
                textbox.Text = keyboardWindow.InputSource.Text;
        } 

The OnScreenKeyboard is a partial class which inherits from Window. The Constructor of the OnScreenKeyboard looks like this:

    public US_Keyboard(TextBox owner, Window wndOwner)
    {
        InitializeComponent();
        this.Owner = wndOwner;
        this.DataContext = this;

        //Delete text in textbox
        if (InputSource.Text.Length > 0)
        {
            OldTextBoxValue = InputSource.Text;
            InputSource.Text = "";
        }

        //Set caret to start point of textbox
        AdornerLayer layer = AdornerLayer.GetAdornerLayer(owner);
        CaretAdorner adorner = new CaretAdorner(owner);
        adorner.OffsetX = _CaretOffsetXDefault;
        layer.Add(adorner);
        _adorner = adorner;
        _layer = layer;

        SetKeyboardSize();
        SetKeyboardPosition();
    }

So far everything works fine, except one thing: After the OnScreenKeyboard-Window shows up, it needs a second tap to react. It looks like it has no focus and needs one click to get the focus. How can i fix this?

I already tried the following in the constructor, but it does not help:

this.Focus();
  • Possible duplicate of [WPF Window set Focus](https://stackoverflow.com/questions/6395645/wpf-window-set-focus) – Rickless Dec 18 '17 at 08:16
  • It sounds like the same question, but the solution doesn't work for me –  Dec 18 '17 at 08:35
  • 1
    I would be more concerned about needing focus... shouldn't an on-screen-keyboard ideally work by receiving mouse clicks while the primary input focus stays in a different application that receives the input? – grek40 Dec 18 '17 at 08:55
  • i see your concerns and i agree with you. nevertheless i need a quick solution for my application –  Dec 18 '17 at 09:01
  • could you attempt to subscribe to IsVisibleChanged, and then call Focus() once the event is fired? – Milan Dec 18 '17 at 14:06
  • i testet it, nothing changed @Milan –  Dec 18 '17 at 14:39
  • One more desperate attempt. What about yourTextbox.isloaded event calling keyboard.focus for itself? – Milan Dec 18 '17 at 15:00

1 Answers1

0

You can consider using Win32 API:

using System;
using System.Windows;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace YourSolution
{
    static class WindowExtensions
    {
        [DllImport("User32.dll")]
        internal static extern bool SetForegroundWindow(IntPtr hWnd);

        public static void SetFocus(this Window window)
        {
            var handle = new WindowInteropHelper(window).Handle;
            SetForegroundWindow(handle);
        }
    }
}

When you want to focus a window, use window.SetFocus();.
Note: The window you want to focus must be loaded before you call SetFocus.

Grant Howard
  • 135
  • 2
  • 9