开发者

isShiftDown when JButton pressed?

开发者 https://www.devze.com 2023-02-23 03:53 出处:网络
Hi all I have a JFrame and I\'ve added a JButton to that JFrame. Also I\'ve added an ActionListener to my JButton.

Hi all

I have a JFrame and I've added a JButton to that JFrame.

Also I've added an ActionListener to my JButton.

Now please convert this Pseudocode to Java :

public void actionPreformed(ActionEvent e){
    if (isShif开发者_StackOverflow社区tDown)
        print "Shift is Down.";
    else
        print "Shift is Up.";
}

Actually I want to know isShiftDown while my JButton pressed or not.

Thanks.


replace isShiftDown by

(e.getModifiers() & InputEvent.SHIFT_MASK) != 0

(e.getModifiers() & ActionEvent.SHIFT_MASK) != 0

getModifiers() returns a bitmask of all modifiers pressed during an event (alt, ctrl, shift...) that you can bitwise-and to get the status of one of them. Pretty much what it says in the doc.

Edit: As of Java 9 it is recommended to use InputEvent.SHIFT_DOWN_MASK

Edit2: In this case(ActionEvent#getModifiers() (Java SE 9 & JDK 9)), should use ActionEvent.SHIFT_MASK instead of InputEvent.SHIFT_MASK

  • ActionEvent.SHIFT_MASK : 1
  • InputEvent.SHIFT_MASK : 1 @Deprecated(since="9")
  • InputEvent.SHIFT_DOWN_MASK : 64
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号