C# winforms .Net 4.5
In a system where there are two keyboards I am trying to intercept the input from the second keyboard and replace the key-presses with one of my choosing.
Using the Raw input API I am able to identify which keyboard a key was pressed on (there are many examples of this) and insert the key of my choosing. The problem is that the Raw input API cannot cancel the key press which i need to do to substitute my own key. So I get the original key followed by my key.
https://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard
Alternately I can use a keyboard hook to grab keys but this does not identify which keyboard the key came from.
It also eventually will crash with: "C# Global Low Level Keyboard Hook 'A callback was made on a garbage collected delegate of type 'Keyboard!Keyboard.globalKeyboardHook+keyboardHookProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.' "
It has been suggested that a combination of the two methods can solve the problem.
Is it possible to swallow a key in Raw Input?
However I have not been able to get this to work. It seems that the raw input process happens after the hook so although I can flag where the key came from it is too late. And then the app eventually crashes as previously explained.
I'm not sure what I can do about this?
I have seen many similar questions but have yet to see the answer. Does anyone have an example of how this can be done?