2014-10-06 36 views
3

我看了一些例子,並試圖在Google上搜索幾個小時,但似乎我找不到一個涵蓋我所問的問題。用JNativeHook註冊多鍵壓力機

如果我想這樣做

W +一個在同一時間,並註冊爲一個獨立的按鍵事件而不是僅僅作爲W和單獨的,我怎麼會去這樣做使用JNativeHook是什麼?是否因爲我錯過了某個覆蓋此類的課程,或者是否有某種解決方法只能讀取一個密鑰?

我想在控制檯中做到這一點(因此我使用這個庫,而不是Swing)。

我把他們的榜樣,並希望嘗試和修改:

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

public class GlobalKeyListener implements NativeKeyListener { 

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

     if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) { 
      GlobalScreen.unregisterNativeHook(); 
     } 
    } 

    @Override 
    public void nativeKeyReleased(NativeKeyEvent e) { 
     System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode())); 
    } 

    @Override 
    public void nativeKeyTyped(NativeKeyEvent e) { 
     System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode())); 
    } 

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

      System.exit(1); 
     } 

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

回答

3

你要聽每一個人的按鍵和所需的組合釋放事件,並設置一些種類標誌爲每個鍵被壓下。如果在按下所需的一個鍵之後,並且符合條件,那麼當這些鍵被按在一起時,您可以做任何你需要做的事情。如果不創建自定義鍵盤驅動程序,無法爲兩個鍵獲取單個事件。如果您的目標是壓制W和A關鍵事件,直到兩者都按下,請查看維基的unsupported consuming events部分。請注意,事件抑制僅適用於Windows和OS X目標,並且抑制的事件不會傳遞到其他應用程序。

它不是最漂亮的例子,但它應該做你正在尋找的東西。

private short hotKeyFlag = 0x00; 
private static final short MASK_A = 1 << 0; 
private static final short MASK_W = 1 << 1; 

... 
@Override 
public void nativeKeyPressed(NativeKeyEvent e) { 
    if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) { 
     GlobalScreen.unregisterNativeHook(); 
    } 
    else if (e.getKeyCode() == NativeKeyEvent.VK_A) { 
     hotKeyFlag &= MASK_A; 

     // Check the mask and do work. 
     if (hotKeyFlag == MASK_A & MASK_W) { 
      // Fire Shortcut. 
     } 
    } 
    else if (e.getKeyCode() == NativeKeyEvent.VK_W) { 
     hotKeyFlag &= MASK_W; 

     // Check the mask and do work. 
     if (hotKeyFlag == MASK_A & MASK_W) { 
      // Fire Shortcut. 
     } 
    } 
} 

@Override 
public void nativeKeyReleased(NativeKeyEvent e) { 
    if (e.getKeyCode() == NativeKeyEvent.VK_A) { 
     hotKeyFlag ^= MASK_A; 
    } 
    else if (e.getKeyCode() == NativeKeyEvent.VK_W) { 
     hotKeyFlag ^= MASK_W; 
    } 
} 
2

這就是我的回答: -

private boolean a = false, w = false; 

@Override 
public void nativeKeyPressed(NativeKeyEvent e) { 
    if (e.getKeyCode() == NativeKeyEvent.VC_A) { 
     a = true; 
     if (w) { 
      System.out.println("W+A"); 
     } else {//remove this else only for testing 
      System.out.println("Only A"); 
     } 
    } else if (e.getKeyCode() == NativeKeyEvent.VC_W) { 
     w = true; 
     if (a) { 
      System.out.println("A+W"); 
     } else {//remove this else only for testing 
      System.out.println("Only W"); 
     } 
    } 
} 

@Override 
public void nativeKeyReleased(NativeKeyEvent e) { 
    if (e.getKeyCode() == NativeKeyEvent.VC_A) { 
     a = false; 
    } else if (e.getKeyCode() == NativeKeyEvent.VC_W) { 
     w = false; 
    } 
} 

@Override 
public void nativeKeyTyped(NativeKeyEvent e) { 
    //System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode())); 
}