2

Updated: May 13th, 2011

Please find solution in my Answer below.


Updated: May 5th, 2011

So the problem now I'm having is that I want to invoke the JEdtiable submit button after pressing Ctrl+S. The great Thariama has shown the Ctrl+S part for TinyMCE, so now I have to figure out how I get JEditable to submit.

After pressing Ctrl+S I tried to find the submit button and click it as per invoke cancel on jeditable. Doesn't work.

Here's my relevant code for the JEditable Initialization:

    //Edit Note
$(function(){
   $(".edit").editable('ajax/save.php?editnotetext', {
      type : 'mce',
      submit : '<button class="save_button">Save</button>',
      cancel: 'Cancel',
      event: 'dblclick',
      indicator : 'Saving...',
      tooltip : 'Doubleclick to edit...',
      onblur: 'ignore',
      width : '700px',
      height : '100px'
   });
});

And Here's my code for trying to submit it

var receiveShortCutEvent = function(e) {
  if (e.ctrlKey){
        //console.log('e.keyCode:',e.keyCode);
        var handled = true; // default case set this to false which lets the browser execute a browsershortcut if existent
        switch (e.keyCode){   // be careful that keyCode may differ in browsers sometimes 
          case 83 : 
              $('.edit').find('.save_button').click();
              break;

          default : handled = false;
        }
    }
};

Any thoughts would be most welcome!


April 28th, 2011

Using TinyMCE with JEditable (as per http://sam.curren.ws/index.cfm/2008/6/12/jEditable-TinyMCE-Plugin). After editing my content, I'd like to submit it by pressing Ctrl+S. Unfortunately, nothing gets submitted and I'm left with my original content. It does work if I press the submit button.

I've tried:

$(function(){
    $(".edit").keypress(function(event) {
        if ((event.which == 115 && event.ctrlKey)){
            alert("you pressed ctrl-s!");
            $(this).submit();
        }
    });


   $(".edit").editable('ajax/save.php?editnotetext', {
      type : 'mce',
      submit : 'save',
      cancel: 'cancel',
      indicator : 'Saving...',
      tooltip : 'Click to edit...',
      onblur: 'ignore',
      width : '500px',
      height : '100px'
   });
});
Community
  • 1
  • 1
Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120

3 Answers3

1

Take a look at these jquery plugins for creating keyboard shortcuts. It might be what your looking for.

hotkeys

js-hotkeys

locrizak
  • 12,192
  • 12
  • 60
  • 80
  • Hi locrizak. That may help, but unfortunately the real problem is TinyMCE/JEditable as I'm not sure exactly what's going on the scripts and don't have enough technical ability to solve it. E.g. how does one even "bind" the Ctrl+S to the submit button created by JEditable? – Kamil Sindi Apr 20 '11 at 17:08
  • It looks pretty sweet, going to be adding it to a project secretly to blow their minds! – locrizak May 06 '11 at 14:54
1

This is a somewhat special thing to achieve. The problem here is that tinymce (and every mighty rte) uses an iframe for editing the content. This leads to the problem that the iframe will "catch" all keyboard events when the iframe has focus. I will show you what i do to solve this issue

In your tinymce init (or one of your plugins) you need to set the following

// 83 instead of 73 as keyCode here !!!!!!!!
setup : function(ed) {
 ed.onKeyDown.add(function(ed, evt) {

    // catch crtl+s, use receiveShortCutEvent in the html-document
  if (evt.keyCode == 83 && evt.ctrlKey && !evt.shiftKey && !evt.altKey && !evt.metaKey) {
    setTimeout(function(){
      var e = { type : 'keydown'};
      e.charCode = e.keyCode = e.which = 83;
      e.shiftKey = e.altKey = e.metaKey = false;
      e.ctrlKey = true;
      window.parent.receiveShortCutEvent(e); // !!! delegate created event object
    }, 1);
  }
 });
},

in the html-document (or an included js-file) you need to set this

// init in the main document
// Keydown events on the main document will call receiveShortCutEvent
$(document).bind('keydown', function(e)
{
    var handled = receiveShortCutEvent(e); 
    return !handled;
});

you will need to define what to do for which shortcut here too

var receiveShortCutEvent = function(e)
{
  if (e.ctrlKey){
    //console.log('e.keyCode:',e.keyCode);
    var handled = true; // default case set this to false which lets the browser execute a browsershortcut if existent
    switch (e.keyCode){   // be careful that keyCode may differ in browsers sometimes 
      case 83 : 
          alert("you pressed ctrl-s!");
          break;

      default : handled = false;
  }
}
Thariama
  • 50,002
  • 13
  • 138
  • 166
  • Thanks Thariama. I'm having probs with the last part. Can I just put it after the second code? My js doesn't work if I do. – Kamil Sindi Apr 21 '11 at 21:02
  • I also tried `$(document).ready( function receiveShortCutEvent(e) {`. All the other javscript works but Ctrl+S doesn't. – Kamil Sindi Apr 21 '11 at 21:14
  • did you put an alert inside the receiveShortCutEvent unction to see if the function gets called? – Thariama Apr 26 '11 at 08:31
  • i edited my post, looks like i still had an old keyCode in there (72 instead of 83) – Thariama Apr 28 '11 at 14:46
  • Hi Thariama. So the alert is now called, but nothing is submitted. Is there a line of code I should be "uncommenting"? Thanks for all your help btw. – Kamil Sindi Apr 28 '11 at 15:07
  • try to alert (console with firebug is better) the param "e" and tell me what's in it – Thariama Apr 28 '11 at 15:57
  • that is good, you may also try to alert(e.keyCode), you should do something in receiveShortCutEvent depending on the properties of e (there is e.ctrlKey too) – Thariama Apr 28 '11 at 16:32
  • I think the problem I'm having now is submitting with JEditable. I've detailed my plight above. – Kamil Sindi May 05 '11 at 18:03
0

SOLUTION 1: NOT USING TINYMCE

If you're not using TinyMCE with JEditable, then look at Arman P.'s post at Invoke the JEdtiable Submit Button By Modifying Plugin.

SOLUTION 2: USING TINYMCE

As Thariama points out, Tinymce uses an iframe for editing the content. This leads to the problem that the iframe will 'catch' all keyboard events when the iframe has focus. As such, you need to modfy the tinymce customization.

First is in the JEditable initialization, you but give the save button a class, which we will call "save_button":

    $(".edit").editable('ajax/save.php?editnotetext', {
        type : 'mce',
        submit : '<button class="save_button">Save</button>',
        ...
    });

In the TinyMCE initialization, you must create a setup that catches Ctrl+S and submits the buttons of save_button class:

   tinyMCE.init({
    ...
    setup : function(ed) {
     ed.onKeyDown.add(function(ed, evt) {
        // catch crtl+s, use receiveShortCutEvent in the html-document
        if (evt.keyCode == 83 && evt.ctrlKey && !evt.shiftKey && !evt.altKey && !evt.metaKey) {
           evt.preventDefault();
           $('.save_button').submit();
       }
     });
   }

  });
Xyz
  • 5,955
  • 5
  • 40
  • 58
Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120