0

我有一个执行 JNativeHook.jar 的线程,它创建了一个全局键盘和鼠标侦听器。根据用户输入,线程可以这样做。

当用户(假设)按下 VK_SPACE 时,我想停止所有线程执行。我想生成另一个也监视键盘的线程,当它再次获得 VK_SPACE 时,它会告诉主线程恢复。

这种操作在 Java 中是否可行,它看起来如何?

这是一些可以使用的代码和JNativeHook .jar

代码:

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class GlobalKeyListenerExample implements NativeKeyListener 
{
public void nativeKeyPressed(NativeKeyEvent e) 
{
    System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

    if (e.getKeyCode() == NativeKeyEvent.VK_ESCAPE) 
    {
        GlobalScreen.unregisterNativeHook();
    }
    if (e.getKeyCode() == NativeKeyEvent.VK_SPACE) 
    {
            WatchForMe w = new WatchForMe(this);
            w.start();
            this.wait();
    }
}

public void nativeKeyReleased(NativeKeyEvent e){}
public void nativeKeyTyped(NativeKeyEvent e){}

public static void main(String[] args) 
{
    try 
    {
        GlobalScreen.registerNativeHook();
    }
    catch (NativeHookException ex) 
    {
        System.err.println("There was a problem registering the native hook.");
        System.exit(1);
    }

    //Construct the example object and initialze native hook.
    GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListenerExample());
}
}

public class WatchForMe implements NativeKeyListener 
{
boolean alive = true;
Thread me = null;
public WatchForMe(Thread me)
{
    if(me == null) return;
    this.me = me;
    GlobalScreen.getInstance().addNativeKeyListener(this);
}
public void nativeKeyPressed(NativeKeyEvent e) 
{
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
        if (e.getKeyCode() == NativeKeyEvent.VK_SPACE) 
        {
                me.notify(); 
                alive = false;
        }
}
 public void run()
 {
     while(alive){}
 }

public void nativeKeyReleased(NativeKeyEvent e){}
public void nativeKeyTyped(NativeKeyEvent e){}
}
4

2 回答 2

0

在方法运行中:

public void run(){
 while(me!=null) //execute forever
   try{
      Thread.sleep(5);
      if(!alive) continue; //define pause

      ... do somthing   
   catch(Exception e){}

我们需要“线程”一直在检查这个是否“暂停”。

重要的!建议使用public synchronized void setAlive (boolean value)避免“线程”冲突的方法

于 2013-01-30T02:32:08.973 回答
0

与其跑来跑去试图锁定不同的显示器和其他东西,我个人GlobalKeyListenerExample会在其中放置一个标志,当检测到某个组合键时,开始忽略任何更多传入的键,直到某个组合键出现。

您目前面临的问题是知道 1-您所在的线程以及它可能具有的“暂停”效果以及 2-知道何时引发“取消暂停”组合......因为您不再收听关键事件(因为您可能无意中停止了本机接口用来引发事件的线程......)

public void nativeKeyPressed(NativeKeyEvent e) 
{
    System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

    if (isPaused) {
        if (e.getKeyCode == UNPAUSE_KEY_CODE) { 
            isPaused = false;
        }
    } else {
        if (e.getKeyCode == PAUSE_KEY_CODE) { 
            isPaused = true;
        } else {
            // Handle other key strokes...
        }
    }
}

更新

就像一个旁注......这个

public void run()
{
    while(alive){}
}

不是个好主意。基本上,它使线程处于“运行”状态,不必要地消耗 CPU 周期。就您而言,我不确定您实际上如何能够纠正它...

于 2013-01-30T02:25:53.143 回答