Results 1 to 13 of 13
  1. #1
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default making right-click work with control key and left-click

    I'm a student doing a project for college where I'm making a minesweeper game. I'm trying to make it so that if I hold down the control key and left click, it will do the same thing as right clicking on a minesweeper tile. My reasoning is because I use an Apple laptop, which doesn't have a right click "key" on the mousepad, so it would be nice not to have to have an external mouse to run the program (right click is how I place a flag on a mine). Also, I just want to know how to do it. I've done some searches and haven't had any luck. Here's the code that I have for my ActionEvents that respond to "normal operation":

    Java Code:
    // ActionEvents for GUI setup. Assigns key modifiers.
      @Override
      public void actionPerformed(ActionEvent e)
      {
        String s = e.getActionCommand();
        int id = Integer.parseInt(s);
     
        if (id == -1)  // new game button
        {          
          newGame();
          return;
        }
     
        if (timerStarted == false && Minesweeper.finish == false)
        {
          timerStarted = true;
          timerLabel.setText("000");
        }
    
        if (e.getModifiers() == ActionEvent.SHIFT_MASK)
        {
          rightClick(id);
        }
        else if (e.getModifiers() == ActionEvent.CTRL_MASK)
        {
          doubleClick(id);
        }
        else
        {
          leftClick(id);     
        }
        refresh(this.getGraphics());
      }
    I thought that I could do it with the CTRL_MASK method, but I couldn't get it to work. Any suggestions?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: making right-click work with control key and left-click

    Assuming I understand what you are wanting to do I suggest you simply write an inner class extending MouseAdapter. Then override mousePressed and intercept events when the mouse is pressed. By examining the modifiers set in the event you can tell which mouse button is pressed and if shift, ctrl, alt, etc are also pressed when the mouse button is pressed.

    Edit: BTW, modifiers may contain information for more than one key or mouse event. So do it like this.

    if ((modifier & MASK) == MASK)

    Regards,
    Jim
    Last edited by jim829; 07-24-2013 at 05:49 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default Re: making right-click work with control key and left-click

    Thanks Jim. This is by far the most complicated GUI I've had to program, so I'm learning a lot. We'll see how it goes.

  4. #4
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default Re: making right-click work with control key and left-click

    I got it. Thanks for the help, Jim! I don't know that I did it exactly how you described, but you pointed me in the right direction:

    Java Code:
    // implements the right click functionalities
        @Override
        public void mousePressed(MouseEvent e)
        { 
          // first condition is for single right-click
          // second condition is for single left-click + control key = right-click 
          if((SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) || 
             ((SwingUtilities.isLeftMouseButton(e) && e.isControlDown()) == true))
          {
            ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 
                    this.getActionCommand(),ActionEvent.CTRL_MASK);
     
            for(ActionListener a: this.getActionListeners()) 
            {
              a.actionPerformed(ae);
            }
          }
    My only problem is now unmarking a tile that has already been marked. When I click on a tile that has already been marked with a flag by using left-click + control key, it unmarks it AND then opens it (left-click alone is designated for opening a tile). When I do this with a right-click it behaves as it should (unmarks it but does not open it). I think that I should be able to figure this out though. Thanks again!

    Aaron

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: making right-click work with control key and left-click

    Never test equality of a boolean variable or expression with true/false. It's redundant and error prone.
    Java Code:
    if((SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) || 
        ((SwingUtilities.isLeftMouseButton(e) && e.isControlDown())/* == true*/))
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default Re: making right-click work with control key and left-click

    So there is a difference in the notation between:

    if (boolean)

    and

    if (boolean == true)

    ???

    It was explained to me that there is no difference between the two.

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: making right-click work with control key and left-click

    Quote Originally Posted by dofo View Post
    So there is a difference in the notation between:

    if (boolean)

    and

    if (boolean == true)

    ???

    It was explained to me that there is no difference between the two.
    There isn't, it's just better to not use the equality. if (boolean) and if (!boolean)

  8. #8
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default Re: making right-click work with control key and left-click

    Thank you for the clarification. How/why is it error prone to use the equality? I'm not trying to be disrespectful, just trying to learn.

    Aaron

  9. #9
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: making right-click work with control key and left-click

    Quote Originally Posted by dofo View Post
    Thank you for the clarification. How/why is it error prone to use the equality? I'm not trying to be disrespectful, just trying to learn.

    Aaron
    It is easy to make a typo by typing just '=' instead of '==', which would even compile because it would assign the value you are supposed to be
    testing for to that variable and would cause problems and it is less easily readable.

  10. #10
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default Re: making right-click work with control key and left-click

    Got it. Thank you.

  11. #11
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default Re: making right-click work with control key and left-click

    Quote Originally Posted by dofo View Post
    My only problem is now unmarking a tile that has already been marked. When I click on a tile that has already been marked with a flag by using left-click + control key, it unmarks it AND then opens it (left-click alone is designated for opening a tile). When I do this with a right-click it behaves as it should (unmarks it but does not open it). I think that I should be able to figure this out though. Thanks again!
    I thought that I would be able to rectify this problem, but I am stumped. It has to do with the release of the mouse button. To add to what I wrote above, if I use the control + left-click option, as soon as I press the left-click button, it unmarks the tile as it should, and if I do not release the left-click button, it will not open the tile below the cursor until I release the left-click button. Furthermore, if I do this first part, pressing the control + left-click over the marked tile, the whole time keeping the left-click held in, then move the cursor off of the tile and release the left-click, it does exactly what I want it to do (it doesn't open any tiles).

    I've tried a number of things to no avail. I tried Jim's original suggestion and couldn't get it to behave correctly. If you need to see code, I can post it. It's a LOT of code for the whole program though.

    Aaron

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: making right-click work with control key and left-click

    Quote Originally Posted by dofo View Post
    So there is a difference in the notation between:

    if (boolean)

    and

    if (boolean == true)

    ???

    It was explained to me that there is no difference between the two.
    Why stop at one redundancy?
    Java Code:
    if ((((booleanVariableOrExpression == true) == true) == true) == true)
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    dofo is offline Member
    Join Date
    Jul 2013
    Location
    Charlottesville, USA
    Posts
    12
    Rep Power
    0

    Default Re: making right-click work with control key and left-click

    Quote Originally Posted by DarrylBurke View Post
    Why stop at one redundancy?
    Java Code:
    if ((((booleanVariableOrExpression == true) == true) == true) == true)
    db
    Very charming.

Similar Threads

  1. Replies: 3
    Last Post: 07-04-2011, 01:50 PM
  2. Replies: 2
    Last Post: 06-22-2011, 02:19 PM
  3. reverse of (control + left click)
    By nocturnalhacker in forum IntelliJ IDEA
    Replies: 0
    Last Post: 03-22-2011, 08:30 AM
  4. Replies: 0
    Last Post: 09-04-2009, 08:54 AM
  5. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 05:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •