我要在按住鼠标左键时将布尔值设为true,否则将其设为false,我想使用“ Jnativehook”鼠标侦听器”(https://github.com/kwhat/jnativehook/wiki/Mouse),但布尔值不是不会改变。
代码:
package me.ordinals;
import org.jnativehook.mouse.*;
import java.awt.event.InputEvent;
public class mouseHandler implements NativeMouseListener {
@Override
public void nativeMouseClicked(NativeMouseEvent nativeMouseEvent) {
}
@Override
public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {
if (nativeMouseEvent.getButton() == InputEvent.BUTTON1_DOWN_MASK) {
ac.getInstance().setToggled(true);
}
}
@Override
public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {
if (nativeMouseEvent.getButton() == InputEvent.BUTTON1_DOWN_MASK) {
ac.getInstance().setToggled(false);
}
}
}
答案 0 :(得分:0)
您在这里使用错误的常量:
if (nativeMouseEvent.getButton() == InputEvent.BUTTON1_DOWN_MASK) {
如果您查看NativeMouseEvent API,则如果按下按钮1,则getButton()将返回1:
/** Indicates mouse button #1; used by getButton(). */
public static final int BUTTON1 = 1;
您正在使用java.util.InputEvent
常量,其值为1024,即使使用的是Swing GUI,也没有使用正确的常量。因此更改为
if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1) {
与其他表情相同。