问题
When I edit a text field and press Ctrl + A, then instead of selecting all text in the field, the main menu's handler for Ctrl + A is called.
How to restore the default behaviour, without losing the Ctrl + A accelerator in menu?
回答1:
Ctrl + A doesn't do anything on my WinXP workstation using the following snippet. So there is no "default behaviour":
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
public class Snippet22 {
public static void main( String[] args ) {
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, 0);
text.setText("ASDF");
text.setSize(64, 32);
shell.pack();
shell.open();
while ( !shell.isDisposed() ) {
if ( !display.readAndDispatch() ) display.sleep();
}
display.dispose();
}
}
If you want Ctrl + A to work as expected, add such a listener:
Listener ctrlAListener = new Listener() {
public void handleEvent( Event event ) {
if ( event.stateMask == SWT.CTRL && event.keyCode == 'a' ) {
((Text)event.widget).selectAll();
}
}
};
and add it to every Text
instance you use:
text.addListener(SWT.KeyUp, ctrlAListener);
来源:https://stackoverflow.com/questions/4143751/how-to-restore-default-keybindings-ctrla-ctrlc-etc-for-widgets-in-swt