我想将密钥从WPF发送到另一个窗口(例如编辑器)
我找到了这个Methode
public static class SendKeys
{
public static void Send(Key key)
{
if (Keyboard.PrimaryDevice != null)
{
if (Keyboard.PrimaryDevice.ActiveSource != null)
{
var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Down) { RoutedEvent = Keyboard.KeyDownEvent };
InputManager.Current.ProcessInput(e1);
}
}
}
}
,但它不起作用:/
所以我从我的主程序中调用它
SendKeys.Send(Key.D7);
在这种情况下,WindowsForms不是替代方法:/
答案 0 :(得分:0)
使用传递的key
参数代替Key.Down
。
这很好用:
public partial class MainWindow : Window
{
public MainWindow()
{
this.Loaded += OnLoaded;
this.KeyDown += OnKeyDown;
InitializeComponent();
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
SendKeys.Send(Key.A);
SendKeys.Send(Key.B);
SendKeys.Send(Key.C);
SendKeys.Send(Key.D);
SendKeys.Send(Key.LeftCtrl);
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Key pressed: " + e.Key);
}
}
public static class SendKeys
{
public static void Send(Key key)
{
if (Keyboard.PrimaryDevice != null)
{
if (Keyboard.PrimaryDevice.ActiveSource != null)
{
var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key) { RoutedEvent = Keyboard.PreviewKeyDownEvent };
InputManager.Current.ProcessInput(e1);
}
}
}
}