C# - 发送键盘事件(最后一个)选定的窗口 [英] C# - Sending keyboard events to (last) selected window

查看:138
本文介绍了C# - 发送键盘事件(最后一个)选定的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这里找到虚拟键盘组件 http://www.codeproject.com /KB/miscctrl/touchscreenkeyboard.aspx 喜欢屏幕键盘(OSK.exe)在Windows上。有人可以告诉我,我该如何使用它,这样它总是停留在顶端,但用户将能够在dekstop键盘输入选择其他窗口,就像在Windows的屏幕键盘上,具体我不知道如何选择最后选定的窗口(不能使用GetForegroundWindow或GetFocus而已,因为当用户点击虚拟键盘上它被聚焦,我得到键盘窗口本身的句柄)?
这是给我,让任何建议,将不胜感激十分迫切。

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.

在此先感谢。

推荐答案

您需要做的是让你的窗口,它不能被激活。这是很容易通过覆盖 CreataParams 完成。然后你可以使用SendKey.Send按键后发送到当前活动窗口时,窗口将永远不会被激活。

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.

下面是一个简单的例子

  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");
    }
  }

您会发现一个奇怪的是,因为你的窗口不再激活它,而斯特朗反应过来的时候,你拖动窗口。基本上拖动的作品,它只是不给拖动过程中的视觉反馈。您可以通过重写的WndProc和处理WM_NCLBUTTONDOWN和WM_MOUSEMOVE消息的解决这一问题。

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.

这篇关于C# - 发送键盘事件(最后一个)选定的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆