1

我尝试使用 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 上工作

4

1 回答 1

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

因此,您需要看到的是如何将自定义项添加到鼠标缓冲区。

对于如何处理键盘事件,我建议您查看 minecrafts GuiChat.java。

它在你的 Eclipse 的 forgeSrc.jar 中(如果你使用 Eclipse)

查看他们从哪里读取输入,并将缓冲区输入添加到那里。

另外,不要使用线程!只需在 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();
        }
    }
}
于 2016-08-10T06:29:50.017 回答