java swing判断双击Shift

如何判断是否按下了Shift键?

/***
	 * 判断是否仅仅按下了Shift
	 * @param e
	 * @return
	 */
	public static boolean isJustShiftDown(KeyEvent e){
		int keyCode=e.getKeyCode();
		return e.isShiftDown()&&keyCode!= KeyEvent.VK_TAB
				&&keyCode!= KeyEvent.VK_QUOTE/*双引号*/
				&&keyCode!= KeyEvent.VK_OPEN_BRACKET/*{*/
				&&keyCode!= KeyEvent.VK_CLOSE_BRACKET/*}*/
						&&keyCode!= KeyEvent.VK_COLON/*分号*/;
	}

 

如何判断是否双击呢?

inputTextArea.addKeyListener(new KeyListener() {
			private long lastTimeMillSencond;

			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
			}

			@Override
			public void keyPressed(KeyEvent e) {
				if (EventHWUtil.isJustShiftDown(e)) {
					if (lastTimeMillSencond == 0) {
						lastTimeMillSencond = System.currentTimeMillis();
					} else {
						long currentTime = System.currentTimeMillis();
						if (MenuUtil2.isDoubleClick(currentTime - lastTimeMillSencond )) {
							System.out.println("双击Shift");
							doAction();
							lastTimeMillSencond = 0;
						} else {
							lastTimeMillSencond = System.currentTimeMillis();
						}
					}
				}
			}
		});

 

isDoubleClick的实现如下:

/***
     * 通过时间间隔来判断是否是双击(不是鼠标,是键盘)
     * @param delta
     * @return
     */
    public static boolean isDoubleClick(long delta){
    	return (delta<300&&delta>100);
    }

 

 

 

你可能感兴趣的:(双击Shift,Shift按下,判断按下Shift)