创建一个 WPF 触摸屏键盘应用程序,将按键发送到另一个窗口

发布于 2024-09-01 20:14:01 字数 412 浏览 2 评论 0原文

我的触摸屏键盘是高度可定制的界面,除了发送按键之外,拥有我需要的所有组件,任何人都会看到这个问题。最初,当我创建它时,我打算使用 Forms.SendKeys.Send() 但它是一个 WPF 应用程序......不行。对于初学者来说,VB.Net以其无限的智慧决定它不会处理默认表单消息。去算算吧。

或者,这可能是我真正的问题,我无法让 WPF 应用程序停止获得焦点。我希望它像 Windows 触摸键盘一样工作,但是透明 WPF 中发生的每个操作都会使该应用程序成为活动应用程序。我希望事件仍然发生,但我需要活动窗口是您想要输入内容的窗口,例如记事本。

关于我应该做什么,使我的 WPF 不可聚焦并将键盘按钮发送到聚焦(或其他)窗口的任何建议?

PS,我在 Vb.Net 的 Visual Studio 2010 中使用 WPF(我可以使用 C# 代码!)

My touch screen keyboard is highly customizable interface and has every component I need except for sending keys, anyone see a problem with this. Originally when I was creating it I was going to use the Forms.SendKeys.Send() but it's a WPF application... no go. For starters VB.Net in its infinite wisdom decided it would not handle default form messages. Go figure.

Or perhaps, this is my real problem, I can't get the WPF application to stop getting focus. I want it to act like the windows touch keyboard, but Every action that occurs in the transparent WPF makes that application the active one. I want events to still occur but I need the active window to be the window you wish to type into, like notepad.

Any Suggestions on what I should do, to make my WPF not focusable and to send keyboard buttons to the focused (or other) window?

PS, I'm using WPF in Visual Studio 2010 in Vb.Net (I can use C# Code to!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心激荡 2024-09-08 20:14:01

我想你会在这里找到答案

很多复杂的方法可以实现这一点,但我提出的解决方案是上面的链接很简单,只有一个怪癖,老实说可以解决。拖动输入窗口时,它不会提供反馈,直到移动完成,但您可以通过处理一些非客户端消息来解决此问题,如果您需要,我可以花一些时间查看解决方法,但首先确认此解决方案是正确的为你。

更新:如何将上述方法应用于 WPF 表单的示例。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace WpfApplication1
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    const int WS_EX_NOACTIVATE = 0x08000000;
    const int GWL_EXSTYLE = -20;

    [DllImport("user32", SetLastError = true)]
    private extern static int GetWindowLong(IntPtr hwnd, int nIndex);

    [DllImport("user32", SetLastError = true)]
    private extern static int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewValue);

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      WindowInteropHelper wih = new WindowInteropHelper(this);
      int exstyle = GetWindowLong(wih.Handle, GWL_EXSTYLE);
      exstyle |= WS_EX_NOACTIVATE;
      SetWindowLong(wih.Handle, GWL_EXSTYLE, exstyle);
    }    
  }
}

I think you will find the answer here usefull

There are a number of compilcated ways to achieve this, but the solution I posed that the above link is easy and only has one quirk, that to be honest can be worked around. When dragging the input window it does not provide feedback until the move is completed, but you can work around this by handling some non-client messages, I can spend sometime look at the work around if you need, but first confirm this solution is right for you.

Update: A sample of how a the approach above can be applied to a WPF form.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace WpfApplication1
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    const int WS_EX_NOACTIVATE = 0x08000000;
    const int GWL_EXSTYLE = -20;

    [DllImport("user32", SetLastError = true)]
    private extern static int GetWindowLong(IntPtr hwnd, int nIndex);

    [DllImport("user32", SetLastError = true)]
    private extern static int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewValue);

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      WindowInteropHelper wih = new WindowInteropHelper(this);
      int exstyle = GetWindowLong(wih.Handle, GWL_EXSTYLE);
      exstyle |= WS_EX_NOACTIVATE;
      SetWindowLong(wih.Handle, GWL_EXSTYLE, exstyle);
    }    
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文