Java通过ctrl单击来检测OSX上释放了哪个mouseButton

时间:2018-10-07 15:01:26

标签: java macos awt mouseevent processing

我尝试跟踪是否按下了BUTTON1 BUTTON2BUTTON3

问题是在OSX ctrl + click上被视为右键单击。 因此,如果我有以下内容(使用处理程序):

boolean mouse_left_pressed;
boolean mouse_right_pressed;

void setup() {
  size(200, 100);  
}

void draw() {
  background(0);
  fill(255);
  text("left: "+mouse_left_pressed, 50, 50);
  text("right: "+mouse_right_pressed, 50, 75);
}


public void mousePressed(MouseEvent e) {
  if (e.getButton() == LEFT) mouse_left_pressed = true;
  if (e.getButton() == RIGHT) mouse_right_pressed = true;
}

public void mouseReleased(MouseEvent e) {
  if (e.getButton() == LEFT) mouse_left_pressed = false;
  if (e.getButton() == RIGHT) mouse_right_pressed = false;
}

如果我执行以下步骤:

  • 用鼠标左键单击并按住
  • 按住并按住
  • 释放鼠标左键

然后,尽管我再也没有按下鼠标,但是mouse_left_pressed的状态仍然为真。 有没有办法获得释放的实际鼠标按钮?

1 个答案:

答案 0 :(得分:0)

这是因为OSX中的Control +左键单击等效于右键单击。有点hack,但是您可以按如下方式检查修饰符:

boolean mouse_left_pressed;
boolean mouse_right_pressed;

void setup() {
  size(200, 100);  
}

void draw() {
  background(0);
  fill(255);
  text("left: "+mouse_left_pressed, 50, 50);
  text("right: "+mouse_right_pressed, 50, 75);
}


public void mousePressed(MouseEvent e) {
  if (e.getButton() == LEFT) mouse_left_pressed = true;
  if (e.getButton() == RIGHT) {
    if (!e.isControlDown()) {
      mouse_right_pressed = true;
    }
    else {
      mouse_left_pressed = true;
    }
  }
}

public void mouseReleased(MouseEvent e) {
  println(e);
  if (e.getButton() == LEFT) mouse_left_pressed = false;
  if (e.getButton() == RIGHT) {
      if (!e.isControlDown()) {
        mouse_right_pressed = false;
      }
      else {
        mouse_left_pressed = false;
      }
  }
}