2010-07-19 156 views
2

我想使用ckeditor来编辑单行,所以我不希望用户能够使用输入密钥。有谁知道如何停止使用输入密钥?ckeditor关闭输入密钥

我试过$("#text").keypress(function(event){ alert(event.keyCode); });但它没有注册。谢谢你的帮助。

回答

2

这是一种破解,但我得到它的工作方式是改变keystroke配置上发生的事情。我将它设置为“模糊”。

keystrokes: 
    [ 
    [ 13, 'blur'], 
    [ CKEDITOR.SHIFT + 13, 'blur' ], 
    [ CKEDITOR.CTRL + 90, 'undo' ], 
    [ CKEDITOR.CTRL + 89, 'redo' ], 
    [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90, 'redo' ], 
    [ CKEDITOR.CTRL + 66, 'bold' ], 
    [ CKEDITOR.CTRL + 73, 'italic' ], 
    [ CKEDITOR.CTRL + 85 , 'underline' ], 
    [ CKEDITOR.ALT + 109, 'toolbarCollapse' ] 
    ] 

此外,在数据发布之前,我删除了任何段落标签。

0

这不是一个优雅的解决方案(正则表达式可能太简单了,但我很幸运,我不允许源模式)。如果您在关键事件中做了返回错误,它只是拒绝了密钥,会很好。

$('#text').ckeditorGet().on('afterCommandExec', function (e) { 
    var text = $('#text').ckeditorGet().getData(); 
    $('#text').ckeditorGet().setData(text.replace(/<\/*p>/i, '')); 
    $('#text').ckeditorGet().focus(); 
}); 

这将需要与config.shiftEnterMode相结合,CKEDITOR.ENTER_P或你不得不处理BR的为好。由于重置光标位置,焦点并不完美。使用afterCommandExec似乎是在按下输入键之后触发的事件,在没有键盘键的情况下(对于某种键控键,请参阅the onchange plugin

3

这就是我所做的。我创建了一个名为“doNothing”的虚拟插件。只需在'plugins'下创建'doNothing'目录,然后将以下代码粘贴到那里的plugin.js文件中。

(function() 
{ 
    var doNothingCmd = 
    { 
     exec : function(editor) 
     { 
      return; 
     } 
    }; 
    var pluginName = 'doNothing'; 
    CKEDITOR.plugins.add(pluginName, 
    { 
     init : function(editor) 
     { 
      editor.addCommand(pluginName, doNothingCmd); 
     } 
    }); 
})(); 

添加插件到你的config.js文件,config.extraPlugins = 'doNothing';,再放入

config.keystrokes = 
[ 
    [ 13 /*Enter*/, 'doNothing'], 
    [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ], 
    [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ], 

    [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ], 

    [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ], 
    [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ], 
    [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ], 

    [ CKEDITOR.CTRL + 76 /*L*/, 'link' ], 

    [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ], 
    [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ], 
    [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ], 

    [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ] 
]; 

瞧!现在输入键完全没有。你也应该可以将'doNothing'分配给任何你想禁用的键,尽管我自己并没有尝试过其他的键。

0

你遇到的一个问题是,ckeditor使用iframe作为编辑部分,事件(如关键事件)不像iframe那样冒泡出正常元素。

我这样做的方式是连接到ckeditor的关键事件并取消它的输入。事情是这样的:

CKEDITOR.replace($('#myText')[0], //can't be jQuery obj 
{ on: {'key': ckeditorKeyPress} } 
); 

function ckeditorKeyPress(event){ 
    switch(event.data.keyCode){ 
     case 13: //enter key 
     event.cancel(); 
     event.stop(); 
     break; 
    } 
    //trigger an imitation key event here and it lets you catch the enter key 
    //outside the ckeditor 
} 
4

CKEditor的4具有配置对象上提供一个blockedKeystrokes属性。

例,阻止进入和Shift + Enter:

CKEDITOR.inline('someEditorElementId', { 
    blockedKeystrokes: [13, CKEDITOR.SHIFT + 13] 
}); 
+0

'blockedKeystrokes'不是为我工作的CKEditor ** ** 4.7.3,除非我做错了什么 – Drakkin 2017-12-08 16:06:10