Silly key input question

I need to check if the Shift key is pressed when moving the mouse. It doesn’t matter when is was pressed, just that is actually pressed at the moment I check.

I poked around a bit and can only find the info I already knew about keyListeners. Then I came across key Bindings, which I really don’t think is what I want either.

The reason behind my question is I am drawing a line. If the user has the shift key down while moving the mouse, I want the line to be constrained either horizontally or vertically. I can work that code out, but I need the magic -

isShiftPressed() method. :slight_smile:

Anyone have my magic method?

Regards,
Dr. A>

java.awt.event.InputEvent:

boolean isShiftDown()
Returns whether or not the Shift modifier is down on this event.

Since mouse event inherits indirectly from InputEvent you should be able to call it.

Whether its honoured on all systems!? Well, this is java, so write it once then test everywhere :slight_smile:

Kev

Wee haw! Test results will occur after I get back to the code tonight!

Thanks Kev!

I probably read the posts wrong, but just in case it isnt “honoured by all systems” you could just use a boolean :stuck_out_tongue:

i.e:


public class Example extends Canvas implements MouseMotionListener, KeyListener {
      boolean shiftDown;
      public Example() {
            shiftDown = false;
      }

      ...
      
      public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
                  shiftDown = true;
            }
      }
      public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
                  shiftDown = false;
            }
      }

      ...
      
      public void mouseMoved(MouseEvent e) {
            if (shiftDown) {
                  // constrain the line
            }
      }
}

edit: btw congrats on your new daughter :smiley:

Would that actually work? I mean if you only press shift do you actually get a keyboard event? Since its a modifier I’d assumed you didn’t get an event for shift.

This really is a question, I really don’t know :slight_smile:

Kev

EDIT: Lower case k on Kev! Whatever next!

Yup, Shift works as a key event. I use it as a key in my game to open up a certain menu and I haven’t had problems on any system with it.

Ah, in that case I’d use that rather than the mouse event modifier since its more likely to work :slight_smile:

Kev

All keys generate proper up/down events.

The exceptions are:
-extended keys (win/menu) and also print (iirc)
-tab(*)
and is alt+ctrl (duh)

(*=


DocComponentListener DCL=new DocComponentListener();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(DCL);
<whatever>.addKeyListener(DCL);
[...]
class DocComponentListener implements KeyListener, KeyEventPostProcessor
{
      public boolean postProcessKeyEvent(KeyEvent e)
      {
            //this one catches tab
            return true;
      }
      public void keyTyped(KeyEvent event)
      {
<etc>
}

)
The additional silly thing with tab is that the msvm generates usual keyevents for that thing, too.

The jar is a hybrid… it also spits some debug info out :slight_smile:

You’ve all touched on some of the stuff I wanted to avoid with key input. I understand that on some JVMs if a key is held, you will get repeats of keyPressed or keypressed/keyreleased. I really don’t want the line visually toggling each time, hence the check for Shift at exactly the point they move the mouse.

I was also worried about the Tab/Shift/Alt, etc being special keys. As long as I can check if Shift is pressed in the mouseEvent, then I’m probably good to go.

Regards,
Dr. A>

PS - woogley - Thanks for the congrats! I sure love being a dad!

PPS - kEV - I’ll try and get the name right next time. :wink:

well, yes, that’s the point of the boolean. as long as the shift key is held, the keyPressed event will be fired about every 10ms (this depends on the oprating system and settings though). since you’re using a boolean to check if shift is down, you can check it any time you want regardless of any events going on. You wont notice the constant keyPress events because all thats happening is:

shiftDown = true;

every 10ms, which isnt exactly a bottleneck to worry about.

Tested out the info you all provided and I have now my drawing doing what I wanted. :slight_smile:

Thanks to all for the infos,
Dr. A>