3

如果按下 F10 键,我想停止程序。此代码键入 1000 次。我正在使用 jnativehook 库(https://github.com/kwhat/jnativehook/wiki/Usage)。

程序运行(使用 F9)但是当我按 F10 时它并没有停止。

-------------------------------------------------- -----------------------------------------


package jnativehook01;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.dispatcher.SwingDispatchService;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class SwingExample extends JFrame implements NativeKeyListener, WindowListener {
    private static final long serialVersionUID = 1L;

    public SwingExample() {
        // Set the event dispatcher to a swing safe executor service.
        GlobalScreen.setEventDispatcher(new SwingDispatchService());

        setTitle("JNativeHook Swing Example");
        setSize(300, 150);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(this);
        setVisible(true);
    }

    public void windowOpened(WindowEvent e) {
        // Initialze native hook.
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            ex.printStackTrace();

            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(this);
    }


    public void windowClosing(WindowEvent e) { /* Unimplemented */ }
    public void windowIconified(WindowEvent e) { /* Unimplemented */ }
    public void windowDeiconified(WindowEvent e) { /* Unimplemented */ }
    public void windowActivated(WindowEvent e) { /* Unimplemented */ }
    public void windowDeactivated(WindowEvent e) { /* Unimplemented */ }

    // Start program (F9)
    public void nativeKeyReleased(NativeKeyEvent e) {
        if (e.getKeyCode() == NativeKeyEvent.VC_F9) {

            Robot bot;  
            try {
                String text = "a";
                StringSelection stringSelection = new StringSelection(text);
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                clipboard.setContents(stringSelection, stringSelection);

                bot = new Robot();

                for (int i = 0; i < 1001; i++) {

                    //t
                    bot.keyPress(KeyEvent.VK_T);
                    bot.delay(100);
                    bot.keyRelease(KeyEvent.VK_T);

                    bot.delay(500);

                    // Type "a"
                    bot.keyPress(KeyEvent.VK_CONTROL);
                    bot.keyPress(KeyEvent.VK_V);
                    bot.keyRelease(KeyEvent.VK_V);
                    bot.keyRelease(KeyEvent.VK_CONTROL);

                    bot.delay(500);

                    //Enter

                    bot.keyPress(KeyEvent.VK_ENTER);
                    bot.keyRelease(KeyEvent.VK_ENTER);

                    if (e.getKeyCode() == NativeKeyEvent.VC_F10) {
                         System.exit(1);
                    }

                    // Wait 4 sec for the new fill
                    bot.delay(4000);

                }

            } catch (AWTException e1) {
            }
        }


    }

    public void nativeKeyPressed(NativeKeyEvent e) { /* Unimplemented */ }
    public void nativeKeyTyped(NativeKeyEvent e) { /* Unimplemented */ }

    public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SwingExample();
            }
        });
    }

    @Override
    public void windowClosed(WindowEvent e) {
        // TODO Auto-generated method stub

    }
}
4

2 回答 2

1

更可能的问题似乎在 if 块内

if (e.getKeyCode() == NativeKeyEvent.VC_F10) {
                     System.exit(1);
                }

System.exit(1) 将在发生错误时使用,因此 System.exit(1) 主要用于 Catch 块。

if (e.getKeyCode() == NativeKeyEvent.VC_F10) {
                     System.exit(0);return;
                }

这应该可以完成这项工作。供参考System.exitDetail TerminatingAjavaProgram

于 2017-09-18T13:46:24.453 回答
0

这里有几件事。首先,您正在使用 swing,因此您必须在 AWT 事件调度线程上访问 swing 组件。有一个调度程序可以用作这个库的一部分。请参阅项目页面上的使用 swing wiki 指南。

现在谈谈你的实际问题;如何停止循环?我看到您在事件侦听器上有一个循环,它将阻止接收 F-10 之类的事件,直到循环完成并且线程可以继续调度事件。您应该在自己的线程中启动 F-9 进程循环,并使用一些线程安全机制在按下 F-10 时发出该循环的信号。如果您出于某种原因想要停止本机挂钩,则 GlobalScreen 上有一个方法可以取消挂钩。

于 2017-09-18T21:51:19.673 回答