Question

KeyEventArgs e = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, keyData);

Keyboard.PrimaryDevice.ActiveSource is null and program stop working. I tried also:

MainWindow mw = new MainWindow();
System.Windows.PresentationSource source = System.Windows.PresentationSource.FromVisual(mw);
KeyEventArgs e = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, keyData);

But still null, I cannot replace mw to this, because code is in different file class.

I'm trying to catch keyboard from other windows, code is only working if I catch keyboard with the current active window of my program.

Any idea?

Était-ce utile?

La solution

It's null because your application does not have focus. You can create a dummy System.Windows.Interop.HwndSource, like so:

KeyEventArgs kea = new KeyEventArgs(Keyboard.PrimaryDevice, new HwndSource(0, 0, 0, 0, 0, "", IntPtr.Zero), 0, key);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top