How do I prevent a multi-line text field from "stealing" tab-key presses?
I mean: I'd like to use TAB to cycle between the elements of a window, but when I enter the multiline text, TAB becomes a "normal" key, and simply inserts tabulators into the text I'm typing.
How do I handle this? Should I write some custom listener, or can I change the behaviour of the component开发者_StackOverflow中文版 by using an SWT constant?
SWT defines a mechanism for implementing this kind of behaviour using the TraverseListener class. The following snippet (taken from here) shows how:
Text text1 = new Text(shell, SWT.MULTI | SWT.WRAP);
text1.setBounds(10,10,150,50);
text1.setText("Tab will traverse out from here.");
text1.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
e.doit = true;
}
}
});
Add a keystroke listener to the textfield or textarea and implement a special handler for TAB. I've seen this 'tab catching' on Swing applications too and it's usually annoying.
You could offer another handler for Shift-TAB which then would insert a \t
, if you still want to allow tabs in the textfield.
精彩评论