如何在java的后台运行这个关键监听器
Posted
技术标签:
【中文标题】如何在java的后台运行这个关键监听器【英文标题】:how to run this key listener in background in java 【发布时间】:2021-06-29 07:22:29 【问题描述】:如何在后台运行这个关键监听器?我应该在代码部分嵌入什么?代码在前窗口中运行良好,但是当我切换到另一个窗口时 失去其功能。
import java.util.*;
import javax.imageio.*;
import java.awt.*;
import javax.swing.*;
public class KeyListenerExample extends Frame implements KeyListener
Label l;
TextArea area;
KeyListenerExample()
l=new Label();
l.setBounds(20,50,100,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
public void keyPressed(KeyEvent e)
l.setText("Key Pressed");
public void keyReleased(KeyEvent e)
l.setText("Key Released");
public void keyTyped(KeyEvent e)
l.setText("Key Typed");
public static void main(String[] args)
new KeyListenerExample();
【问题讨论】:
您是说即使您的应用程序不是焦点应用程序,您也希望您的应用程序继续捕获按键? 如果 Frame 在后台,它不会收到按键。 Java key listener to track all keystrokes的可能重复 【参考方案1】:您可以使用 JNativeHook Java 库,即使您不在窗口中,它也会抓取密钥,这里是 JNativehook 的示例代码。来自我的一个较旧的项目。如果没有清除,请随时提出问题,您将需要下载 JNativeHook jar 文件
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class Main2 extends JDialog implements NativeKeyListener
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws Exception
EventQueue.invokeLater(new Runnable()
@Override
public void run()
try
Main2 frame = new Main2(new JFrame()
@Override
public boolean isShowing()
return true;
);
catch (Exception e)
e.printStackTrace();
);
/**
* Create the frame.
*
* @throws IOException
*/
public Main2(JFrame jFrame) throws IOException, NativeHookException
System.setProperty("sun.java2d.noddraw", "true");
setBounds(0, 0, 1, 1);
setUndecorated(true);
this.setLocationRelativeTo(null);
try
SwingExecutorService ses = new SwingExecutorService();
GlobalScreen.getInstance().setEventDispatcher(ses);
GlobalScreen.registerNativeHook();
Logger loggy = Logger.getLogger(GlobalScreen.class.getPackage().getName());
loggy.setLevel(Level.SEVERE);
catch (Exception e)
GlobalScreen.getInstance().addNativeKeyListener(this);
setFocusable(true);
setVisible(true);
@Override
public void nativeKeyPressed(NativeKeyEvent e)
char ch = e.getKeyChar();
System.out.println(e.getKeyText(e.getKeyCode()));
int key = e.getKeyCode();
System.out.println(ch);
System.out.println(key);
String modifiers = e.getModifiersText(e.getModifiers());
int rawCode = e.getRawCode();
System.out.println(rawCode);
@Override
public void nativeKeyTyped(NativeKeyEvent e)
private class SwingExecutorService extends AbstractExecutorService
private EventQueue queue;
public SwingExecutorService()
queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
@Override
public void shutdown()
queue = null;
@Override
public List<Runnable> shutdownNow()
return new ArrayList<>(0);
@Override
public boolean isShutdown()
return queue == null;
@Override
public boolean isTerminated()
return queue == null;
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
return true;
@Override
public void execute(Runnable r)
EventQueue.invokeLater(r);
@Override
public void nativeKeyReleased(NativeKeyEvent arg0)
【讨论】:
【参考方案2】:如果这个另一个窗口也和第一个窗口在同一个java进程中运行,你可以使用
KeyboardFocusManager.addKeyEventDispatcher(java.awt.KeyEventDispatcher)
使用您的自定义 KeyEvenDispatcher
【讨论】:
以上是关于如何在java的后台运行这个关键监听器的主要内容,如果未能解决你的问题,请参考以下文章