Checking for Ctrl key onReturn
Checking for Ctrl key onReturn
Probably a basic JS or JQuery question, but I'd like to modify the formOptions
onReturn()
function to check if the control key is also pressed. If it is then I'd like the editor to submit the current data and immediately reopen to add another record (for a rapid ability to add entries without clicking the "Create" button in between records).
onReturn: function () {
if (/* control key is pressed*/) {
this.submit( () => {
this.create();
} );
} else {
this.submit();
}
}
However, I can't figure out how to easily check if the control key is pressed without some elaborate onkeydown setup outside of the onReturn
function.
Any ideas on how to do this?
This question has accepted answers - jump to:
Answers
e.ctrl
will tell you if control was pressed.Allan
That's what I thought, but I get it as undefined.
Here's where I look for it in the console log:
Am I overlooking something?
According to the docs, onReturn pass one parameter when used as a function:
It sounds like you need to add the parameter, something like this:
Kevin
Thanks, Kevin.
I had tried that also, but
e
in this case is an instance of Editor, which doesn't have actrl
property and so is undefined.I'm not a jQuery expert by any stretch, but the example of
e.ctrl
that I've seen so far refers toe
as an event object.Here's a SO question that highlgiths what I'm referring to:
https://stackoverflow.com/questions/93695/best-cross-browser-method-to-capture-ctrls-with-jquery
For the
onReturn
function, I'm not sure how to access an event object or if there even is one.The docs actually need to be updated here (done - will be deployed shortly) - it does pass the original event in that you can use, but as the second parameter:
should do it.
Sorry for the confusion. I had thought you were modifying the Editor code internally.
Allan
Thanks, @allan. Unfortunately I'm still getting
undefined
in the console, event when I just doconsole.log(ev)
.Just to make sure there's no syntax errors, here's the new code for the instance of editor, which is the same as above except for the
onReturn
function you've specified above:Oops - sorry. That's something that is coming in 1.8. I just looked at the source and forgot that it was a fairly recent addition! Just working on wrapping up the documentation for 1.8 and the packaging at the moment.
Allan
Ah, ok.
Looking forward to it for several reasons!
@allan, I upgraded to Editor 1.8.0 today and revisited this and I'm still not getting the original event passed to the
OnReturn
event.Again for reference here's my instance of Editor:
Just to add some clarification with 1.8.0, it is executing my custom
OnReturn
function, but the event object doesn't seem to be passed along. I've found the spot in the Editor code that passes it (or at least I think I have at line 5290 in the un-minified version), but for some reason it doesn't come through.OK, another piece of information -- I can get it to work in the un-minified version, but not the minified version.
Doh - sorry. Its
ctrlKey
!If you load this example and then in the console use:
it will show the state of the control key (note the first column in that specific table won't work for inline editing).
If you have the debug version working but not the minified one, it suggests that the minifed one might be cached as the old version.
Allan
It was the cache . . . ITWASTHECACHE!!! AHHHHHHH!!!!!!!!!
I cleared it for the debug version to troubleshoot (and saw that it was ctrlKey) but never thought to do so for the minified one!
Anyway, works perfect now. Thanks, Allan!
Just following up to post my final solution on this one.
Now when a user presses return, the form submits and closes. If they press control+return, it submits and reopens for another entry.
There's also a button on the create form that does the same thing if they press that.