Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 518256

Summary: Way to override default key bindings in Orion Code Edit
Product: [ECD] Orion Reporter: Brian Gleeson <brian.gleeson>
Component: ClientAssignee: libing wang <libingw>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P2 CC: libingw
Version: 14.0   
Target Milestone: 15.0   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Brian Gleeson CLA 2017-06-14 11:29:55 EDT
In Orion Code Editor there are a number of default key bindings that perform certain actions. We would like the ability to override such default bindings with our own custom functionality. We encountered this issue when we wanted to perform a specific client-side action when the user presses the ESC key. But as that key is reserved by the editor's key bindings, it did not work. We had to use CTRL+e instead, as that is not reserved.
Comment 1 libing wang CLA 2017-06-14 14:27:10 EDT
Hi Brian, can you try http://wiki.eclipse.org/Orion/How_Tos/Code_Edit#How_to_override_an_editor.27s_default_action?

the action you want to override is "escape". Just replace "gotoLine" in the example with "escape" and see.
Comment 2 libing wang CLA 2017-06-14 14:27:48 EDT
I give it a quick try like:
                    editorViewer.editor.getTextView().setAction("escape", function(){
                        console.log("my action invoked!");
                        return true;
                    });
And it works
Comment 3 libing wang CLA 2017-06-16 16:29:50 EDT
You can also override any reserved keybindings by the following snippet. Details also addressed in https://wiki.eclipse.org/Orion/How_Tos/Code_Edit#How_to_know_the_reserved_keybindings.

var actionID = editorViewer.editor.getTextView().getKeyModes()[0].match({keyCode:27/*esc*/, type: "keydown", metaKey: false, shiftKey: false, altKey: false, ctrlKey: false});
if(actionID) {
	editorViewer.editor.getTextView().setAction(actionID, function(){
		console.log("my action invoked!");
		return true;
	});
}
Comment 4 libing wang CLA 2017-06-19 09:09:20 EDT
I am trying to find a better way, instead of using editorViewer.editor.getTextView().getKeyModes()[0]. I will update the wiki. Please stay tuned.
Comment 5 libing wang CLA 2017-06-19 11:27:41 EDT
In the perfect world, you should add your own keymode into the wdiget.
Refer to https://github.com/eclipse/orion.client/blob/master/bundles/org.eclipse.orion.client.editor/web/orion/editor/contentAssist.js, around the line of
ContentAssistMode.prototype = new mKeyModes.KeyMode();

However, this coding pattern requires 'orion/keyBinding', etc, which are only available in the requirejs build of the widget.

In the almond build, we are not exporting orion modules yet.

I am closing this bug now.