gpt4 book ai didi

Java检测OSX上按住ctrl单击释放了哪个mouseButton

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:06 27 4
gpt4 key购买 nike

我尝试跟踪 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;
}

如果我执行以下步骤:

  • 用鼠标左键单击并按住
  • 按住 Control 并按住
  • 松开鼠标左键

然后,虽然我不再按下鼠标,但 mouse_left_pressed 的状态仍然为 true。有没有办法获取实际释放的鼠标按钮?

最佳答案

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

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;
}
}
}

关于Java检测OSX上按住ctrl单击释放了哪个mouseButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689763/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com