0

I need to make the GUI in java respond to mouse and keyboard input simultaneously.. I know I should add something to the loop in the action listener .. but did not find the right idea .. any suggestions please??

I need to make my GUI respond to the mouse motion and clicks and at the same time respond to the keyboard button pressed if mouse is over a button and pressed enter .. the GUI will respond to the keyboard and the mouse motion actions will continue normally !! .. Hope the problem is cleared!

4
  • 2
    "I know I should add something to the loop in the action listener": what loop? And why does this statement make me nervous? Please give the details of your problem. Assume that we have no idea what your program looks like or is doing. Commented Sep 26, 2012 at 19:21
  • Again, where are you stuck? What behavior are you having trouble eliciting? Is the problem now that you want a certain response when the mouse is hovering over a component and enter is pressed? If not that then what? The specific problem is important. And again, what do you mean by "loop in the action listener"? Again, assume that we have not yet seen your code and that we can't read minds. Ask your question as you would want someone asking you the same question to ask it, if you couldn't see their code or know in advance what they're trying to do. Commented Sep 26, 2012 at 19:38
  • I need an idea not a solution to a certain code problem!! and about the loop.. I thought that mouse motion needs a loop and keyboard storks needs another and I should merge these two loops in a convenient way .... but I don't think it is even a valid thought!!. Commented Sep 26, 2012 at 19:53
  • No, there is no place for a loop here. But again, idea for what? where/at which step exactly are you stuck? How does Alex's "idea" not help? Commented Sep 26, 2012 at 19:56

3 Answers 3

2

You do not have to "add something in loop". You just have to add MouseListener and KeyListener to your GUI element (e.g. Frame) and implement the callback methods as you want.

Sign up to request clarification or add additional context in comments.

4 Comments

Usually it's better not to use a KeyListener with Swing GUI's but rather Key Bindings. That is how Swing GUI components themselves handle keyboard input.
thanks but I already know these things and already did it .. I edited the question .. hope problem will be cleared after edit.
@dawn: his advice will work as long as your program stores and is able to recall the state of the mouse and keyboard and react to changes in state. He can only give you general advice since you are as yet asking a very general question. For a more specific solution, you have to give more specific information (as has already been requested).
Is there an action listener that will fire when EITHER event occurs to simplify the code?
0

Take a look, at Toolkit.addAWTEventLstener

This will allow you monitor all the events flowing the event queue.

The problem your going to have is identifying the components that are in the area of effect and overcoming the default behaviour of the components (pressing enter while a text field has focus will trigger a action event on it, but now you want to do something else)

Comments

0

To get the behavior of responding to a mouse over the button and enter being pressed I would:

  • Use Key Bindings attached to the JButton to allow it to respond to the enter key.
  • Make sure that when doing the above use the InputMap that is associated with the JComponent.WHEN_IN_FOCUSED_WINDOW constant so that the button doesn't actually have to have focus to respond but needs to be in the focused window.
  • Of course bind to the KeyStroke associated with KeyEvent.VK_ENTER.
  • In the key bindings action, check if the button's model is in the rollOver state by calling isRollOver() on the model.
  • and if so, respond.
  • Note that none of this above requires a MouseListener because I'm using the ButtonModel's isRollOver in place of that.

As an example, my SSCCE:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class MouseKeyResponse extends JPanel {
   private JButton button = new JButton("Button");

   public MouseKeyResponse() {
      button.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            System.out.println("button clicked");
         }
      });
      add(button);

      setUpKeyBindings(button);
   }

   private void setUpKeyBindings(JComponent component) {
      int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
      InputMap inputMap = component.getInputMap(condition);
      ActionMap actionMap = component.getActionMap();

      String enter = "enter";
      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
      actionMap.put(enter, new EnterAction());
   }

   private class EnterAction extends AbstractAction {
      @Override
      public void actionPerformed(ActionEvent evt) {
         if (button.isEnabled() && button.getModel().isRollover()) {
            System.out.println("Enter pressed while button rolled over");
            button.doClick();
         }
      }
   }

   private static void createAndShowGui() {
      MouseKeyResponse mainPanel = new MouseKeyResponse();

      JFrame frame = new JFrame("MouseKeyResponse");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.