简体   繁体   English

如何从JavaScript模拟CKEditor上的DEL键

[英]How to simulate DEL key press on CKEditor from JavaScript

Short story: 短篇故事:

// get the editor instance
var editor = CKEDITOR.instances.editor1;

// this is what I want, but it does not exist
editor.execCommand('delete');

// I've played with this, found somewhere, but without success.
editor.fire('key', { keyCode : 46 } )

Long story: 很长的故事:

there is a problem when using the CKEditor within the Webbrowser control from .NET WindowsForms. 在.NET WindowsForms的Webbrowser控件中使用CKEditor时出现问题。 Several keys, including the DELete key are not propagated to the control at all. 包括DELete键在内的几个键根本不会传播到控件。

I managed to intercept the key using a global keyboard hook and sent window messages direct to the embedded IE window handle, but without success. 我设法使用全局键盘钩子来拦截键,并将窗口消息直接发送到嵌入式IE窗口句柄,但没有成功。

Now my goal is to simulate the delete key from within the javascript, because I can call a js function from my .NET app. 现在,我的目标是从javascript中模拟Delete键,因为我可以从.NET应用程序中调用js函数。 Somehow this must work, because it works within the virtual keyboard plugin. 它必须以某种方式起作用,因为它在虚拟键盘插件中起作用。 (see sample ) (请参阅示例

Sadly I wasn't able to get how this works from the plugin code. 可悲的是,我无法从插件代码中了解其工作原理。 I would be glad if anybody can post a working sample. 如果有人可以发布工作样本,我将非常高兴。

Thanks! 谢谢!

I found a library used in javascript virtual keyboard ( Jsvk plugin ). 我找到了一个用于javascript虚拟键盘( Jsvk插件 )的库。
It is called DocumentSelection and can be found here . 它称为DocumentSelection,可以在此处找到。

<script src="./documentselection.js"></script>

function simulateDelete()
{
    var editor = CKEDITOR.instances.editor1;
    var container = (editor.container.getElementsByTag('textarea').getItem(0) ||
                     editor.container.getElementsByTag('iframe').getItem(0)
                    ).$;
    DocumentSelection.deleteAtCursor(container, true);
}

Maybe someone has an easier solution without the need of an external libaray. 也许有人不需要外部libaray就能轻松解决问题。

I think u want some clues... 我想你想要一些线索...

There it is...Check the Documentation 那里...查看文档

Key Event in CKEditor CKEditor中的关键事件

alert( event.getKey() );

to get the key Element and also the other one is 得到关键元素,另外一个是

alert( event.getKeystroke() == 65 );// "a" key
alert( event.getKeystroke() == CKEDITOR.CTRL + 65 );// CTRL + "a" key
alert( event.getKeystroke() == CKEDITOR.CTRL + CKEDITOR.SHIFT + 65 );//CTRL + SHIFT + "a" key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM