Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2007

Tip: Looking for answers? Try searching our database.

Checking if certain key is pressed during mouse events. How to do it?

Thread view: 
Kamil.Szot@gmail.com - 14 Jan 2007 03:05 GMT
In Photoshop when you hold spacebar you can drag the zoomed in document
with left mouse button.
I want to achieve same effect in Swing java app.
I need to test in mouse event whether spacebar is pressed. I know how
to do it for Alt, Shift and similar special keys, but how to do it for
spacebar ?
Dan Andrews - 17 Jan 2007 19:40 GMT
> In Photoshop when you hold spacebar you can drag the zoomed in document
> with left mouse button.
> I want to achieve same effect in Swing java app.
> I need to test in mouse event whether spacebar is pressed. I know how
> to do it for Alt, Shift and similar special keys, but how to do it for
> spacebar ?

Hi Kamil,

I think you are going to have to keep track of the state of the
spacebar yourself. Try this sample code below and see if it helps you
out.

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - -

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestFrame extends JFrame {

 TestFrame() {
   JPanel panel = new JPanel();
   panel.setPreferredSize(new Dimension(100, 100));

   /*
    * Not really much point in the panel being focusable
    * but for the example we need key events.
    */
   panel.setFocusable(true);

   SpecialListener listener = new SpecialListener();
   panel.addMouseListener(listener);
   panel.addMouseMotionListener(listener);
   panel.addKeyListener(listener);

   getContentPane().add(panel, BorderLayout.CENTER);
 }

 private void processSpaceBarDrag(MouseEvent e) {
   System.out.println("Processing spaceBar drag");
 }

 private void completeSpaceBarDrag() {
   System.out.println("Completing spaceBar drag");
 }

 private class SpecialListener extends MouseAdapter implements
     MouseMotionListener, KeyListener {

   private boolean spaceBarPressed = false;

   private boolean spaceBarDragging = false;

   public void mouseReleased(MouseEvent e) {
     if (spaceBarDragging) {
       spaceBarDragging = false;
       completeSpaceBarDrag();
     }

   }

   public void mouseDragged(MouseEvent e) {
     if (spaceBarPressed) {
       spaceBarDragging = true;
       processSpaceBarDrag(e);
     }
   }

   public void mouseMoved(MouseEvent e) {

   }

   public void keyPressed(KeyEvent e) {
     if (e.getKeyCode() == KeyEvent.VK_SPACE) {
       spaceBarPressed = true;
     }
   }

   public void keyReleased(KeyEvent e) {
     if (e.getKeyCode() == KeyEvent.VK_SPACE) {
       spaceBarPressed = false;
       if (spaceBarDragging) {
         spaceBarDragging = false;
         completeSpaceBarDrag();
       }
     }
   }

   public void keyTyped(KeyEvent e) {

   }
 }

 public static void main(String[] args) {
   JFrame frame = new TestFrame();
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);

 }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.