模拟按键presses在WPF按键、presses、WPF

2023-09-03 04:04:45 作者:是一个故事

我开发一个屏幕上的小键盘的触摸WPF应用程序。这将出现在弹出。当一个按钮为pressed,应发送键击到应用程序,使它看起来就像用户打字成文本框。这是我的code:

I am developing an on-screen numpad for a WPF touch app. This will appear in a Popup. When a button is pressed, it should send a keystroke to the application, making it look as though the user was typing into a TextBox. This is my code:

// 'key' is set beforehand
InputManager.Current.ProcessInput(new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, Environment.TickCount, key) { RoutedEvent = Control.KeyDownEvent });

这就是所谓的 Button.Click 事件处理程序中。

This is called within the Button.Click event handler.

到目前为止,只有 Key.Back 工作过。没有数字键的工作,而且也不 Key.Decimal

So far only Key.Back has worked. None of the digit keys work, and neither does Key.Decimal.

编辑:我想用的SendKeys 将解决这个问题,但它只是做同样的事情

I thought using SendKeys would solve the problem, but it just does the same thing.

为什么我的数字按钮的工作?我已经验证了正确的被传递。

Why aren't my digit buttons working? I have verified that the correct Key is being passed.

推荐答案

根据我们的意见的讨论,我建议你考虑这样的方法。我不知道的全部要求,但分裂的功能,并使得应用较少的单片始终是一个很好的方法!

Following our discussions in comments, I suggest you consider an approach like this. I don't know the full requirement, but splitting the functionality up and making the application less monolithic is always a good approach!

一起来,伪UML图会是这样的:

First up, a pseudo-UML diagram would be this:

如下您的三个功能将实施(伪code):

Your three functions would be implemented as follows (pseudocode):

UserControl1.InsertCharacter(string character)
{
    textBox1.Text = textBox1.Text.Insert(textBox1.CaretIndex, character); 
    this.ValidateInput();
}

UserControl1.ValidateInput()
{
    // Perform validation
}

UserControl1.OnKeyDown()
{
    // Perform validation
    this.ValidateInput();
}

UserControl2.AppendCharacter(string clickedCharacter)
{
    this.userControl1.InsertCharacter(clickedCharacter); 
}

要进一步分离UserControl2距离的UserControl1正如我所说,你可以使用事件触发模式,如.NET事件或EventAggregator。

To further decouple UserControl2 from UserControl1 as I mentioned you could use eventing patterns such as .NET events or EventAggregator.