private void displayInfo(KeyEvent e, String keyStatus) { // Method copied from http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html // You should only rely on the key char if the event // is a key typed event. int id = e.getID(); String keyString; if (id == KeyEvent.KEY_TYPED) { char c = e.getKeyChar(); keyString = "key character = '" + c + "'"; } else { int keyCode = e.getKeyCode(); keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")"; } int modifiersEx = e.getModifiersEx(); String modString = "extended modifiers = " + modifiersEx; String tmpString = KeyEvent.getModifiersExText(modifiersEx); if (tmpString.length() > 0) { modString += " (" + tmpString + ")"; } else { modString += " (no extended modifiers)"; } String actionString = "action key? "; if (e.isActionKey()) { actionString += "YES"; } else { actionString += "NO"; } String locationString = "key location: "; int location = e.getKeyLocation(); if (location == KeyEvent.KEY_LOCATION_STANDARD) { locationString += "standard"; } else if (location == KeyEvent.KEY_LOCATION_LEFT) { locationString += "left"; } else if (location == KeyEvent.KEY_LOCATION_RIGHT) { locationString += "right"; } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) { locationString += "numpad"; } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN) locationString += "unknown"; } // Added: System.out.println("Keypress: " + keyString); }
@Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 67) // C { if (captureWindow) { this.openCaptureWindow(); } else { this.captureImage(); } } else if (e.getKeyCode() == 27) // Escape { System.out.println("Escape pressed, exiting"); System.exit(0); } else if (e.getKeyCode() == 84) // t { // Testing purpose for (int i = 0; i < images.size(); i++) { System.out.println("image " + i + ", used? " + images_used.contains((Integer) i)); } } else if (e.getKeyCode() == 89) // y { // Testing purpose System.out.println("getRandomImageNum() = " + getRandomImageNum()); } else if (e.getKeyCode() == 73) // i { // Testing purpose System.out.println("LAST ADDED"); for (int i = 0; i < images_lastadded.size(); i++) { System.out.println( i + " - image " + images_lastadded.get(i) + ", used? " + images_used.contains((Integer) images_lastadded.get(i))); } } else if (e.getKeyCode() == 85) // u { // Testing purpose for (int i = 0; i < imagepanels.length; i++) { for (int j = 0; j < imagepanels[i].imagenum_now.length; j++) { for (int j2 = 0; j2 < imagepanels[i].imagenum_now[j].length; j2++) { String print1; if (imagepanels[i].imagenum_now[j][j2] < 10) print1 = " " + imagepanels[i].imagenum_now[j][j2]; else if (imagepanels[i].imagenum_now[j][j2] < 100) print1 = " " + imagepanels[i].imagenum_now[j][j2]; else print1 = "" + imagepanels[i].imagenum_now[j][j2]; String print2; if (imagepanels[i].imagenum_next[j][j2] < 10) print2 = " " + imagepanels[i].imagenum_next[j][j2]; else if (imagepanels[i].imagenum_next[j][j2] < 100) print2 = " " + imagepanels[i].imagenum_next[j][j2]; else print2 = "" + imagepanels[i].imagenum_next[j][j2]; System.out.println( "imagepanels[" + i + "]." + "imagenum_now[" + j + "][" + j2 + "] = " + print1 + ", next = " + print2); } } } } else { displayInfo(e, "KEY TYPED: "); } }
@Override public void keyPressed(KeyEvent arg0) { if (arg0.getKeyCode() == 67) // C { // Are we allowed to capture a new image? if (framenr - lastcapture_framenr > number_of_frames_betweencaptures) { captureImage(); cw.setVisible(false); if (timer != null) timer.cancel(); cwText.setText(""); // Empty text lastcapture_framenr = framenr; cwTimer.cancel(); if (number_of_second_showcapturetext > 0) { cwText.setText("Bildet ble lagret ..."); TimerTask task = new TimerTask() { @Override public void run() { EventQueue.invokeLater( new Runnable() { public void run() { cwText.setText(""); // Empty text } }); } }; timer = new Timer(); timer.schedule(task, (int) (number_of_second_showcapturetext * 1000)); } } else { // Console debug: // System.out.println("At framenr " + framenr + ", " + (framenr-lastcapture_framenr) + // "frames has passed, should be " +number_of_frames_betweencaptures); // System.out.println("You must wait an other "+ // // (int)Math.ceil(((double)number_of_frames_betweencaptures-(double)(framenr-lastcapture_framenr))/fps) + // " seconds"); TimerTask task = new TimerTask() { boolean finished = false; @Override public void run() { EventQueue.invokeLater( new Runnable() { public void run() { if (!finished) { if ((int) Math.ceil( ((double) number_of_frames_betweencaptures - (double) (framenr - lastcapture_framenr)) / fps) > 0) { cwText.setText( "Du må vente " + (int) Math.ceil( ((double) number_of_frames_betweencaptures - (double) (framenr - lastcapture_framenr)) / fps) + " sekunder før nytt bilde"); } else { finished = true; cwText.setText("Du kan nå ta nytt bilde"); } } } }); } }; timer = new Timer(); timer.schedule(task, 0, (1000 / fps)); // Update for every frame } } else if (arg0.getKeyCode() == 27) // Escape { System.out.println("Escape pressed, exiting"); System.exit(0); } }