2017-06-12 166 views
2

如何在後臺運行這個keylistener?我應該在代碼段中嵌入?代碼工作正常,在前面的窗口,因爲我切換到它 失去它的功能如何在java背景下運行這個按鍵監聽器

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(); 
     } 
    } 
+0

問題中的一個,你說你希望你的應用繼續捕捉按鍵,即使您的應用程序不是專注的應用程序? – Gregg

+1

如果框架在後臺,它將不會收到按鍵。 –

+1

[Java關鍵字偵聽器可能跟蹤所有擊鍵](https://stackoverflow.com/questions/12177416/java-key-listener-to-track-all-keystrokes) – Gregg

回答

0

如果其他窗口也同樣的java程序運行作爲第一個窗口中的另一個窗口,您可以使用

KeyboardFocusManager.addKeyEventDispatcher(java.awt.KeyEventDispatcher) 

與自定義KeyEvenDispatcher

1

可以使用JNativeHook Java庫,會搶了鑰匙,即使你是不是在你的窗口,這裏是附屬用於JNativehook示例代碼。從我的自由年長projects.feel問,如果它不清零,你將需要下載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) { 
    } 
}