Java鼠标侦听器 [英] Java Mouse Listener

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

问题描述

这听起来很简单又愚蠢,但是对于我来说,我找不到一种无需在组件上也可以使用mousePressed的鼠标侦听器的方法. void mousePressed(){}似乎无法按照我想要的方式工作.

This probably sounds simple and stupid, but for the life of me I cannot find a way to have a mouse listener which does mousePressed without having to be on a component. void mousePressed(){} doesn't seem to work the way I want it to.

本质上,我正在制作一个Java程序,该程序旨在在没有图形的情况下工作,并在后台执行操作.因此,例如,如果您单击chrome,它仍然会影响程序.

Essentially I am making a java program which aims to work without graphics, and does things in the background. So if you click in chrome for example it still will effect the program.

我正在尝试的是这个,我意识到这是非常不正确的.

What I was trying was this, which I realize is horribly incorrect.

class MKeyListener extends KeyAdapter {
    @Override
    public void keyPressed(KeyEvent e) {
        moveMouse.playing = false;
    }
}

如推荐的那样,我尝试了JNativeHook库,但是它似乎无法按照我认为的方式工作:

As reccomended I tried the JNativeHook library, however it doesn't seem to work the way I think it should:

public class mousepresstest implements NativeMouseInputListener{

    @Override
    public void nativeMouseClicked(NativeMouseEvent e) {
        System.out.println("worked");
    }
}

按鼠标键不打印文本,我在这里遗漏了什么吗?

It doesn't print the text on mouse pressed, am I missing something here?

推荐答案

Java鼠标侦听器仅用于摆动/自动组件,也用于同一运行过程.

Java Mouse listeners are only meant for swing/awt components and that too from the same running process.

如果您想听其他应用程序的鼠标/键盘事件,请使用 JNativeHook 库.您可以安装全局键盘挂钩并侦听按键事件或鼠标挂钩以了解鼠标事件.您无需使用Swing或其他GUI类.

If you want to listen for mouse/keyboard events from other apps use the JNativeHook library.You can install a global keyboard hook and listen for keypress or a mousehook for mouse events.You do not need to use Swing or other GUI classes.

内部JNativeHook使用JNI提供这些功能.

Internally JNativeHook uses JNI to provide these functionality.

这篇关于Java鼠标侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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