WPF将密钥发送到textBox

问题描述 投票:0回答:1

我正在使用虚拟键盘,但是我很难发送密钥。什么都没有发生,除非我将它们硬添加到文本框中。这是我的代码

  public static void Send(Key key)
    { //Found online
                var e = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key)
                {
                    RoutedEvent = Keyboard.KeyDownEvent
                };
                InputManager.Current.ProcessInput(e);
    }


  private void ClickOnKey(object sender, RoutedEventArgs e)
    {
        CustomButton cb = sender as CustomButton;
        string text = cb.text;
        Console.WriteLine(text);
        element_to_focus.Focus(); //The textbox
        Send(translate_string_to_key(text));
     }
wpf keyboard-events
1个回答
0
投票

ProcessInput似乎只会引发与键有关的适当事件,而实际上不会修改文本。

如果您真的想模拟按键,则需要利用p / invoke并使用SendInput。幸运的是,某人已经为我们完成了p / invoke工作,并使他们的工作可用:SendInput

它也可以作为NuGet包提供(我发现了两个版本):https://archive.codeplex.com/?p=inputsimulatorhttps://www.nuget.org/packages/InputSimulator/

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.