WPF模擬鍵盤輸入相關問題

允許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,所以目標能通過發送英文字符收到中文字符)

1

2

3

_uie.Focus(); _uie of TextBox

System.Windows.Forms.SendKeys.SendWait("kwwl");//https://msdn.microsoft.com/zh-cn/library/system.windows.forms.sendkeys.send.aspx

//從當前窗口切出後,原窗口ime狀態會不見,所以在切換前還要保持原來的ime狀態。





TextBox控件,模擬鍵盤輸入(不經過IME,所以目標不能通過發送英文字符收到中文字符) 

1

2

3

4

5

6

7

8

9

10

11

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 

 

WPF模擬鍵盤輸入相關問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章