WPF set the input method of TextBox

One, InputLanguageManager class

This class can control the language switching of the Microsoft Input Method.
It's very simple. There are only two additional properties:
InputLanguageand RestoreInputLanguageit is very simple to use.
Control the preferences of the input language, and whether the input language is restored when the cursor leaves the control.

1.1 InputLanguage (the preferred language for controlling the input method)

This additional attribute is used to control the preference of the input language.
If you add InputLanguageManager.InputLanguageit to your TextBox , when your cursor enters the TextBox, it will automatically switch to the language you set. as follows:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <WrapPanel Grid.Row="0">
        <Label Content="Language1:" />
        <TextBox
            Width="180"
            Height="30"
            InputLanguageManager.InputLanguage="en-US" />
    </WrapPanel>
    <WrapPanel Grid.Row="1">
        <Label Content="Language2:" />
        <TextBox
            Width="180"
            Height="30"
            InputLanguageManager.InputLanguage="zh-CN" />
    </WrapPanel>
</Grid>

As above, when the cursor moves to the first TextBox, it will switch to the English input method, and when it moves to the second TextBox, it will switch to the Chinese input method. The premise is that your system has installed input methods for these two languages. Well, if you are the home version of win10, sorry, it only supports one language, please upgrade to the professional version.

How do I know which input methods are available in my system?
Can be input in the background InputLanguageManager.Current.AvailableInputLanguages;
This property returns an enumerated collection of input method languages ​​that the current system can use.

var languages= InputLanguageManager.Current.AvailableInputLanguages;
foreach (var languagein languages)
{
    
    
    Console.WriteLine(language);
}

1.2 RestoreInputLanguage (whether to restore the language when the cursor leaves)

InputLanguageManager.RestoreInputLanguageAfter setting this additional property for the TextBox , when the cursor moves out of the TextBox, the previous language will be restored. The only attribute Trueand Falsetwo values are optional.

Two, InputMethod class

The InputMethod class has 6 additional attributes, which can be used to control the input mode, such as setting whether to enable the input method, restricting the input content, etc.

2.1 PreferredImeState (set input method state)

InputMethod.PreferredImeStateThis additional attribute can set the preferred state of the input method. There are three enumeration values: On, Off, DoNotCare. When set to On, the input method is enabled. When set to Off, turn off input conversion. No effect when DoNotCare.
After closing the input conversion, only the input of English numbers and symbols can be performed, that is, letters cannot be converted into other languages. The conversion mode can be restored manually.

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition
			Height="*" />
		<RowDefinition
			Height="*" />
	</Grid.RowDefinitions>
	<WrapPanel
		Grid.Row="0">
		<Label
			Content="Language1:" />
		<TextBox
			Width="180"
			Height="30"
			InputMethod.PreferredImeState="On"
			InputLanguageManager.InputLanguage="zh-CN" />
	</WrapPanel>
	<WrapPanel
		Grid.Row="1">
		<Label
			Content="Language2:" />
		<TextBox
			Width="180"
			Height="30"
			InputMethod.PreferredImeState="Off"
			InputLanguageManager.InputLanguage="zh-CN" />
	</WrapPanel>
</Grid>

2.2 IsInputMethodEnabled (whether to enable input method)

InputMethod.IsInputMethodEnabledThis attribute controls whether to enable the input method. If it is set to False, the input method is invalid and cannot be started manually .

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition
			Height="*" />
		<RowDefinition
			Height="*" />
	</Grid.RowDefinitions>
	<WrapPanel
		Grid.Row="0">
		<Label
			Content="Language1:" />
		<TextBox
			Width="180"
			Height="30"
			InputMethod.IsInputMethodEnabled="True"
			InputLanguageManager.InputLanguage="zh-CN" />
	</WrapPanel>
	<WrapPanel
		Grid.Row="1">
		<Label
			Content="Language2:" />
		<TextBox
			Width="180"
			Height="30"
			InputMethod.IsInputMethodEnabled="False"
			InputLanguageManager.InputLanguage="zh-CN" />
	</WrapPanel>
</Grid>

2.3 PreferredImeConversionMode (set conversion mode)

InputMethod.PreferredImeConversionModeThis attribute can set the conversion mode of the input method, and there are a total of 11 enumeration values ​​to choose from.

Enumerated value description
Alphanumeric The input method uses alphanumeric conversion mode.
CharCode The input method uses character code conversion mode.
DoNotCare The input method does not consider which input conversion method is used; the actual conversion method is uncertain.
Eudc The input method uses EUDC (End User Defined Characters) conversion mode.
Fixed The input method uses a fixed conversion mode.
FullShape The input method uses the full-shape conversion mode.
Katakana The input method uses Katakana conversion mode.
Native The input method uses native character (Hiragana, Korean and Chinese) conversion mode.
NoConversion The input method does not perform any input conversion.
Roman The input method uses Roman character conversion mode.
Symbol The input method uses symbol conversion mode.
<Grid>
	<Grid.RowDefinitions>
		<RowDefinition
			Height="*" />
		<RowDefinition
			Height="*" />
	</Grid.RowDefinitions>
	<WrapPanel
		Grid.Row="0">
		<Label
			Content="Language1:" />
		<TextBox
			Width="180"
			Height="30"
			InputMethod.IsInputMethodEnabled="True"
			InputLanguageManager.InputLanguage="zh-CN" />
	</WrapPanel>
	<WrapPanel
		Grid.Row="1">
		<Label
			Content="Language2:" />
		<TextBox
			Width="180"
			Height="30"
			InputMethod.IsInputMethodEnabled="False"
			InputLanguageManager.InputLanguage="zh-CN" />
	</WrapPanel>
</Grid>

I personally use this attribute often. When you move the cursor into the TextBox, this attribute can switch the conversion mode, and the matching InputLanguageManagerclass can accurately switch to the Japanese input method, and use the conversion mode of hiragana. Often used in work.

2.4 PreferredImeSentenceMode (set the Lenovo function of the input method)

InputMethod.PreferredImeSentenceModeThe default sentence mode is to select the smart association mode. It is to guess the word you want to input and give you a few candidate words. If disabled, your input method will have no smart association function.
There are 7 enumeration values ​​to choose from.

Enumerated value description
Automatic The input method automatically uses the sentence pattern conversion method.
Conversation This input method uses dialogue style sentence pattern conversion.
DoNotCare The input method does not care which sentence pattern conversion method is used; the actual sentence pattern conversion mode is uncertain.
None This input method does not perform any sentence pattern conversion.
PhrasePrediction The input method uses phrase association sentence pattern conversion.
PluralClause This input method uses plural clause sentence pattern conversion.
SingleConversion This input method uses a single Japanese/Korean Kanji sentence pattern conversion.

2.5 InputScope (set input scope)

InputMethod.InputScopeSet the input range name of the input method. Using this attribute can limit the input content, there are a lot of options for developers to choose. Here is the URL enumeration class introduced by the enumeration value : InputScopeNameValue
has more content, so I won’t go into details here. Readers who need it can go to check the information.

2.6 IsInputMethodSuspended (whether to suspend input method)

InputMethod.IsInputMethodSuspendedSet this property to decide whether to suspend the input method.

Does it help you? Like it~

Guess you like

Origin blog.csdn.net/qq_42068856/article/details/101769749