AWT mousePress()和keyPress()不像我在Minecraft Forge中所期望的那样工作

时间:2016-08-09 13:32:23

标签: java awt keypress minecraft-forge mousepress

我尝试使用awt.Robot以编程方式创建鼠标和键事件。问题是关键事件严重或未得到处理。 没有错误消息。这是代码:

package com.ClCH38.CC_Macro.TDI;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

public class Tdi {

    Robot robot;
    Tdi tdi;

    public Tdi() {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public void directInputKey() { 
        try {
            robot.keyPress(KeyEvent.VK_1);    // Choose slot 1.             Work fine 
            Thread.sleep(50);
            robot.keyRelease(KeyEvent.VK_1);// Needed to release key
            Thread.sleep(50);
            robot.keyPress(KeyEvent.VK_2);    // Choose slot 2.@            
                The item is put in hand, but slot 1 is seems always selected
            Thread.sleep(50);
            robot.keyRelease(KeyEvent.VK_2);// Needed to release key
            Thread.sleep(50);
            robot.keyPress(KeyEvent.VK_1);    // Choose slot 1            Don’t change anything
            Thread.sleep(50);
            robot.keyRelease(KeyEvent.VK_1);// Needed to release key
            Thread.sleep(50);
            robot.keyPress(KeyEvent.VK_S);    // Move player back        Don’t work
            Thread.sleep(50);
            robot.keyRelease(KeyEvent.VK_S);// Needed to release key
            Thread.sleep(50);
            robot.mousePress(MouseEvent.BUTTON3_DOWN_MASK);//             Place block
            Thread.sleep(50);
            robot.mouseRelease(MouseEvent.BUTTON3_DOWN_MASK); // mouse button 3 is for AWT mouse right button
            Thread.sleep(50);
            robot.mousePress(MouseEvent.BUTTON3_DOWN_MASK);//             Don’t work
            Thread.sleep(50);
            robot.mouseRelease(MouseEvent.BUTTON3_DOWN_MASK);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

我在Mac OS 10.10.9,Java 8

上工作

1 个答案:

答案 0 :(得分:0)

Forge不会直接处理按键和鼠标事件。

游戏以每秒20次“滴答”的速度运行。其间的所有交互都保存在缓冲区中。

org.lwjgl.input.Mouse;

检索鼠标事件

例如我用于鼠标的是:

      if(this.isHoveringOverOptionsList) {
            if (Mouse.isButtonDown(0))
                {
                if(this.notThisTick == 0) {
                    this.notThisTick = 20;
                    this.handleClickOnQuestion(mouseX,mouseY);
                }
                }
            else {
                while (this.isHoveringOverOptionsList && Mouse.next())
                {
                    int scroll = Mouse.getEventDWheel();
                    if (scroll != 0)
                    {
                        /** moderation is key **/
                        if      (scroll > 0) scroll = -1;
                        else if (scroll < 0) scroll =  1;
                        this.scrollOptions(scroll);
                    }
                }
            }
        }

        if(this.isHoveringOverTextArea) {
            if (Mouse.isButtonDown(0))
            {
                if(this.notThisTick == 0) {
                    if(this.isHoveringOverLineDownButton) {
                        this.scroll(1);
                        this.notThisTick = 2;
                    }
                    if(this.isHoveringOverLineUpButton) {
                        this.scroll(-1);
                        this.notThisTick = 2;
                    }
                }
            }

            else {
                while (this.isHoveringOverTextArea && Mouse.next())
                {
                    int scroll = Mouse.getEventDWheel();
                    if (scroll != 0)
                    {
                        /** moderation is key **/
                        if      (scroll > 0) scroll = -1;
                        else if (scroll < 0) scroll =  1;
                        this.scroll(scroll);
                    }
                }
            }
        }

所以你要看到的是如何将自定义项添加到鼠标缓冲区。

关于如何处理键盘事件,我建议你看看guiChat.java。

你的eclipse中的forgeSrc.jar(如果你使用eclipse)

查看他们从哪里读取输入,然后将你的bufferend输入添加到那里。

另外,不要使用线程!只需在TickEvent

中激活您的活动
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ServerTickEvent;

public class EventHandlerWorld {
    @SubscribeEvent 
    public void calculateAverageTickTime(ServerTickEvent event) {
        if(event.phase == Phase.START) {
            MinecraftServer x = FMLCommonHandler.instance().getMinecraftServerInstance();
        }
    }
}