当前位置:网站首页>C# 切换中英文输入法
C# 切换中英文输入法
2022-06-25 12:35:00 【dotNET跨平台】
本文经原作者授权以原创方式二次分享,欢迎转载、分享。
原文作者:唐宋元明清
原文地址:https://www.cnblogs.com/kybs0/p/10298697.html
C# 切换中英文输入法
在界面输入时,有时需要限定输入法。
在不自定义正则表达式或者其它输入处理的情况下,切换中英文时与当前语言栏匹配,有以下的几种系统方案:
InputLanguage方案
使用System.Windows.Forms.InputLanguage[1],判断/设置当前输入法状态;
获取当前输入法
/// <summary>
/// 获取当前输入法
/// </summary>
/// <returns></returns>
private string GetCultureType()
{
var currentInputLanguage = InputLanguage.CurrentInputLanguage;
var cultureInfo = currentInputLanguage.Culture;
//同 cultureInfo.IetfLanguageTag;
return cultureInfo.Name;
}切换输入法
/// <summary>
/// 切换输入法
/// </summary>
/// <param name="cultureType">语言项,如zh-CN,en-US</param>
private void SwitchToLanguageMode(string cultureType)
{
var installedInputLanguages = InputLanguage.InstalledInputLanguages;
if (installedInputLanguages.Cast<InputLanguage>().Any(i => i.Culture.Name == cultureType))
{
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo(cultureType));
CurrentLanguage = cultureType;
}
}通过InputLanguage的设置,可以统一同一进程内的输入法。比如切换成英文输入法后,当前程序周期内一直是英文输入状态。
InputMethod附加属性
System.Windows.Input.InputMethod,API原文给的注释是:
可以提供用于管理和使用文本服务框架,它提供的替换文字,如语音和手写的输入法支持的交互功能
1) InputMethod下有依赖属性PreferredImeConversionMode,可以限定输入字符类型,同时切换语言栏的中英文状态。
此属性设置了限定输入字符后,可以通过人工切换输入法,切换回其它输入法。
2)InputMethod.IsInputMethodEnabled,也可以设置中英文输入法限定
一旦设置了,输入控件的输入法不可人工切换。除非重新设置
IsInputMethodEnabled值。
InputMethod适合程序周期内,不同输入框分别有不同输入法的情况。
比如下面俩个文本框的输入法限定:
//通过InputMethod.PreferredImeConversionMode附加属性,设置输入框的输入法输入转换模式,即限定输入字符类型
InputMethod.SetPreferredImeConversionMode(InputTestTextBox0, ImeConversionModeValues.Alphanumeric);
InputMethod.SetPreferredImeConversionMode(InputTestTextBox1, ImeConversionModeValues.Native);ImeConversionModeValues枚举值列表如下:
/// <summary>描述要执行的一种输入法输入转换模式。</summary>
[Flags]
public enum ImeConversionModeValues
{
/// <summary>输入的法使用本机字符 (平假名、 朝鲜语、 繁体中文) 转换模式。</summary>
Native = 1,
/// <summary>输入的法使用片假名转换模式。</summary>
Katakana = 2,
/// <summary>输入的法使用全形转换模式。</summary>
FullShape = 4,
/// <summary>输入的法使用罗马字符转换模式。</summary>
Roman = 8,
/// <summary>输入的法使用字符代码转换模式。</summary>
CharCode = 16, // 0x00000010
/// <summary>输入的方法不会执行任何输入的转换。</summary>
NoConversion = 32, // 0x00000020
/// <summary>输入的法使用 EUDC (最终用户定义的字符) 转换模式。</summary>
Eudc = 64, // 0x00000040
/// <summary>输入的法使用符号转换模式。</summary>
Symbol = 128, // 0x00000080
/// <summary>输入的法使用固定的转换模式。</summary>
Fixed = 256, // 0x00000100
/// <summary>输入的法使用字母数字的转换模式。</summary>
Alphanumeric = 512, // 0x00000200
/// <summary>输入的方法并不关心使用哪种输入的转换方法;实际的转换方法是不确定的。</summary>
DoNotCare = -2147483648, // -0x80000000
}以上俩方案的Demo源码[2]:
PS:如果是winform程序,可以直接设置Control.ImeMode属性
参考[3]
参考资料
[1]
System.Windows.Forms.InputLanguage: https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.inputlanguage?view=netframework-4.7.2
[2]Demo源码: https://github.com/Kybs0/InputLanguageTestDemo
[3]详情可参考: https://blog.csdn.net/iluna/article/details/4903479
边栏推荐
- My first experience of go+ language -- a collection of notes on learning go+ design architecture
- 阿里稳定性之故障应急处理流程
- ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
- Introduction to mongodb chapter 01 introduction to mongodb
- [machine learning] what is machine learning?
- torch.tensor拼接与list(tensors)
- 515. Find Largest Value in Each Tree Row
- Elemtnui select control combined with tree control to realize user-defined search method
- Back test of quantitative trading - tqzfuturerenkowavestrategy
- LeetCode链表题解技巧归纳总结
猜你喜欢

Sword finger offer II 029 Sorted circular linked list

Sword finger offer II 025 Adding two numbers in a linked list

PPT绘图之AI助力论文图

更新pip&下载jupyter lab

Analyse de l'optimisation de la réécriture des requêtes lazyagg de l'entrepôt

架构师必备的七种能力

The editor is used every day. What is the working principle of language service protocol?

剑指offer 第 3 天字符串(简单)
![[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer
![[转]以终为始,详细分析高考志愿该怎么填](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[转]以终为始,详细分析高考志愿该怎么填
随机推荐
Spoken English - weak reading
The amount is verified, and two zeros are spliced by integers during echo
买基金在哪里开户安全?还请赐教
剑指 Offer II 029. 排序的循环链表
Common colors for drawing
CUDA error: unspecified launch failure
量化交易之回测篇 - 期货CTA策略策略(TQZFutureRenkoWaveStrategy)
《MongoDB入门教程》第01篇 MongoDB简介
[AI helps scientific research] fool drawing of loss curve
字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
Oracle trigger error report table or view does not exist
MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
3+1 guarantee: how is the stability of the highly available system refined?
MySQL 学习笔记
Sword finger offer day 3 string (simple)
list.replace, str.append
二叉树之_哈夫曼树_哈弗曼编码
三行代码简单修改jar包的项目代码
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
It is extraordinary to make a move, which is very Oracle!