Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if IME (Input Method Editor) is active in Silverlight

Does anyone know how I could easily detect if the Windows OS IME (Input Method Editor) is active in the Silverlight framwork. Let me explain the scenario which is causing me issues:

I have hit an issue where using a Chinese (Simplified, China) Microsoft Pinyin causes a CLR exception. The scenario is when a TextBox is handling its TextChanged event. For example:

  • A TextBox should not accept characters but only numbers. In order to achieve that it is handling its TextChanged event and in that event it handles the input by reversing its Text property to the last correct input character by character. In this scenario if an (Chinese (Simplified, China) Microsoft Pinyin) IME is used an a FatalExecutionEngineError is thrown.

In WPF it is possible to overcome this issue by not reversing the Text in the TextChanged event by using the InputMethod.Current.ImeState to check if an IME is active. Unfortunately this is not available in the Silverlight framework which is why I am posting this question.

Currently the only thing I have found is that I could set IsInputMethodEnabled property of the InputMethod class to the TextBox control in order disable all IME input but this of course will not only disable the incorrect input but also the correct one.

Anyone has any ideas how I could detect if a IME is used/active in the Silverlight platform? Thanks.

like image 668
Vladimir Amiorkov Avatar asked Feb 06 '15 15:02

Vladimir Amiorkov


1 Answers

I was able to resolve the issue in both the WPF and Silverlight frameworks. The issue was caused by the fact that by handling the TextBox Text while a IME is inputting symbols that Text was making the IME itself change its input which it looks like is not handled gracefully by the Windows OS and was causing a CLR exception.

What I did was:

In the WPF framework as mentioned I used the static InputMethod.Current.ImeState value to determine if IME is active and if it was with On value I skipped reverting the TextBox Text property in its TextChanged event.

In the Silverlight framework I use a combination of the TextInputStart, TextInputUpdate events and a local private field to store if IME was detected. The TextInputUpdate event is only triggered if IME is active and used as input and the TextInputStart is always triggered. What I did was:

  1. Created a bool IsImeActive = false; filed
  2. Hook to the TextInputStart event of the TextBox
  3. In that event set the IsImeActive field to False
  4. Hook to the TextInputUpdate event of the TextBox
  5. In that event set the IsImeActive field to True
  6. Finally in the TextChanged event add a condition that checks the IsImeActive field and if it is False run the logic which handles (reverses) the input.

Hope this is helpful.

like image 118
Vladimir Amiorkov Avatar answered Sep 20 '22 00:09

Vladimir Amiorkov