Java Keylistener没有窗口打开? [英] Java Keylistener without window being open?

查看:198
本文介绍了Java Keylistener没有窗口打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java创建一个自动点击器(只有我知道的语言,我刚刚学习了Threads)。我想让applet在它自己的窗口中打开(不在网页上),我希望能够在没有选择窗口的情况下使用空格键启动和停止程序,这样我就可以在另一个程序上使用自动点击器并且能够在没有alt-f4ing一堆东西的情况下阻止它。

I'm trying to create an auto-clicker in Java(only language I know and I just learned Threads). I want to have the applet open in it's own window(not on a webpage), and I want to be able to start and stop the program with the spacebar without the window being selected so that I can use the auto-clicker on another program and be able to stop it without alt-f4ing a bunch of stuff.

有什么可以引用我的东西可以帮助我吗?或者你有任何建议吗?

Is there anything you can refer me to that can help me along with this? Or do you have any suggestions?

推荐答案

这可能超出了Java applet的范围。事实上,全局键盘钩子绝对不能简单地使用Java,但我可以帮助你朝着正确的方向前进。

This might be out of the scope of a Java applet. In fact, global keyboard hooks are definitely out of the scope of simply using Java, but I can help you get moving in the right direction.

然而,你有一些希望。我将指出你的方向 JNI(Java Native Interface),将允许您使用本机库。现在,既然你想留在Java世界,我建议不要直接使用JNI,因为你必须编写一些令人困惑的本机代码(通常是C,C ++)。有几个JNI包装器允许你使用这些函数,但是本机实现被抽象掉了,但是其中很多都要花钱。

However, you have some hope. I'll point you into the direction of JNI (Java Native Interface), which will allow you to use native libraries. Now, since you want to stay in the Java world, I suggest not directly using JNI because you'll have to write some confusing native code (typically C, C++). There are several wrappers for JNI that allow you to use the functions but the native implementations are abstracted away, but a lot of these cost money.

因此,我认为最好的解决方案是 JNA(Java Native Access)。这允许您直接从Java中调用本机库。 (注意:实现不是跨平台的。您必须为Windows,Linux等单独实现。)在项目网站的示例中有一个很好的Windows键盘钩子示例。

So the best solution for you, I think, is JNA (Java Native Access). This allows you to directly call native libraries from within Java. (NOTE: The implementation not will be cross platform. You have to make separate implementations for Windows, Linux, etc.) There's a good example for Windows keyboard hooks in the examples on the project website.

至于打开它自己的窗口而不是在网页中,你是否希望applet不在浏览器中运行,而是在它自己的单独进程中运行,或者只是在一个单独的窗口中,仍然依赖于浏览器窗口打开?

As for opening it's own window not in a webpage, do you want the applet to not run within the browser but in its own separate process, or just be in a separate window and still rely on the browser window being open?


  • 如果您只想启动一个新窗口并仍然需要打开浏览器,那么这是一个很好的例子:

  • If you want to simply launch a new window and still require the browser to be open, then here's a good example:

final Frame window = new Frame("This is the Frame's Title Bar!");
window.add(new Label("This is the Frame."));
window.setSize(300,200);
window.setVisible(true);

window.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
        window.dispose();
    }
});


  • 如果您希望applet生成新进程并在不需要浏览器的情况下运行,请查看 JavaFX

    这篇关于Java Keylistener没有窗口打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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