Reputation: 9189
I have the following Text in swt. I'm trying to display in a text widget a specific text and move the caret to the end just in case the user wants to add something.
The problem is that the caret is always before last character and not after
text = new Text(group, SWT.BORDER);
text.addListener(SWT.KeyDown, e -> {
if (e.keyCode == SWT.ARROW_UP) {
String prevText = "some text from history";
text.setText(prevText);
text.setSelection(prevText.length());
//This doesn't work either: caret is stil before last character
//text.setSelection(prevText.length()+10);
}
});
More details:
Windows7
swt-4.3
SWT-OS: win32
SWT-WS: win32
SWT-Arch: x86_64
On an OSX it behaves properly.
Upvotes: 2
Views: 166
Reputation: 36894
On Windows, using the ▲ and ▼ arrow keys will move the caret left or right.
This only applies to single line Text
widgets though.
So this is very much the intended behaviour and not a bug.
Upvotes: 2
Reputation: 20985
I can confirm your observation running the snippet with SWT 3.106 (as shipped with Eclipse 4.6) on Windows 7.
It is the Up key that changes the position of the caret to the before-last character. On Windows, the Up and Down keys change the cursor position to the left and right respectively. To prevent that from happening you need to stop the Text widget from consuming the key event with
event.doit = false;
This issue only applies to single-line text input fields. If a Text is created with new Text( parent, SWT.MULTI )
, setting the selection works as expected.
Alternatively, for single-line text input fields, use the overloaded setSelection(in,int)
or setSelection(Point)
method. For example:
int selection = text1.getText().length() + 1;
text1.setSelection( selection, selection );
Upvotes: 3