Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
Hello everyone,
I have an issue implementing a screen keyboard for a touch app, i need an usercontrol (for dragabble purposes) in wpf that don't steal focus from others inputs and keep it itselft always on top.

To do that i create a window whit {x:null} background and i put the usercontrol inside, then i show the window in the start of the app and i can view the "invisible" window whit the keyboard allways on top, and i can drag and drop the keyboard over the invisible window, the only problem is that i cant prevent stealing focus from other inputs, so i can't use the keyboard.

I hope you can help me.
Sorry for my bad english.

Thanks!

Matías

Here is the control XAML, it only has 3 buttons for test purposes.
XML
<UserControl x:Class="SuperCajero.TecladoCompleto"
             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="200" d:DesignWidth="1024" Focusable="False">
    <Grid Width="1024" Height="200" removed="Green" Focusable="False">
        <Button Content="A" Height="35" HorizontalAlignment="Left" Margin="471,92,0,0" Name="btnA" VerticalAlignment="Top" Width="35"  Click="btnA_Click" Focusable="False"/>
        <Button Content="B" Height="35" HorizontalAlignment="Left" Margin="219,100,0,0" Name="btnB" VerticalAlignment="Top" Width="35"  Click="btnB_Click" Focusable="False"/>
        <Button Content="C" Height="35" HorizontalAlignment="Left" Margin="366,100,0,0" Name="btnC" VerticalAlignment="Top" Width="35"  Click="btnC_Click" Focusable="False"/>
    </Grid>
</UserControl>


Here is the C# code of the control that makes it draggable inside the auxiliar window
C#
public partial class TecladoCompleto : UserControl
    {
        public TecladoCompleto()
        {
            InitializeComponent();
            MakeDraggable(this, this);
        }

        public void MakeDraggable(System.Windows.UIElement moveThisElement, System.Windows.UIElement movedByElement)
        {
            bool isMousePressed = false;
            System.Windows.Media.TranslateTransform transform = new System.Windows.Media.TranslateTransform(0, 0);
            moveThisElement.RenderTransform = transform;
            System.Windows.Point originalPoint = new System.Windows.Point(0, 0), currentPoint;

            movedByElement.MouseLeftButtonDown += (a, b) =>
            {
                isMousePressed = true;
                originalPoint = ((System.Windows.Input.MouseEventArgs)b).GetPosition(moveThisElement);
            };

            movedByElement.MouseLeftButtonUp += (a, b) => isMousePressed = false;
            movedByElement.MouseLeave += (a, b) => isMousePressed = false;
            movedByElement.MouseMove += (a, b) =>
            {
                if (!isMousePressed) return;

                currentPoint = ((System.Windows.Input.MouseEventArgs)b).GetPosition(moveThisElement);

                transform.X += currentPoint.X - originalPoint.X;
                transform.Y += currentPoint.Y - originalPoint.Y;
            };
        }

        private void btnA_Click(object sender, RoutedEventArgs e)
        {
            WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.VK_A);
        }

        private void btnB_Click(object sender, RoutedEventArgs e)
        {
            WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.VK_B);
        }

        private void btnC_Click(object sender, RoutedEventArgs e)
        {
            WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.VK_C);
        }
    }


Here is the auxiliar transparent window that owns the usercontrol
XML
<Window x:Class="SuperCajero.AuxWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:SuperCajero"
        Title="AuxWindow" WindowStyle="None" Topmost="True" AllowsTransparency="True" removed="{x:Null}" Focusable="False" >
    <Grid>
        <controls:TecladoCompleto HorizontalAlignment="Center" VerticalAlignment="Bottom" Focusable="False"/>
    </Grid>
</Window>
Posted
Updated 25-Feb-14 7:02am
v4
Comments
Sergey Alexandrovich Kryukov 25-Feb-14 11:38am    
What do you mean "how"? By doing some good piece of work. What have you tried so far?
—SA

1 solution

You can created it by doing some programming work. As your concern is not clear, I cannot tell you anything specifically useful; and you cannot expect that someone writes a complete solution for you. Anyway, please see this article: A software Virtual Keyboard for your WPF apps[^].

—SA
 
Share this answer
 
v2
Comments
Member 10499931 25-Feb-14 11:42am    
The link you post is no longer up.
I create the keyboard, into a transparent window so i can use the application, all i need is how i can prevent that the keyboard that is in another wpf window steal the focus, so then the screen keyboard can write the letter pressed by the mouse into an input in another window.
Sergey Alexandrovich Kryukov 25-Feb-14 11:47am    
Sorry, I mixed it up with my answer on similar topic. But this article (I fixed this answer, above) is accessible, I just tested it.
The problem of steeling the focus is solved in the following way: make it non-focusable, but "always on top". Yes, this is the problem, and this is the trick.
—SA
Member 10499931 25-Feb-14 11:56am    
i already done that, i set all properties of the controls associated to the keyboard non-focusable but when i click a key into the virtual keyboard, the input lost his focus anyway and the input simulator don't work

Sergey Alexandrovich Kryukov 25-Feb-14 12:10pm    
Are you sure? Nothing on the virtual keyboard windows should get focus, including the window itself. I am almost sure you did not do it; with WPF, it can only be done via P/Invoke, to best of my knowledge. Well, unfortunately...
—SA
Member 10499931 25-Feb-14 12:46pm    
in the xaml window file, all the controls and the window itself has Focusable="False", is that right?

What do you mean about "P/Invoke"??

I'm sorry for my ignorance.

Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900