3

I want to use virtual keyboard assembly found here http://www.codeproject.com/KB/miscctrl/touchscreenkeyboard.aspx like on screen keyboard (OSK.exe) in Windows. Can someone please tell me how can I use it so that it always stays on top and yet for user to be able to select other windows on dekstop for keyboard input, just like "on screen keyboard" in Windows, specifically I don't know how to select last selected window (can't use GetForegroundWindow or GetFocus only, because when user clicks on virtual keyboard it gets focused and I get handle of keyboard window itself)? This is very urgent to me so any advice would be greatly appreciated.

Thanks in advance.

Maks
  • 117
  • 1
  • 3
  • 9

3 Answers3

4

What you need to do is make your window that it cannot be activated. This is quite easily done by overriding CreataParams. Then you can use SendKey.Send to send key presses to the currently active window, your window will never become active.

Here is a simple example

  public partial class Form1 : Form
  {
    const int WS_EX_NOACTIVATE = 0x08000000;

    public Form1()
    {      
      InitializeComponent();      
    }

    protected override CreateParams CreateParams
    {
      get
      {
        CreateParams param = base.CreateParams;
        param.ExStyle |= WS_EX_NOACTIVATE;
        return param;
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      SendKeys.Send("A");
    }
  }

One strange thing you will notice is that since your window never becomes active it does react rather strang when you drag the window. Basically the dragging works, it just does not give the visual feedback during the drag. You can address this by overriding WndProc and handling the WM_NCLBUTTONDOWN and WM_MOUSEMOVE messages.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • Thank you very much Chris! You were right this is the code I need. Before this I discovered in my so called "design" that I missed very important thing, I didn't need only active window handle but its control which had the focus also and I couldn't remember or find on net how to do that. This solved complete problem. After applying code you wrote at first program window didn't stay on top like it should, but then I removed, now unnecessary calls to "SetFocus()" and "SetForegroundWindow()" and it works like a charm. Yes, I noticed strange window behavior while dragging :) Thanks again. Cheers! – Maks May 09 '10 at 04:38
  • @Maks, I am glad that helped. To keep the window on top, you can set the TopMost property to true. – Chris Taylor May 09 '10 at 16:02
  • @Maks: If you liked this answer, you can mark it as the correct answer by clicking on the checkbox next to it. :) – Robert Harvey May 09 '10 at 17:07
1

When you get the input focus, the windows message WM_SETFOCUS is sent to your window, and .net converts this into the Forms events that you receive. The windows message contains the handle of the previous input-focus window.

If this information isn't available in your C# Form.Activated or Control.Enter/Control.GotFocus event, then you may need to override Form.WndProc to catch the raw windows message and retrieve the handle - which you can then use to activate or send WM_KEYDOWN messages to the previous input-focus window.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • Thank you for your effort to reply Jason, but although I was working a lot using Win32 API before right now I chosed simpler approach. I registered timer to be triggered every second it would call method to get active window handle and save it. Not too clean maybe but right now I cannot afford to spend more time revisiting raw API. – Maks May 09 '10 at 04:27
  • No problem. It's not "elegant", but it'll work, which is often all that really matters in the end :-) – Jason Williams May 09 '10 at 18:14
0

Sending Keystrokes to another Application in C#
http://www.codeproject.com/KB/cs/SendKeys.aspx

Then, all you would need is a way to select another window from the virtual keyboard. To do that, you just need the target window's title.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Thanks for link Robert, but I don't know the name of the window the user can select. Still though nice link, I found some clarification about the actual code but not the solution. – Maks May 09 '10 at 04:29