Jinput-获取鼠标位置 [英] Jinput - Get Mouse Location

查看:135
本文介绍了Jinput-获取鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jinput打印出鼠标位置:

I am attempting to print out the mouse location using Jinput:

public static void main(String[] args) {
    input = new InputManager();

    while (true) {
        for (Mouse mouse : input.getMice()) {
            mouse.poll();
            System.out.println("Mouse X: " + mouse.getX().getPollData());
            System.out.println("Mouse Y: " + mouse.getY().getPollData());
            System.out.println("---------");
        }
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            // DO NOTHING < BAD
        }
    }
}

这是我的InputManager,它在初始化时扫描所有输入设备,并将所有鼠标分离到一个单独的列表中:

Here is my InputManager, which upon initialization scans for all the input devices, and separates all the mice into a separate list:

public class InputManager {
    public ArrayList<Mouse> mice;

    public InputManager() {
        mice = new ArrayList<Mouse>();
        Controller[] inputs = ControllerEnvironment.getDefaultEnvironment()
            .getControllers();
        for (int i = 0; i < inputs.length; i++) {
            Mouse mouse;
            if (inputs[i].getType() == Controller.Type.MOUSE) {
                mouse = (Mouse) inputs[i];
                mice.add(mouse);
            }
        }
        System.out.println("Discovered " + mice.size() + " mice.");
    }

    public ArrayList<Mouse> getMice() {
        return mice;
    }
}

对于x和y,要打印的信息始终为0.我在Windows 10上运行此程序,是否会引起任何问题?如何使用Jinput从鼠标获取鼠标数据?

The information that is being printed out is always 0 for both x and y. I am running this on windows 10, does that cause any issues? How do I get the mouse data from the mouse using Jinput?

推荐答案

JInput是较低级别的,您在混淆窗口指针和鼠标.鼠标只是相对轴> 2的设备.每次轮询后或在每个事件中的值都不是像素数或位置,它仅是从其先前值开始的大致抽象单位的偏移量.对于相同的物理距离变化,某些鼠标报告的值较大,因此必须对其进行缩放,这就是使用DirectX鼠标(也是相对轴设备)的游戏具有鼠标缩放滑块的原因.

JInput is lower level, you are confusing a window pointer, and a mouse. The mouse is just a device with >2 relative axis. The values after each poll or in each event are not a number of pixels, or a position, it is just an offset in roughly abstract units from it's previous value. Some mice report larger values for the same amount of physical distance changed, so you have to scale it, which is why games that use the directx mouse (also a relative axis device) have a mouse scale slider.

这篇关于Jinput-获取鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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