简体   繁体   中英

How to keep text box's focus when a window pops up

i am building a virtual keyboard to suit the needs of the touch screen machine i'm going to be deploying on. i am using a popup window for the keyboard and have been able to wire all number buttons as follow, here's my virtual keyboard class

public partial class NumKeypad : Window
{
    withoutB withoutbvn;
    enterBvn ebvnn;
    public NumKeypad()
    {
        InitializeComponent();
    }

    public NumKeypad(withoutB wobvn)
    {
        InitializeComponent();
        withoutbvn = wobvn;            
    }

    private void one_Click(object sender, RoutedEventArgs e)
    {
        var focusedElt = FocusManager.GetFocusedElement(withoutbvn);
        var tbox = focusedElt as TextBox;
        try
        {
            withoutbvn.ph.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;//this works, but this is assigning directly to only one control. i want to assign to whatever control that has focus
        }
        catch (Exception ex)
        {

        }
    }
}

on the first line of the one_click function(which handles all input button click) i'm trying to get a reference to the element currently focused in the page whose instance is "withoutbvn". on the second line, i am tryin to convert the element to a text box so i can write to its text property. but that keeps returning null. meaning when this pop up windows come up(the keyboard pop up window comes up when a textbox or any other input element receives focus), i cannot get a reference to the focused textbox so i cannot write to it. Please how do i ensure a focused textbox remains focused so that i can assign its text property from a pop up window? Or if there's a better way to do this, pls point me in the right direction. Thanks

I've used this keyboard for WPF :

keyboard control wpf

It is a popup control which can be customized as you wish. You have the entire code and it's free. In my case I had to adjust the popup (layout and to add the German letters) and was pretty straightforward.

I also had to show a numeric keyboard, and I've used the same keyboard but with a simpler layout. Behind the scenes, all it is very simple: you have to define a key in a grid, place it where you want and make sure you generate on click the corresponding virtual key code.

i used the popup control to create the keyboard, used buttons to create all keys and wired a single event handler to all input buttons, then different event handlers for the backspace and enter buttons. once any letter, number or symbol button is clicked, the following function gets called.

try
        {
            IInputElement focusedControl = Keyboard.FocusedElement;
            var foc = focusedControl as TextBox;
            foc.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;
        }
        catch (Exception)
        {

        }

that inserts the button's text to the control in focus. This is pretty basic. i'll appreciate more suggestion on how i can expand on this. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM