Click here to Skip to main content
15,071,463 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to write a on screen keyboard , but when it has focus I can not write,
for example : when i open notepad , and I want to type some word , it doesn't because my app gets focus!
for this my app should lose focus and the application that i want to type words in it(like notepad)should gets focus.
I don't know how to do this!
thank's in advance.
Sergey Alexandrovich Kryukov 17-Feb-11 17:05pm
Not a bad question! This is not a trivial issue.

What you need is this:

internal static class User32Constants {
    internal const int WS_EX_TOPMOST = 8;
    internal const int WS_EX_NOACTIVATE = 0x08000000;

In your Virtual keyboard form, add:

protected override CreateParams CreateParams {
    get {
        CreateParams par = base.CreateParams;
        if (DesignMode) return par;
        par.ExStyle |= User32Constants.WS_EX_TOPMOST;
        par.ExStyle |= User32Constants.WS_EX_NOACTIVATE;
        return par;
    } //get CreateParams
} //CreateParams

The use of WS_EX_TOPMOST works as the property TopMost (which could be used instead).
There three delicate moments: topmost behavior is needed to not hide the form behind when focusing on a control to be used for input, WS_EX_NOACTIVATE — to avoid grabbing focus by the virtual keyboard (Nishant explained that before). The check of DesignMode is good to avoid disrupting of the Form Designer in case you use it.

I bet your next question would be about sending input from the virtual control; not many do it correctly. A hint: find an answer in one of my recent Answers.

Manas Bhardwaj 17-Feb-11 17:07pm
Sergey Alexandrovich Kryukov 17-Feb-11 17:10pm
Thank you.
AnuradhaJayalath 7-Jul-12 8:22am
how to do this in program
Sergey Alexandrovich Kryukov 11-Jul-12 15:32pm
Well (sigh...), pretty much the same. I don't even install VB.NET with Visual Studio or anything. If you do not understand C# well enough, try to translate it automatically:

I would advise that, just for asking question and using experience of others, you should understand at least some of essential C#. Most good answers, cookbooks advice and articles on .NET are in C#.
Typically this is done by creating the window with the WS_EX_NOACTIVATE style. In C#, you don't directly create the window (or form), but you can try overriding the CreateParams property (and then remove that style from your form):[^]
Sergey Alexandrovich Kryukov 17-Feb-11 17:02pm
Sorry, I happened to implement a virtual keyboard component few years ago and can see your implementation is not quite complete. It needs something else, please see my Answer.
So, I did not vote this time.
Thank you.
Nish Nishant 17-Feb-11 17:11pm
No worries :-)

The guy already has his keyboard form on top. So I assume this is all he needs. But your answer may be more complete.
Sergey Alexandrovich Kryukov 17-Feb-11 19:51pm
Sorry, no. Imagine the target application is maximized (very usual). To type, it needs focus. Even though "the guy already has his keyboard form on top", it go back hidden with the target application when the user attempts to focus, which is a must to start typing. TopMost is the absolute must.

Do you see my point now? Maybe I would not see it myself if I did not have my implementation...

Nish Nishant 17-Feb-11 20:10pm
SA, you misunderstood me. I am not saying that topmost is not important. I am saying that I assume the guy has already done that - because it's very easy to do that in WinForms. Just set the form's TopMost property to true. So there's no need to set WS_EX_TOPMOST because TopMost will do that for you.

But for WS_EX_NOACTIVATE, Winforms does not provide a property, you have to override CreateParams. I hope that makes sense. Thanks SA.
Sergey Alexandrovich Kryukov 17-Feb-11 22:19pm
Oh, I misunderstood because you did not say TopMost, you say "already... on top" which is cannot be certainly understood as TopMost. Also, I cannot find TopMost in the Question or anywhere else. Note that I added the flag TOPMOST because the flag WS_EX_NOACTIVATE can only be use in CreateParams, so I originally used them both (naturally, it's best to keep related aspects together).
So now we agree on how it works.
Thank you.
Thank you for all reply.

now i can do it with c sharp..

how can i make it using
if you can help me.

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