问题描述
我正在使用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 Native Interface(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.
所以你想做的是:
-
实施一个C语言中的程序将侦听操作系统上的全局按键,如果此操作系统是Windows而不是查找Windows钩子上的文档,这些文档在Microsoft和MSDN的Web和其他地方都有很好的文档记录。如果您的操作系统是Linux或Mac OS X,那么您将需要使用X11开发库来监听全局按键。根据我在
通过JNI将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.
要记住的重要一点是,如果您首先编写和调试您的JNI代码,它会更容易C / C ++代码并确保它正常工作。然后将它与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中搜索没有焦点的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!