Please help, Distinguish enter key event in Editor [message #539110] |
Wed, 09 June 2010 16:07  |
Jeffery Yuan Messages: 27 Registered: July 2009 |
Junior Member |
|
|
Hi, all:
I have a command line application, and want to develop a rcp application to provide more functions, such as syntax check and highlight, content assistant, quick fix ect.
Here is my problem:
After user enters the command in the editor, I need to capture the enter key event and call cli method.
I tried this in TextEditor.createPartControl(Composite):
this.getSourceViewer().getTextWidget().addKeyListener(
new KeyListener() {
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.CR) {
// call cli method here
System.out.println("Executing the command");
callCli();
}
}
});
but the editor also provides content assistant function, when user inputs prefix of one command or one option, the editor will immediately open a popup and let user select. in this case, the callCli method would also be executed.
How can I distinguish the enter keyevent when user select these choices from popup window from that enter when user wants to execute the command.
Thanks very much for the reply and help : )
btw, one option is to let user enter "ctrl+enter" to execute command, but this is not a pratical and user-friendly choice.
[Updated on: Wed, 09 June 2010 16:07] Report message to a moderator
|
|
|
Re: Please help, Distinguish enter key event in Editor [message #539933 is a reply to message #539110] |
Mon, 14 June 2010 10:21   |
Dani Megert Messages: 3802 Registered: July 2009 |
Senior Member |
|
|
yuanyun.ken wrote:
> Hi, all:
> I have a command line application, and want to develop a rcp
> application to provide more functions, such as syntax check and
> highlight, content assistant, quick fix ect.
> Here is my problem:
> After user enters the command in the editor, I need to capture the
> enter key event and call cli method.
> I tried this in TextEditor.createPartControl(Composite):
> this.getSourceViewer().getTextWidget().addKeyListener(
> new KeyListener() {
> public void keyReleased(KeyEvent e) {
> }
> public void keyPressed(KeyEvent e) {
> if (e.keyCode == SWT.CR) {
> // call cli method here
> System.out.println("Executing the command");
> callCli();
> }
> }
> });
>
> but the editor also provides content assistant
> function, when user inputs prefix of one command or one option, the
> editor will immediately open a popup and let user select. in this
> case, the callCli method would also be executed.
>
> How can I distinguish the enter keyevent when user select these
> choices from popup window from that enter when user wants to execute
> the command.
You can add an
org.eclipse.jface.text.contentassist.ContentAssistant.addCom pletionListener(ICompletionListener)
to your editor's content assistant.
Dani
> Thanks very much for the reply and help : )
> btw, one option is to let user enter "ctrl+cr" to execute
> command, but this is not a pratical and user-friendly choice.
|
|
|
Re: Please help, Distinguish enter key event in Editor [message #540116 is a reply to message #539110] |
Tue, 15 June 2010 02:02   |
Jeffery Yuan Messages: 27 Registered: July 2009 |
Junior Member |
|
|
Hi, Dani:
Thanks very much for the reply, and it works.
I add CompletionListener to the ContentAssistant, when assistSession starts or ends, I set two flags, through the two flags, I can know whether the enter happens in content assist, and allow or cancel the enter operation accordingly.
the code is as following:
public class CliConfiguration extends SourceViewerConfiguration {
private boolean contentAssistSessionStarted = false;
private boolean contentAssistSessionEnded = false;
@Override
public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
ContentAssistant assistant = new ContentAssistant();
...
assistant.addCompletionListener(new ICompletionListener() {
public void selectionChanged(ICompletionProposal proposal,boolean smartToggle) {
}
public void assistSessionStarted(ContentAssistEvent event) {
contentAssistSessionStarted = true;
contentAssistSessionEnded = false;
}
public void assistSessionEnded(ContentAssistEvent event) {
contentAssistSessionEnded = true;
contentAssistSessionStarted = true;
}
});
return assistant;
}
public class CliCmdInputEditor extends TextEditor {
@Override
public void createPartControl(Composite parent) {
super.createPartControl(parent);
this.getSourceViewer().getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {
public void verifyKey(VerifyEvent e) {
if (e.character == SWT.CR || e.character == SWT.LF) {
if (cliConfiguration.hitEnterKeyInContentAssit()) {
// do nothing, and allow enter key
System.out
.println("the enter key happened in content assist");
return;
}
// cancel enter operation, and call cli method.
System.err.println("call cli method here");
e.doit = false;
}
}
});
}
[Updated on: Tue, 15 June 2010 04:07] Report message to a moderator
|
|
|
Re: Please help, Distinguish enter key event in Editor [message #541434 is a reply to message #539110] |
Mon, 21 June 2010 07:35  |
Jeffery Yuan Messages: 27 Registered: July 2009 |
Junior Member |
|
|
An update to this post, the previous way is problematic.
Instead we can check event.doit property directly:
-- A flag indicating whether the operation should be allowed. Setting this field to false will cancel the operation.
if the enter key happens in content assist, event.doit is set as false:
so the code is as below:
public class CliCmdInputEditor extends TextEditor {
@Override
public void createPartControl(Composite parent) {
super.createPartControl(parent);
this.getSourceViewer().getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {
public void verifyKey(VerifyEvent e)
if (e.doit == false) {
return;
}
if (e.character == SWT.CR || e.character == SWT.LF) {
// don't output newline here
e.doit = false;
System.err.println("call cli method here");
}
}
});
[Updated on: Tue, 22 June 2010 06:39] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.01558 seconds