键盘快捷键 JQUERY - Command+S、Control+S

发布于 2024-08-16 04:45:08 字数 251 浏览 3 评论 0原文

我的网站上有一个文本编辑器,我希望用户能够使用键盘快捷键进行保存,就像在 Microsoft Word 中一样:

  • 对于 Mac:Cmd+S
  • 对于 PC:Ctrl+S

在 jQuery 中实现此目的的最佳方法是什么?另外,这将用于在 CKEditor 中保存内容,CKEditor 使用 iFrame,不确定这是否是一个问题。

I have a text editor on my site and I want users to be able to use keyboard shortcuts to save, just like they would in Microsoft Word:

  • For the Mac: Cmd+S
  • For the PC: Ctrl+S

What is the best way to accomplish this in jQuery? Also, this will be used to save content in CKEditor, which uses an iFrame, not sure if that's an issue or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-08-23 04:45:08

您知道 CKEditor 有内置热键功能吗?

实现起来非常简单:您在配置数组中设置一个数组keylines;从上面的链接获取示例数组;查找“S”的击键;并通过条目和适当的 CKEditor 命令修改数组。我现在无法测试它,但这就是它应该的样子。您可能需要查找适合您要执行的“保存”操作的命令。

... your config array .....

CKEDITOR.config.keystrokes = 
[
    [ 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.CTRL + 83 /*S*/, 'save' ],

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

You are aware that CKEditor has built in hotkey functionality?

It's pretty straightforward to implement: You set up an array keystrokes in your config array; take the example array from the link above; Look up the keystroke for "S"; and amend the array by an entry for it, and the appropriate CKEditor command. I can't test it right now but this is what it should look like. You may need to look up the appropriate command for the "save" operation you want to execute.

... your config array .....

CKEDITOR.config.keystrokes = 
[
    [ 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.CTRL + 83 /*S*/, 'save' ],

    [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ]
];
浅忆流年 2024-08-23 04:45:08

在 Mac 上,要捕获 CMD-s,请将事件处理程序分配给编辑器,如下所示:

CKEDITOR.instances['editor1'].on('key', function (evt) {
    console.log(evt.data.keyCode)
    //cmd-s - save
    if (evt.data.keyCode == 1114195) {
        evt.cancel();
        save();
    }    
});

在我的示例中,我正在调用 save() 函数。

On the Mac, to capture CMD-s, assign an event handler to the editor like this:

CKEDITOR.instances['editor1'].on('key', function (evt) {
    console.log(evt.data.keyCode)
    //cmd-s - save
    if (evt.data.keyCode == 1114195) {
        evt.cancel();
        save();
    }    
});

In my example, I'm calling my save() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文