我建立一個虛擬控制器,以便殘障好友可以玩電腦遊戲。 WASD工作正常。但我也需要它來模擬右鍵單擊。我有我的mousePressed
和mouseReleased
事件BUTTON3_DOWN_MASK
,但它實際上並沒有做任何事情。java的機器人右鍵點擊不工作
我的代碼如下。我知道這是馬虎,但我試圖找出第一個:
public class GameController2 extends JFrame implements MouseListener, MouseMotionListener, ComponentListener{
public JLabel CoordsLabel;
public JLabel buttonWA;
public JLabel buttonW;
public JLabel buttonWD;
public JLabel buttonA;
public JLabel buttonD;
public JLabel buttonAS;
public JLabel buttonS;
public JLabel buttonSD;
public JFrame controlFrame;
public int mMovedX;
public int mMovedY;
public Robot robot;
public ImageIcon cIconOver, cIcon, rIcon, rIconOver, brIcon, brIconOver, bIcon, bIconOver, blIcon, blIconOver, lIcon, lIconOver, tlIcon, tlIconOver, tIcon, tIconOver, trIcon, trIconOver;
public int fWidth;
public int fHeight;
public ArrayList<JLabel> buttons;
public int gX = 0;
public int gY = 0;
public Icon origImg;
public GameController2(){
controlFrame = new JFrame();
GridBagLayout gridbag = new GridBagLayout();
controlFrame.setLayout(gridbag);
controlFrame.setSize(300,300);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
tlIcon = new ImageIcon("tlIconTest.png");
buttonWA = new JLabel();
buttonWA.setName("buttonWA");
//buttonWA.setBackground(Color.BLACK);
buttonWA.setOpaque(true);
buttonWA.addMouseListener(this);
buttonWA.addMouseMotionListener(this);
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
gridbag.setConstraints(buttonWA, c);
controlFrame.add(buttonWA);
buttonWA.setIcon(tlIcon);
tIcon = new ImageIcon("tIconTest.png");
buttonW = new JLabel("W");
buttonW.setName("buttonW");
buttonW.setBackground(Color.BLACK);
buttonW.setOpaque(true);
buttonW.addMouseListener(this);
buttonW.addMouseMotionListener(this);
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(buttonW, c);
controlFrame.add(buttonW);
buttonW.setIcon(tIcon);
trIcon = new ImageIcon("trIconTest.png");
buttonWD = new JLabel("W+D");
buttonWD.setName("buttonWD");
buttonWD.setBackground(Color.BLACK);
buttonWD.setOpaque(true);
buttonWD.addMouseListener(this);
buttonWD.addMouseMotionListener(this);
c.gridx = 2;
c.gridy = 0;
gridbag.setConstraints(buttonWD, c);
controlFrame.add(buttonWD);
buttonWD.setIcon(trIcon);
lIcon = new ImageIcon("lIconTest.png");
buttonA = new JLabel("A");
buttonA.setName("buttonA");
buttonA.setBackground(Color.BLACK);
buttonA.setOpaque(true);
buttonA.addMouseListener(this);
buttonA.addMouseMotionListener(this);
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(buttonA, c);
controlFrame.add(buttonA);
buttonA.setIcon(lIcon);
cIcon = new ImageIcon("rMouseOrig.png");
CoordsLabel = new JLabel();
CoordsLabel.setName("CoordsLabel");
//CoordsLabel.setBackground(Color.BLACK);
CoordsLabel.setOpaque(true);
CoordsLabel.addMouseListener(this);
CoordsLabel.addMouseMotionListener(this);
c.gridx = 1;
c.gridy = 1;
gridbag.setConstraints(CoordsLabel, c);
controlFrame.add(CoordsLabel);
CoordsLabel.setIcon(cIcon);
rIcon = new ImageIcon("rIconTest.png");
buttonD = new JLabel();
buttonD.setBackground(Color.BLACK);
buttonD.setOpaque(true);
buttonD.setName("buttonD");
buttonD.addMouseListener(this);
buttonD.addMouseMotionListener(this);
c.gridx = 2;
c.gridy = 1;
gridbag.setConstraints(buttonD, c);
controlFrame.add(buttonD);
buttonD.setIcon(rIcon);
blIcon = new ImageIcon("blIconTest.png");
buttonAS = new JLabel();
buttonAS.setBackground(Color.BLACK);
buttonAS.setOpaque(true);
buttonAS.setName("buttonAS");
buttonAS.addMouseListener(this);
buttonAS.addMouseMotionListener(this);
c.gridx = 0;
c.gridy = 2;
gridbag.setConstraints(buttonAS, c);
controlFrame.add(buttonAS);
buttonAS.setIcon(blIcon);
bIcon = new ImageIcon("bIconTest.png");
buttonS = new JLabel("S");
buttonS.setName("buttonS");
buttonS.setBackground(Color.BLACK);
buttonS.setOpaque(true);
buttonS.addMouseListener(this);
buttonS.addMouseMotionListener(this);
c.gridx = 1;
c.gridy = 2;
gridbag.setConstraints(buttonS, c);
controlFrame.add(buttonS);
buttonS.setIcon(bIcon);
brIcon = new ImageIcon("brIconTest.png");
buttonSD = new JLabel("");
buttonSD.setBackground(Color.BLACK);
buttonSD.setOpaque(true);
buttonSD.setName("buttonSD");
buttonSD.addMouseListener(this);
buttonSD.addMouseMotionListener(this);
c.gridx = 2;
c.gridy = 2;
gridbag.setConstraints(buttonSD, c);
//controlFrame.add(buttonSD);
buttonSD.setIcon(brIcon);
controlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlFrame.setVisible(true);
controlFrame.setFocusableWindowState(false);
rIconOver = new ImageIcon("rIconOverTest.png");
brIconOver = new ImageIcon("brIconOverTest.png");
bIconOver = new ImageIcon("bIconOverTest.png");
blIconOver = new ImageIcon("blIconOverTest.png");
lIconOver = new ImageIcon("lIconOverTest.png");
tlIconOver = new ImageIcon("tlIconOverTest.png");
tIconOver = new ImageIcon("tIconOverTest.png");
trIconOver = new ImageIcon("trIconOverTest.png");
cIconOver = new ImageIcon("rMouseOver.png");
try{
robot = new Robot();
} catch (AWTException e){
e.printStackTrace();
}
}
@Override
public void componentResized(ComponentEvent e){
//
}
@Override
public void componentHidden(ComponentEvent e){
//
}
@Override
public void componentShown(ComponentEvent e){
//
}
@Override
public void componentMoved(ComponentEvent e){
//
}
@Override
public void mouseEntered(MouseEvent e){
JLabel label = (JLabel)e.getSource();
String name = label.getName();
if(name == "buttonSD"){
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_D);
origImg = buttonSD.getIcon();
buttonSD.setIcon(brIconOver);
} else if(name == "buttonS"){
robot.keyPress(KeyEvent.VK_S);
origImg = buttonS.getIcon();
buttonS.setIcon(bIconOver);
} else if (name == "buttonWD"){
robot.keyPress(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_W);
origImg = buttonWD.getIcon();
buttonWD.setIcon(trIconOver);
} else if(name == "buttonW"){
robot.keyPress(KeyEvent.VK_W);
origImg = buttonW.getIcon();
buttonW.setIcon(tIconOver);
} else if(name == "buttonWA"){
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_A);
origImg = buttonWA.getIcon();
buttonWA.setIcon(tlIconOver);
}else if(name == "buttonA"){
robot.keyPress(KeyEvent.VK_A);
origImg = buttonA.getIcon();
buttonA.setIcon(lIconOver);
} else if(name == "buttonAS"){
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_S);
origImg = buttonAS.getIcon();
buttonAS.setIcon(blIconOver);
} else if(name == "buttonD"){
robot.keyPress(KeyEvent.VK_D);
origImg = buttonD.getIcon();
buttonD.setIcon(rIconOver);
} else if(name == "CoordsLabel"){
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_D);
}
System.out.println(label);
}
@Override
public void mouseMoved(MouseEvent m){
//
}
@Override
public void mouseDragged(MouseEvent e){
//
}
@Override
public void mouseExited(MouseEvent e){
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_D);
if((JLabel)e.getSource() != CoordsLabel){
JLabel tmpLabel = (JLabel)e.getSource();
tmpLabel.setIcon(origImg);
}
}
@Override
public void mouseReleased(MouseEvent e){
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
}
@Override
public void mousePressed(MouseEvent e){
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
}
@Override
public void mouseClicked(MouseEvent e){
//
}
public static void main(String args[]){
new GameController2();
}
}
你肯定BUTTON3_DOWN_MASK是右鍵點擊?這聽起來更像是鼠標中鍵,嘗試用BUTTON2_DOWN_MASK – Dazak
@Dazak至於我與awt.Robot記住:BUTTON2 - 中間,按鍵3 - 右 – ParkerHalo
一切我已閱讀說3 ... –