SWT增添键盘事件

SWT添加键盘事件

  sendText.addKeyListener(new KeyListener(){
   public void keyPressed(KeyEvent e) {
    if(e.keyCode == SWT.CR){
     //让按键原有的功能失效
     e.doit = false;
     //执行你自己的事件
     MessageBox box = new MessageBox(new Shell(), SWT.ICON_INFORMATION | SWT.OK);
        box.setText("提示信息");
        box.setMessage("按回车键了");
        box.open();
    }
   }
   public void keyReleased(KeyEvent e) {}
   });

view plain
  1. package  znewtest;  
  2.   
  3. import  java.net.InetSocketAddress;  
  4. import  java.text.DateFormat;  
  5. import  java.text.SimpleDateFormat;  
  6. import  java.util.Date;  
  7.   
  8. import  org.eclipse.swt.SWT;  
  9. import  org.eclipse.swt.events.KeyEvent;  
  10. import  org.eclipse.swt.events.KeyListener;  
  11. import  org.eclipse.swt.events.SelectionEvent;  
  12. import  org.eclipse.swt.events.SelectionListener;  
  13. import  org.eclipse.swt.layout.GridData;  
  14. import  org.eclipse.swt.layout.GridLayout;  
  15. import  org.eclipse.swt.widgets.Button;  
  16. import  org.eclipse.swt.widgets.Composite;  
  17. import  org.eclipse.swt.widgets.Display;  
  18. import  org.eclipse.swt.widgets.MessageBox;  
  19. import  org.eclipse.swt.widgets.Shell;  
  20. import  org.eclipse.swt.widgets.Text;  
  21.   
  22. public   class  keyWindow {  
  23.     Display display;  
  24.     Shell shell;  
  25.     GridLayout gridLayout;  
  26.     GridData layoutData;  
  27.     Composite composite;  
  28.     Text sendText;  
  29.     Text mesText;  
  30.     DateFormat formatter = new  SimpleDateFormat( "HH:mm:ss" );  
  31.     public  keyWindow(){  
  32.         display = Display.getDefault();  
  33.         shell = new  Shell(display);  
  34.         //初始化shell   
  35.         initShell();  
  36.           
  37.         layoutData = new  GridData();  
  38.         layoutData.widthHint = 260 ;  
  39.         layoutData.heightHint = 200 ;  
  40.         mesText = new  Text(shell, SWT.MULTI | SWT.WRAP   
  41.                 |SWT.V_SCROLL |SWT.BORDER);  
  42.         mesText.setLayoutData(layoutData);  
  43.           
  44.         layoutData = new  GridData();  
  45.         layoutData.widthHint = 260 ;  
  46.         layoutData.heightHint = 60 ;  
  47.         sendText = new  Text(shell, SWT.MULTI | SWT.WRAP   
  48.                 |SWT.V_SCROLL |SWT.BORDER);  
  49.         sendText.setLayoutData(layoutData);  
  50.         sendText.setFocus();  
  51.           
  52.         sendText.addKeyListener(new  KeyListener(){  
  53.             public   void  keyPressed(KeyEvent e) {  
  54.                 if (e.keyCode == SWT.CR){  
  55.                     //让按键原有的功能失效   
  56.                     e.doit = false ;  
  57.                     //执行你自己的事件   
  58.                     MessageBox box = new  MessageBox( new  Shell(), SWT.ICON_INFORMATION | SWT.OK);  
  59.                     box.setText("提示信息" );  
  60.                     box.setMessage("按回车键了" );  
  61.                     box.open();  
  62.                 }  
  63.             }  
  64.             public   void  keyReleased(KeyEvent e) {}  
  65.             });  
  66.     }  
  67.       
  68.     public   void  open() {  
  69.         shell.open();  
  70.         while  (!shell.isDisposed()) {  
  71.             if  (!display.readAndDispatch()) {  
  72.                 display.sleep();  
  73.             }  
  74.         }  
  75.     }  
  76.       
  77.     /**  
  78.      * 设置窗口的标题、位置、大小、图标  
  79.      * @return Shell  
  80.      */   
  81.     public  Shell initShell(){  
  82.         shell.setText("交谈" );  
  83.         shell.setSize(400 350 );  
  84.         shell.setLayout(new  GridLayout());  
  85.         return  shell;     
  86.     }  
  87.     /**  
  88.      * 向聊天区域添加信息  
  89.      * @param msg  
  90.      */   
  91.     public   void  addString(String mes)  
  92.     {  
  93.         mesText.setText(mes + "/n"  + mesText.getText());  
  94.     }  
  95.       
  96.     public   static   void  main(String[] args) {  
  97.         new  keyWindow().open();  
  98.     }  
  99. }  

 

完整示例

 

Color red = display.getSystemColor(SWT.COLOR_RED);
Font font = display.getSystemFont();
control.setFont(font)

Style

Description

SWT.WRAP

Wrap the text to fit the visible area

SWT.LEFT

Left-align the label

SWT.CENTER

Center-align the label

SWT.RIGHT

Right-align the label

SWT.SEPARATOR

Draw a separator instead of text or an image

SWT.HORIZONTAL

Draw the separator horizontally

SWT.VERTICAL

Draw the separator vertically

SWT.SHADOW_IN

Draw the separator with a "shadow in" effect

SWT.SHADOW_OUT`

Draw the separator

Separators

键盘事件类型

SWT.KeyDown

A key was pressed

SWT.KeyUp

A key was released

 

KeyEvent

KeyListener (and KeyAdapter)

keyPressed(KeyEvent)

keyReleased(KeyEvent)



关于event中的character特殊键值

SWT.BS

退回 ('/b')

SWT.CR

回车 ('/r')

SWT.DEL

删除 ('/u007F')

SWT.ESC

ESC ('/u001B')

SWT.LF

换行 ('/n')

SWT.TAB

TAB跳格 ('/t')

 

SWT.CONTROL

 <Ctrl>同 SWT.CTRL

SWT.SHIFT

 <Shift>

SWT.ALT

 <Alt>