gpt4 book ai didi

java - AWT mousePress() 和 keyPress() 在 Minecraft Forge 中无法按我的预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 12:21:05 25 4
gpt4 key购买 nike

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

最佳答案

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)

查看他们从哪里读取输入,并将您的 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();
}
}
}

关于java - AWT mousePress() 和 keyPress() 在 Minecraft Forge 中无法按我的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38852257/

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