在 Java 中没有焦点地监听输入 [英] Listening for input without focus in Java

查看:38
本文介绍了在 Java 中没有焦点地监听输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Robot 类用 Java 编写一个小程序.该程序接管鼠标.在调试过程中,如果它开始以我不希望的方式运行,则很难退出程序,因为我无法将鼠标移到 eclipse 中的终止按钮上,而且我无法使用热键击中它是因为鼠标在另一个窗口中不断点击,而不是让该窗口获得焦点.

I'm making a small program in Java using the Robot class. The program takes over the mouse. while in the course of debugging if it starts acting in a way that I don't want it's hard to quit the program, since I can't move the mouse over to the terminate button in eclipse, and I can't use hotkeys to hit it because the mouse is constant clicking in another window, giving that window focus instead.

我想做的就是连接一个keylistener,这样当我点击q时我就可以退出程序,但我知道如何做到这一点的唯一方法是制作一个窗口,而该窗口需要焦点来捕获输入.有没有办法从任何地方监听键盘或鼠标输入,而不管什么有焦点?

What I'd like to do is just hook up a keylistener so that when I hit q I can quit the program, but the only way I know how to do this involves making a window, and that window needs focus to capture the input. Is there a way to listen for keyboard or mouse input from anywhere, regardless of what has focus?

推荐答案

这不是一个微不足道的问题,Java 并没有提供一种优雅的方法.您可以使用建议的 banjollity 之类的解决方案,但如果您错误的鼠标点击打开另一个当前在任务栏中打开的全尺寸窗口,即使这样也不会一直有效.

This is not a trivial problem and Java doesn't give you a way to do it elegantly. You can use a solution like banjollity suggested but even that won't work all the time if your errant mouse clicks open another fullsized window currently open in your taskbar for example.

事实是,默认情况下,Java 给开发人员很少的操作系统控制权.这是由于两个主要原因:安全性(如 java 文档所引用)以及不同操作系统处理事件的方式完全不同的事实,并且制作一个统一模型来表示所有这些可能没有多大意义.

The fact is, Java by default gives developers very little control over the OS. This is due to 2 main reasons: security (as citied by java documentation) and the fact that different operating systems handle events completely differently and making one unified model to represent all of these would probably not make a whole lot of sense.

所以为了回答你的问题,我想你想要的是你的程序的某种行为,它在全球范围内侦听按键,而不仅仅是在你的应用程序中.像这样的事情将要求您访问您选择的操作系统提供的功能,而要在 Java 中访问它,您将需要通过 Java 本机接口 (JNI) 层来完成.

So to answer your question, I imagine what you want is some kind of behaviour for your program where it listens for keypresses globally, not just in your application. Something like this will require that you access the functionality offered by your OS of choice, and to access it in Java you are going to need to do it through a Java Native Interface (JNI) layer.

所以你想做的是:

  1. 用 C 语言实现一个程序,该程序将在您的操作系统上侦听全局按键,如果该操作系统是 Windows,那么请查找有关 Windows 挂钩的文档,Microsoft 和 MSDN 在网络和其他地方都有很好的文档记录.如果您的操作系统是 Linux 或 Mac OS X,那么您将需要使用 X11 开发库监听全局按键.这可以根据我在 http://ubuntuforums 上写的 Howto 在 ubunutu linux 发行版上完成.org/showthread.php?t=864566

通过 JNI 将您的 C 代码连接到您的 Java 代码.这一步其实是比较简单的一步.按照我在 http://ubuntuforums.org/showthread.php?t=864566 在 windows 和 linux 下,因为将 C 代码连接到 Java 代码的过程在两种操作系统上都是相同的.

Hook up your C code to your Java code through JNI. This step is actually the easier step. Follow the procedure that I use in my tutorial at http://ubuntuforums.org/showthread.php?t=864566 under both windows and linux as the procedure for hooking up your C code to your Java code will be identical on both OSes.

要记住的重要一点是,如果您首先编写和调试 C/C++ 代码并确保它正常工作,那么让 JNI 代码工作要容易得多.然后将其与 Java 集成很容易.

The important thing to remember is that its much easier to get your JNI code working if you first code and debug your C/C++ code and make sure that it is working. Then integrating it with Java is easy.

这篇关于在 Java 中没有焦点地监听输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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