如何在Gecko Fx 60 C#中使用nsITextInputProcessor?

时间:2019-01-04 20:37:04

标签: c# geckofx

我正在使用Gecko Fx 60(C#代码),并且我想使用nsITextInputProcessor将密钥发送到DOM。

我在https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsITextInputProcessor上有个参考。但是我不能..

代码创建一个实例:

Gecko.nsITextInputProcessor test = Gecko.Xpcom.GetService<Gecko.nsITextInputProcessor>("@mozilla.org/text-input-processor;1");
        test = Gecko.Xpcom.QueryInterface<Gecko.nsITextInputProcessor>(test);

谢谢。

2 个答案:

答案 0 :(得分:0)

another question about geckofx nsITextInputProcessor use

该问题的示例代码:

    private void SendKeyEvent(string type, string key, bool alt, bool ctrl, bool shift)
    {
        // Escape for JS.
        key = key.Replace("\\", "\\\\");
        var instance = Xpcom.CreateInstance<nsITextInputProcessor>("@mozilla.org/text-input-processor;1");
        using (var context = new AutoJSContext(Window))
        {
            var result = context.EvaluateScript(
                $@"new KeyboardEvent('', {{ key: '{key}', code: '', keyCode: 0, ctrlKey : {(ctrl ? "true" : "false")}, shiftKey : {(shift ? "true" : "false")}, altKey : {(alt ? "true" : "false")} }});");
            instance.BeginInputTransaction((mozIDOMWindow)((GeckoWebBrowser)this).Document.DefaultView.DomWindow, new TextInputProcessorCallback());
            instance.Keydown((nsIDOMEvent)result.ToObject(), 0, 1);
            instance.Keyup((nsIDOMEvent)result.ToObject(), 0, 1);
        }

        Marshal.ReleaseComObject(instance);
    }

答案 1 :(得分:0)

如果您想在没有 nsITextInputProcessor 的情况下使用其他解决方案,请尝试以下操作:

public void SendKeys(string message)
{
    foreach (char c in message)
    {
        _browser.Window.WindowUtils.SendNativeKeyEvent(0, 0, 0, c.ToString(), c.ToString());
        Task.Delay(10).Wait();
    }
}