您的位置:首页 > 其它

swt中屏蔽系统默认的键盘事件ESC键。

2011-04-26 11:42 190 查看
今天做swt的一个项目中发现在程序运行SplashHandler的时候如果用户按了esc键则程序停止,这时候桌面上面似乎就是剩下了一个图片。所以想办法的解决这个问题,不然用户只能够在任务栏或进程管理中结束程序。

我的思路是当用户按了esc的时候弹出一个对话框,问是否要关闭程序,如果选择是关闭程序,否则程序继续运行。

经过调试查找资料确认下面的代码是可以实现的。现贴出来和大家分享。
package cr.planning.client.splashHandlers;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.splash.AbstractSplashHandler;
import cr.planning.client.Activator;
import cr.planning.client.dialogs.SettingDialog;
import cr.planning.client.service.LoginService;

public class InteractiveSplashHandler extends AbstractSplashHandler {

private Button SetUpButton;
private Button OKButton;
private Text userText;
private Text pwdText;
private Button cancelButton;
private boolean  b;
@Override
public void init(final Shell splash) {
super.init(splash);
createUI();
createUIListeners();
doEventLoop();
}
public InteractiveSplashHandler() {

SetUpButton=null;
OKButton=null;
cancelButton=null;
userText=null;
pwdText=null;
b=false;

}

/**
*
*/
private void createUI(){
Shell shell=new Shell(getSplash(),SWT.NO_TRIM);
shell.addTraverseListener(new TraverseListener() {

@Override
public void keyTraversed(TraverseEvent e) {
// TODO Auto-generated method stub
if (e.detail == SWT.TRAVERSE_ESCAPE) {
boolean   loginOut=MessageDialog.openConfirm(getSplash(), "", "确定要退出吗?");
if(loginOut )
{
System.exit(0);
}else {
e.doit=false;
userText.setFocus();
}
}
}
});
shell.setText("用户登录");
String temppath= Activator.getDefault().getBundle().getLocation();
String path= temppath.replace("initial@reference:file:", "")+"splash.bmp";
System.out.println(path);
shell.setBackgroundImage(new Image(getSplash().getDisplay(),path));

final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
gridLayout.marginRight =10;
gridLayout.marginLeft =200;
gridLayout.marginTop=160;
gridLayout.horizontalSpacing=10;
gridLayout.verticalSpacing=20;
shell.setLayout(gridLayout);

final CLabel userLabel = new CLabel(shell,SWT.RIGHT);
userLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, false, 1, 1));
userLabel.setText("用 户");
userText = new Text(shell, SWT.BORDER|SWT.FILL);
userText.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,2,1));

final CLabel pwdLabel = new CLabel(shell,SWT.RIGHT);
pwdLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, false, 1, 1));
pwdLabel.setText("密 码");
pwdText = new Text(shell, SWT.BORDER);
pwdText.setEchoChar('*');
pwdText.setLayoutData(new GridData(GridData.FILL,GridData.FILL,true,false,2,1));
SetUpButton =new Button(shell, SWT.CENTER);
SetUpButton.setLayoutData(new GridData(GridData.BEGINNING,SWT.None,false,false,1,1));
SetUpButton.setText(" 设 置 ");

OKButton =new Button(shell, SWT.CENTER);
OKButton.setText(" 登 录 ");
OKButton.setLayoutData(new GridData(SWT.None,GridData.CENTER,false,false,1,1));

cancelButton =new Button(shell, SWT.CENTER);
cancelButton.setText(" 取 消 ");
cancelButton.setLayoutData(new GridData(GridData.END,SWT.None,false,false,1,1));
int screenH=Display.getCurrent().getBounds().height;
int screenW=Display.getCurrent().getBounds().width;
shell.setLocation(screenW/2-455/2,screenH/2-295/2);
shell.setSize(new Point(455, 295));
pwdText.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.character==SWT.CR) {
validateLogin();
}
}
});
shell.open();

}

/**
*
*/
private void doEventLoop() {
Shell splash = getSplash();
while (b == false) {
if (splash.getDisplay().readAndDispatch()==false) {
splash.getDisplay().sleep();
}
}
}
private void createUIListeners() {
// Create the OK button listeners
createUIListenersButtonOK();
// Create the cancel button listeners
createUIListenersButtonCancel();

createUIListenersButtonSetting();
}
/**
* 设置按钮监听事件
*/
private void createUIListenersButtonSetting(){
SetUpButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SettingDialog(getSplash()).open();
}
});
}

/**
* 取消按钮监听器
*/
private void createUIListenersButtonCancel() {
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
boolean   loginOut=MessageDialog.openConfirm(getSplash(), "", "确定要退出吗?");
if(loginOut )
{
System.exit(0);
}
}
});
}

/**
* 登录按钮监听器
*/
private void createUIListenersButtonOK() {
OKButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
validateLogin();
}
});
}
public void validateLogin(){
String userName=userText.getText().trim();
String passWrod=pwdText.getText().trim();
if(userName =="" || passWrod==""){
MessageDialog.openError(getSplash(), "出错", "用户名或者密码不能为空!");
userText.setFocus();
return;
}else {
int IsOk=new LoginService().IsAccessible(userName, passWrod);
if (IsOk==2) {
b=true;
}else if(IsOk==0){
MessageDialog.openError(getSplash(), "出错", "用户名或者密码错误!");
userText.setText("");
pwdText.setText("");
userText.setFocus();
}else if (IsOk==3) {
MessageDialog.openError(getSplash(), "出错", "您没有权限进入此系统,请联系系统权限管理员!");
userText.setText("");
pwdText.setText("");
userText.setFocus();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: