How to restore default keybindings (Ctrl+A, Ctrl+C, etc) for widgets in SWT?

给你一囗甜甜゛ 提交于 2019-12-24 00:59:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!