Search code examples
javaworldwind

Poll for pressed buttons in Java


I have a WorldWind application build based on the Java SDK. It has a great event handler for detecting when you click on objects, but I've run into a snag. While I can click on and select individual objects, I can't determine if the user is pressing the control key while they click (if they want to select multiple objects). I can implement event handlers for both the mouse and the keyboard, but I can't for the life of me figure out how to tie the two together. How could I make my mouse listener poll the system for a list of currently depressed keys?


Solution

  • You can call getModifiers() and bitwise compare to see if the control key (or shift key was depressed during the event.

    public void mouseClicked( MouseEvent e ) {
      if( ( e.getModifiers() & ActionEvent.CTRL_MASK ) > 0 ) {
         // Control key depressed
      } 
    }