文本框模拟键盘输入 WPF

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,所以目标能通过发送英文字符收到中文字符)

  1. _uie.Focus(); _uie of TextBox  
  2. System.Windows.Forms.SendKeys.SendWait("kwwl");//https://msdn.microsoft.com/zh-cn/library/system.windows.forms.sendkeys.send.aspx  
  3. //从当前窗口切出后,原窗口ime状态会不见,所以在切换前还要保持原来的ime状态。  
_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,所以目标不能通过发送英文字符收到中文字符)
  public static void SendInput(UIElement target,Key k) 
        { 
            //将按键的值输入到文本框中
            InputManager inputManager = InputManager.Current; 
            InputDevice inputDevice = inputManager.PrimaryKeyboardDevice;
            TextComposition composition = new TextComposition(inputManager, target, k.ToString());
            TextCompositionEventArgs args = new TextCompositionEventArgs(inputDevice, composition);
           
            args.RoutedEvent = UIElement.PreviewTextInputEvent;
            target.RaiseEvent(args);
            args.RoutedEvent = UIElement.TextInputEvent;
            target.RaiseEvent(args);

            //注册目标控件的KeyDown与KeyUp事件
            KeyboardDevice key = inputManager.PrimaryKeyboardDevice;
            PresentationSource s = key.ActiveSource;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            KeyEventArgs argsx = new KeyEventArgs(key, s, (int)(DateTime.Now - startTime).TotalSeconds, k);
            argsx.RoutedEvent = UIElement.KeyDownEvent;
            target.RaiseEvent(argsx);
            argsx.RoutedEvent = UIElement.KeyUpEvent;
            target.RaiseEvent(argsx);
    
        }
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值