没有应用程序焦点的Java中的事件监听器? (全球按键检测) [英] Event listener in Java without app having focus? (Global keypress detection)

查看:81
本文介绍了没有应用程序焦点的Java中的事件监听器? (全球按键检测)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一段时间,每个人似乎都认为这不可能只使用Java,所以我会给出一个镜头;)

I've been searching for a while and everybody seems to think this is not possible using just Java, so I'll give SO a shot ;)

是有没有办法让我的Java应用程序监听事件(特别是关键事件),而另一个不相关的应用程序有窗口焦点?在我的情况下,我想要检测用户何时按下键盘上的暂停键,即使我的Java应用程序没有焦点。

Is there any way to have my Java application listen for events (key events in particular) while another unrelated application has window focus? In my situation, I'm looking to detect when the user has pressed the 'Pause' key on the keyboard even though my Java application does not have focus.

我'听到有人提到,唯一的办法是编写一些C代码并使用JNI。只是好奇是否有人知道如何避免这种情况?或者,如果没有,至少是关于c / JNI东西的非常好的教程?

I've heard some people mention that the only way is to write some C code and use JNI. Just curious if anybody knew of a way to avoid that? Or, if not, at least a really nice tutorial on the c/JNI stuff?

谢谢

推荐答案

我的理解是,这是无法做到的。在基础层面上,JVM只为它接收的O / S事件生成事件,并且只有在有焦点时才接收O / S输入事件。

It's my understanding that this can't be done. At a fundamental level, the JVM only generates events for O/S events it receives, and it only receives O/S input events when it has focus.

我确定你可以使用JNI触发O / S为所有输入生成事件,但这将非常依赖于O / S.

I am sure you could use JNI to trigger the O/S to generate events for all input, but that would be very O/S dependent.

这篇关于没有应用程序焦点的Java中的事件监听器? (全球按键检测)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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