简体   繁体   English

Java(Swing)侦听关键事件

[英]Java (Swing) listen to key events

I am trying to make an Auto Clicker but I am having issues with catching key events, so that users can easily stop the program. 我正在尝试制作自动点击器,但是在捕获关键事件时遇到了问题,因此用户可以轻松地停止该程序。 I have absolutely no idea what is wrong, addKeyListener is not working either. 我完全不知addKeyListener什么问题了, addKeyListener也不起作用。 Can someone please help? 有人可以帮忙吗?

Here is my code: 这是我的代码:

AutoClickerMain Class AutoClickerMain

package com.autoclicker;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class AutoClickerMain {
    public static boolean generate = true;
    public AutoClickerMain(){
        addKeyListener(new KeyInput());
    }


    public static void main(String[] args) throws InterruptedException{
        JFrame frame = new JFrame("Auto Clicker");
        frame.setSize(1086, 1200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("AutoClicker", SwingConstants.CENTER);
        frame.add(label);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.pack();
        frame.setVisible(true);

         try {
            Robot robot = new Robot();
            while (true){
                Thread.sleep(500);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            }
        } catch (AWTException e) {
            e.printStackTrace();
        }

     }

}

KeyInput Class KeyInput

package com.autoclicker;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent event) {
        AutoClickerMain.generate = false;
    }
}

You cant write code using Java swing to catch all global(OS) keyboard events. 您不能使用Java swing来捕获所有全局(OS)键盘事件的代码。 You can only catch key press events if focus is on your JFrame. 如果焦点位于JFrame上,则只能捕获按键事件。 EXAMPLE: 例:

KeyboardFocusManager.getCurrentKeyboardFocusManager()
  .addKeyEventDispatcher(new KeyEventDispatcher() {
      @Override
      public boolean dispatchKeyEvent(KeyEvent e) {
        System.out.println("Got key event!");
        return false;
      }
});

If you want to catch Global key events without focus on your application using java you will need JNI and advanced OS dependent code. 如果要使用Java捕获全局键事件而不关注应用程序,则需要JNI和依赖于OS的高级代码。

so that users can easily stop the program. 这样用户可以轻松地停止该程序。

Create a JMenu with an Exit menu item. 创建一个带有Exit菜单项的JMenu Then you can assign an "accelerator" to the menu item. 然后,您可以为菜单项分配“加速器”。

Read the section from the Swing tutorial on How to Use Menus for more information and examples. 阅读Swing教程中有关如何使用菜单的部分,以获取更多信息和示例。

You can use the ExitAction found in Closing an Application to create the Exit menu item. 您可以使用“ 关闭应用程序”中ExitAction创建“ Exit菜单项。

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

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