简体   繁体   English

如果窗口不在顶部,请听击键

[英]Listen to keystrokes if window is not on top

Alright so I have a JFrame that I have a KeyListener added to, and I set the focusable equal to true. 好的,所以我有一个JFrame,其中添加了KeyListener,并将focusable设置为true。 There is no problem with the KeyListener but it only listens to keys if the window is on top. KeyListener没有问题,但仅在窗口位于顶部时才侦听键。 What I'm trying to do is listen to the key strokes if the window is not on top. 我想做的是如果窗口不在顶部,则听一下按键。 Basically, I'm trying to make a program that executes the code when the user presses F10 and stops executing the code when the user presses F10 again. 基本上,我正在尝试制作一个程序,当用户按下F10时执行代码,而在用户再次按下F10时停止执行代码。 The window shouldn't have to be on top for this program to work, meaning that if I press F10 and the JFrame is not on top, it will still execute. 该程序不一定必须位于该窗口的顶部,这意味着如果我按F10且JFrame不在顶部,它将仍然执行。 Any help would be greatly appreciated! 任何帮助将不胜感激! Thank you. 谢谢。

No, Key Bindings/KeyListeners/any pure Java application will not work as the application must be on top for these to work. 否,键绑定/键侦听器/任何纯Java应用程序将无法正常运行,因为该应用程序必须位于最上方才能使其正常运行。

You're looking for a keyboard hook, and Java by itself cannot do this. 您正在寻找键盘挂钩,而Java本身无法做到这一点。 You'll have to use either JNI, JNA that are linked to a native app such as a C/C++ app, or easier still link Java to a scripting utility such as AutoIt (if Windows, or a similar utility if not). 您将必须使用链接到本机应用程序(例如C / C ++应用程序)的JNI,JNA,或者更容易地将Java链接到脚本实用程序(例如AutoIt)(如果使用Windows,则使用类似的实用程序)。 Note that any and all of these solutions must be platform specific. 请注意,所有这些解决方案都必须是特定于平台的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM