swt中屏蔽系统默认的键盘事件ESC键。
2011-04-26 11:42
190 查看
今天做swt的一个项目中发现在程序运行SplashHandler的时候如果用户按了esc键则程序停止,这时候桌面上面似乎就是剩下了一个图片。所以想办法的解决这个问题,不然用户只能够在任务栏或进程管理中结束程序。
我的思路是当用户按了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(); } } } }
相关文章推荐
- Swiift3.0 UITextFiled键盘返回事件,点击其他区域,键盘收起,停止编辑;UITableViewCell取消选中行,以及系统默认的线的长短调整
- SWT屏蔽控件的某鼠标或键盘事件
- 学习SWT的笔记 --键盘事件处理
- Win10系统中开机怎么设置默认开启小键盘锁?
- 抽奖系统(键盘事件)
- 屏蔽默认的事件功能
- 手机端input,select屏蔽浏览器默认事件
- Unity UGUI中ScrollView的拖拽与被UGUI事件系统屏蔽的问题
- 解决 jquery在iOS系统自带键盘无法触发keyup,keydown,keypress事件
- 使用Javascript封装实现屏蔽鼠标右键系统键菜单,并绑定自定义的事件
- 取消和还原键盘事件默认行为
- VC 屏蔽 ENTER或者ESC键 退出事件
- 屏幕锁定源程序(屏蔽一般的系统热键以及键盘上任何你想要屏蔽的按键)
- 控制台屏蔽键盘消息事件
- Hook屏蔽键盘和鼠标事件
- 屏蔽系统自带键盘
- JS屏蔽键盘相关事件
- EditText的点击事件屏蔽键盘响应
- 使用SWT模拟鼠标键盘事件
- 屏蔽系统的键盘弹出