开发者

C# - Sending keyboard events to (last) selected window

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 i开发者_Go百科t 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜