WPF模拟键盘输入相关问题

来源:互联网 发布:软件项目估计方法 编辑:程序博客网 时间:2024/06/03 20:06

Title: WPF模拟键盘输入相关问题

Author: Kagula

Date: 2015-11-02


允许TextBox输入中文(IME On,默认状态)
  <TextBox Width="200" InputMethod.PreferredImeState="On" 
  InputMethod.IsInputMethodEnabled="True"
  InputMethod.PreferredImeConversionMode="FullShape,Native"></TextBox>


禁止TextBox输入中文(IME Off)
<TextBox Width="200" InputMethod.PreferredImeState="Off" 
InputMethod.IsInputMethodEnabled="False"></TextBox>


指定默认输入法为中文(IME On)
<TextBox Width="200" InputLanguageManager.InputLanguage="zh-CN"></TextBox>


枚举输入法
foreach (System.Windows.Forms.InputLanguage lang in System.Windows.Forms.InputLanguage.InstalledInputLanguages)
if (lang.LayoutName == "Japanese")
{
  System.Windows.Forms.InputLanguage.CurrentInputLanguage = lang;
  InputMethod.Current.ImeState = InputMethodState.On;
}


检查物理键盘是否存在
KeyboardCapabilities keyboardCapabilities = new Windows.Devices.Input.KeyboardCapabilities();
return  keyboardCapabilities.KeyboardPresent != 0 ? true : false;


windows自带的虚拟键盘
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) + Path.DirectorySeparatorChar + "osk.exe");


WPF TextBox PreviewTextInput handle IME (chinese)
http://blog.csdn.net/muzizongheng/article/details/9291637


TextBox控件,模拟键盘输入(经过IME,所以目标能通过发送英文字符收到中文字符)

_uie.Focus(); _uie of TextBoxSystem.Windows.Forms.SendKeys.SendWait("kwwl");//https://msdn.microsoft.com/zh-cn/library/system.windows.forms.sendkeys.send.aspx//从当前窗口切出后,原窗口ime状态会不见,所以在切换前还要保持原来的ime状态。




TextBox控件,模拟键盘输入(不经过IME,所以目标不能通过发送英文字符收到中文字符)
public static void SendInput(UIElement element, string text){    InputManager inputManager = InputManager.Current;    InputDevice inputDevice = inputManager.PrimaryKeyboardDevice;    TextComposition composition = new TextComposition(inputManager, element, text);    TextCompositionEventArgs args = new TextCompositionEventArgs(inputDevice, composition);    args.RoutedEvent = UIElement.PreviewTextInputEvent;    element.RaiseEvent(args);    args.RoutedEvent = UIElement.TextInputEvent;    element.RaiseEvent(args);}

打开IME的一种方式
Window mainWindow = Application.Current.MainWindow;
WindowInteropHelper helper = new WindowInteropHelper(mainWindow);
 IntPtr hImc = ImmGetContext(helper.Handle);
bool imeOpen = ImmGetOpenStatus(hImc);
 
如何控制IME的composition window位置
http://bbs.csdn.net/topics/340034551

1 0