Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have some problems with focussing Elements with my virtual keyboard (wpf).
Its a litte bit strange because I have started programming the virtual keyboard first in a project.
In this project the keyboard works very well, it's always topmost and it don't lost the focus when I choose some element to write in some text :).
I have a second project and imported the keyboard source files into the new project.
But it's not working in my "window1", with other Programs (Firefox,Windows Command Line etc.) its still working.
I've set window1 topmost to false and the osk window to true.

Please help.

Greetings

Update 1:
Hi,

thanks for you reply.

Here's the code sample:

//OSK Window
XML
<pre lang="xml"><Window x:Class="OSKSample.OSK_Window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OSK_Window" Height="120" Background="DarkGray" MinWidth="800" Opacity="0.8" Topmost="True" WindowStyle="None" Initialized="OSK_Window_Initialized" Focusable="False" ShowInTaskbar="False" AllowsTransparency="True"  WindowStartupLocation="Manual" Loaded="Window_Loaded" ResizeMode="NoResize" WindowState="Normal">
    <Grid Name="keyGrid" Focusable="False">
        <Grid.RowDefinitions>
            <RowDefinition Height="192*"/>
        </Grid.RowDefinitions>
    </Grid>
</Window>



C#
public OSK(Window parent)
        {
            this.parentWindow = parent;
            this.setupKeyboardControl();
        }
        public OSK(IInputElement elementToFocusOn)
        {
            // set focus
            this.focusedInputElement = elementToFocusOn;
            this.setupKeyboardControl();
        }


//So, I'm sending the keys with this way...

C#
void button_Click(object sender, RoutedEventArgs e)
       {
           String sendString = "";
           try
           {
               e.Handled = true;
               sendString = ((Button)sender).CommandParameter.ToString();
               if (!String.IsNullOrEmpty(sendString))
               {
                   if (sendString.Length > 1)
                   {
                       sendString = "{" + sendString + "}";
                   }
                   if (this.focusedInputElement != null)
                   {
                      Keyboard.Focus(this.focusedInputElement);
                       this.focusedInputElement.Focus();
                   }
                   System.Windows.Forms.SendKeys.SendWait(sendString);
               }
           }
           catch (Exception)
           {
               Console.WriteLine("Taste {0} konnte nicht gesendet werden", sendString);
           }
       }


Get Window to focus it

C#
private void UserControl_Loaded(object sender, RoutedEventArgs e)
       {
           if (this.parentWindow != null)
           {
               IntPtr HWND = new WindowInteropHelper(this.parentWindow).Handle;
               int GWL_EXSTYLE = (-20);
               GetWindowLong(HWND, GWL_EXSTYLE);
               SetWindowLong(HWND, GWL_EXSTYLE, (IntPtr)(0x8000000));
           }
       }


So far I still don't understand why it's working when I start the osk separately.
Posted
Updated 16-May-11 1:22am
v2
Comments
Sergey Alexandrovich Kryukov 15-May-11 23:53pm    
It's a well recognized known problem. Hard to say anything certain unless you present sufficient information. You can create a minimalized code sample to show this problem (but not all your code, if possible).
--SA
Venkatesh Mookkan 16-May-11 7:23am    
Do not add solution if you want to update the question. You can either "Improve question" or "Add comment" to the given Solution.
ElBoro 17-May-11 18:10pm    
Sorry for this mistake ;)
Ahn_7 5-Sep-11 6:12am    
Hi ElBoro,exactly the same problem which I have..You got the solution ??
If it is, please share your knowledge..

1 solution

As I say, it's hard to help in your particular case as information you provide is insufficient.

However, you can use the following ideas: 1) Basically, to operate on an arbitrary windows, your virtual keyboard should not be focusable and not be activated; so, to be able to stay on top it should be Topmost ("always on top"); 2) Best way to emulate keyboard input working with any application is using P/Invoked Windows API SendInput (http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx[^]).

You can get some help if you study how some successful virtual keyboards work.

Did you see these works:
A Software Virtual Keyboard for Your WPF Apps[^],
A Touch Screen Keyboard Control in WPF[^]?

—SA
 
Share this answer
 
v2

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