用JNativeHook制作游戏 [英] Making games with JNativeHook

查看:229
本文介绍了用JNativeHook制作游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为PC游戏开发一个简单的叠加程序。它只是一个位于屏幕中心的透明矩形,但其大小由用户的鼠标滚轮控制。
所以概念就是简单地将透明矩形的大小与敌方玩家的大小相匹配来计算他的距离。

I'm working on a simple overlay program for PC games. It's just a transparent rectangle positioned in the center of the screen but its size is controlled by the user's mouse wheel. So the concept is to simply match the size of the transparent rectangle to the size of the enemy player to calculate his distance.

不幸的是我无法做到这一点与传统的鼠标听众一样,因为鼠标必须同时专注于游戏和叠加程序。
我正在尝试JNativeHook,但我不能让我的矩形更新。有什么建议吗?

Unfortunately I cannot make this happen with conventional mouse listeners because the mouse must be focused on the game and the overlay program at the same time. I'm trying JNativeHook, but I can't get my rectangle to update. Any advice?

public class Main extends Application implements NativeMouseWheelListener {

    Rectangle r = new Rectangle();
    int y = 540; 
    int width = 75;
    int height = 180;
    int velocity = 10;

    @Override
    public void start(Stage stage) throws Exception {
         AnchorPane root = new AnchorPane();
         r = rect(); 
         root.getChildren().add(r);
         root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");

         Scene scene = new Scene(root, 1920, 1080); 
         scene.setFill(null);

         stage.initStyle(StageStyle.TRANSPARENT);
         stage.setScene(scene);
         stage.setX(0);
         stage.setY(0);
         stage.show();
         stage.setAlwaysOnTop(true);
    }

    public void nativeMouseWheelMoved(NativeMouseWheelEvent e) {
        int direction = e.getWheelRotation();
        System.out.println("Mouse Wheel Moved: " + direction);
        r.setY(r.getY() + direction);
    }

    public Rectangle rect() {
        r.setWidth(width);
        r.setHeight(height);
        r.setX(960 - (width/2));
        r.setY(540);
        r.setFill(Color.TRANSPARENT);
        r.setStroke(Color.BLACK);
        return r;
    }

    public static void main(String[] args) {
        go();
        launch(args);
    }

    public static void go() {
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            ex.printStackTrace();
            System.exit(1);
        }

        GlobalScreen.getInstance().addNativeMouseWheelListener(new Main());
    }
}


推荐答案

我强烈建议使用@ vince-emigh建议并在与ui环境相同的线程上运行JNativeHook,方法是使用AbstractExecutorService的自定义实现,如下所示。

I highly recommend taking @vince-emigh advice and running JNativeHook on the same thread as your ui environment by using a custom implementation of the AbstractExecutorService as follows.

public class JavaFxDispatchService extends AbstractExecutorService {
    private boolean running = false;

    public JavaFxDispatchService() {
        running = true;
    }

    public void shutdown() {
        running = false;
    }

    public List<Runnable> shutdownNow() {
        running = false;
        return new ArrayList<Runnable>(0);
    }

    public boolean isShutdown() {
        return !running;
    }

    public boolean isTerminated() {
        return !running;
    }

    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return true;
    }

    public void execute(Runnable r) {
        Platform.runLater(r);
    }
}

然后使用该类作为事件调度程序:

Then use the class as the event dispatcher:

// Set the event dispatcher to a swing safe executor service.
GlobalScreen.setEventDispatcher(new JavaFxDispatchService());

// Initialize native hook.
try {
    GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
    System.err.println("There was a problem registering the native hook.");
    System.err.println(ex.getMessage());
    ex.printStackTrace();
    System.exit(1);
}

GlobalScreen.addNativeKeyListener(this);

有关详细信息,请参阅 wiki

For more information, please see the Swing article in the wiki

这篇关于用JNativeHook制作游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆