TolikPylypchuk/KeyboardSwitch

Sometimes a keyboard layout disappears from the system

TolikPylypchuk opened this issue · 2 comments

I'm not 100% sure that this has anything to do with KeyboardSwitch, but after implementing auto-configuration for the converter (which loads and unloads keyboard layouts), one of my installed layouts started randomly disappearing, even though it's listed as one of the layouts in Settings.

Happened again during switching text, so the issue is probably the switching process.

Turns out that the issue was the LoadKeyboardLayout call in the SwitchCurrentLayout method.

Previously I defined the LoadKeyboardLayout function to return an IntPtr, then I passed this IntPtr to PostMessage to actually switch the layout and that was it.

But after switching (pun intended) to using Vanara instead of my own methods, the return value of this method became different - it is now of type SafeHKL and this type extends SafeHandle which means that this value can and will be finalized during garbage collection (hence the non-deterministic nature of the bug). And releasing SafeHKL means unloading the layout which is not at all what is needed.

Further, I realized that I didn't even need to call LoadKeyboardLayout in that method - I can just call GetKeyboardLayout after switching it. So that's basically what I've changed in order to fix this bug.