5

Users of my application have a second keyboard with special function keys. Unfortunately, the keys are mapped to buttons such as F, G, F1 and so on. I would like to handle PreviewKeyDown and prevent any keys from these keyboards having an effect in normal controls such as TextBoxes.

In WPF, is there any way of determining which keyboard raised the event?

H.B.
  • 166,899
  • 29
  • 327
  • 400
James L
  • 16,456
  • 10
  • 53
  • 70
  • Have you had a look what properties are available to you while debugging? Is there nothing in the event arguments? Does it not come with anything that would allow you to differentiate between the two? –  Sep 03 '12 at 13:06
  • Hmm, it's a good question this, +1. I'll have a look into it a bit more when I have a minute. –  Sep 03 '12 at 13:07
  • I have found this: [CodeProject](http://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard), could provide a solution. [StackOverflow](http://stackoverflow.com/questions/11451472/raising-keyboard-events-via-an-interface-in-wpf) seems to also have a simular solution. Still no hooking in the WPF EventKey chain. – KyorCode Sep 03 '12 at 13:11
  • Thanks, the problem with that approach is abandoning the WPF keyboard event model and have a parallel implementation for this keyboard. – James L Sep 03 '12 at 13:15
  • Yup, I know, but can't seem to find anything closer then that at the moment.. – KyorCode Sep 03 '12 at 13:17
  • I have the Raw Input API working, and can detect the keyboard it came from, but I can't then prevent that keypress hitting WPF – James L Sep 03 '12 at 13:57
  • Which event is fired first? that from WPF or the Raw Input API? – KyorCode Sep 03 '12 at 14:01
  • We're thinking along the same lines I think. Just trying to find out. – James L Sep 03 '12 at 14:21
  • Are you sure that H.B.'s suggestion with the `Device` property is not what you want? According to the [documentation](http://msdn.microsoft.com/en-us/library/system.windows.input.keyboarddevice.aspx) there will be two instances when there are two physical devices: "Each InputDevice object represents one particular input device. For example, if there are two mice, there will be two InputDevice objects." – Daniel Hilgarth Sep 05 '12 at 11:41
  • Very interesting Daniel. In that case, I just need to find some way of discriminating between the various InputDevices. – James L Sep 05 '12 at 12:33
  • Daniel, there is a single virtual device for all Win32 keyboards – James L Sep 10 '12 at 09:56
  • That contradicts the documentation. Maybe you want to file a content bug on MSDN? BTW: I only get notified of your comment when you put the @ sign in front of my name: @JamesL – Daniel Hilgarth Sep 13 '12 at 07:12

2 Answers2

1

No, it is not possible directly in WPF.

James L
  • 16,456
  • 10
  • 53
  • 70
-3

using System.Windows.Input you could be able to achieve this by capturing the event that is fired in your code behind. Sample code below shows how this can be done in Textbox.

private void SampleTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete) // delete key is pressed
            {
                e.Handled = true; // Ignore key press
            }
        }
Jepoy_D_Learner
  • 435
  • 1
  • 6
  • 13