2013-01-13 85 views
4

如何獲得鍵盤上的按鍵組合鍵E.G. (按Ctrl +somekeyAlt鍵 + somekey)與Java?獲取組合鍵

我使用KeyEvent聽衆,MouseEvent監聽鍵盤上的所有按鍵。我可以通過使用該偵聽器來捕獲鍵盤上的所有關鍵事件。但是,我不能抓住組合鍵,如(Ctrl + Alt + Del)....等等。

+1

也許此http:/ /stackoverflow.com/questions/7851505/how-can-a-keylistener- detect-key-combinations-e-g-alt-1-1回答你的一些問題?順便提一下,用戶應用程序不能捕獲窗口上的Ctrl + Alt + Del等特殊操作系統組合鍵,因爲操作系統在應用程序有機會之前捕獲它們。這就是爲什麼VNC客戶端和這樣的程序通常有一個菜單選項來向主機發送Ctrl + Alt + Del。 – Eelke

+1

http://stackoverflow.com/questions/752999/how-do-i-handle-multiple-key-press-in-java檢查這個答案太 – vels4j

回答

1
@Override 
public void keyPressed(KeyEvent evt) { 
     if (evt.getKeyCode()==KeyEvent.VK_CONTROL) { ctrl = true; } 
    else if (evt.getKeyCode()==KeyEvent.VK_SHIFT) { shift = true; } 
    else if (evt.getKeyCode()==KeyEvent.VK_ALT)  { alt = true; } 
    else { 
     keyHit = KeyEvent.getKeyText(evt.getKeyCode()); 
     System.out.println("Key Hit is "+keyHit); 
    } 

    processLocalKeyEvent(evt); 
} 

@Override 
public void keyReleased(KeyEvent evt) { 

    if (evt.isControlDown() && keyHit != "") ctrl = true; 
    if (evt.isAltDown()  && keyHit != "") alt = true; 
    if (evt.isShiftDown() && keyHit != "") shift = true; 

    if (ctrl) sb.append("Ctrl"); 
    if (shift) sb.append("Shift"); 
    if (alt) sb.append("Alt"); 
    if (!ctrl && !shift && !alt) { 
     sb.append(keyHit); 
    } else { 
     sb.append("_"+keyHit); 
    } 

    if (ctrl || shift || alt) { 
     Thread thread = new Thread(); 
     try { 
      thread.sleep(300); 
      rfbProto.capture(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } else if ((ctrl || shift || alt) && keyHit=="") { 
     rfbProto.capture(); 
    } else if ((!ctrl || !shift || !alt) && keyHit!="") { 
     rfbProto.capture(); 
    } 

    ctrl = false; 
    shift = false; 
    alt = false; 
    keyHit = ""; 
    sb = new StringBuffer(); 
    processLocalKeyEvent(evt); 
} 
9
public void keyPressed(KeyEvent kevt) { 
if(kevt.getKeyChar()=='c') { 
    if(kevt.isAltDown()) 
    //Code if Alt+c pressed 
    if(kevt.isControlDown()) 
    //Code if Ctrl+c pressed 
    if(kevt.isShiftDown()) 
    //Code if Shift+c pressed 
    if(kevt.isAltDown()&&kevt.isControlDown()&&(!kevt.isShiftDown())) 
    //Code if Alt+Ctrl+c pressed 
    if(kevt.isAltDown()&&kevt.isShiftDown()&&(!kevt.isControlDown())) 
    //Code if Alt+Shift+c pressed 
    if(!(kevt.isAltDown())&&kevt.isControlDown()&&(kevt.isShiftDown())) 
    //Code if Shift+Ctrl+c pressed 
    if(kevt.isAltDown()&&kevt.isControlDown()&&kevt.isShiftDown()) 
    //Code if Alt+Ctrl+Shift+c pressed 
} 

使用上面的代碼,如果你想檢查是否已按下Alt + C + E使用任何字符 請執行下列操作

import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.swing.*; 

public class Sample implements KeyListener { 
    private JTextField lbl=new JLabel("Hello"); 
    private JPanel pnl=new JPanel(new BorderLayout()); 
    private JFrame frm=new JFrame ("Sample"); 
    int []arr;int i=0; 

    public Sample() { 
    pnl.add("North", lbl); 
    frm.setContentPane(pnl); 
    frm.pack(); 
    frm.setVisible(true); 
    lbl.addKeyListener(this); 
    arr= new int[3]; 

    public void keyPressed(KeyEvent key) { 
     arr[i]=key.getKeyCode(); 
     i++; 
     if((arr[0]==VK_ALT||arr[1]==VK_ALT||arr[2]==VK_ALT)&& (arr[0]==VK_C||arr[1]==VK_C||arr[2]==VK_C)&&(arr[0]==VK_E||arr[1]==VK_E||arr[2]==VK_E)) { 
     //Code you want 
     } 
    } 

    pubic void keyReleased(KeyEvent evt) { 
     arr[i]=null; 
    } 

    public void keyTyped(KeyEvent kvt) { 
    } 
} 

} 
+0

感謝您的回答govindo。我想要的是全部組合鍵(Ctrl +所有鍵)(Alt +所有鍵)。對我來說,檢查每一個這樣的鍵是不行的。 – Anonymous

+0

@Juno你需要什麼來捕捉所有?你的問題在這裏得到解答。 – vels4j

+0

@ vels4j是的,我想要捕捉所有關鍵事件和所有組合事件圖像的圖像。所以,我需要知道整體組合事件 – Anonymous

1

許多這些問題的答案似乎很複雜,只是想我會添加我的解決方案。

我寫了一個KeyBinder類:

import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.util.Hashtable; 

public abstract class KeyBinder implements KeyListener 
{ 
    private Hashtable<Integer, Boolean> keyMap; 

    private int[] keyCodes; 

    public KeyBinder(final int... keyCodes) 
    { 
     this.keyMap = new Hashtable<>(); 
     this.keyCodes = keyCodes; 
    } 

    @Override 
    public void keyTyped(final KeyEvent e) { } 

    @Override 
    public void keyPressed(final KeyEvent e) 
    { 
     getKeyMap().put(e.getKeyCode(), true); 

     if (getKeysDown()) 
     { 
      onKeysDown(); 
     } 
    } 

    @Override 
    public void keyReleased(final KeyEvent e) 
    { 
     getKeyMap().put(e.getKeyCode(), false); 
    } 

    private Hashtable<Integer, Boolean> getKeyMap() 
    { 
     return this.keyMap; 
    } 

    public boolean getKeysDown() 
    { 
     for (final int key : this.keyCodes) 
     { 
      if (getKeyMap().containsKey(key)) 
      { 
       if (!getKeyMap().get(key)) 
       { 
        return false; 
       } 
      } else { 
       return false; 
      } 
     } 

     return true; 
    } 

    public abstract void onKeysDown(); 
} 

,然後在我的控制:

final KeyBinder binder = new KeyBinder(KeyEvent.VK_ALT, KeyEvent.VK_A) 
{ 
    @Override 
    public void onKeysDown() 
    { 
     System.out.println("Alt+A"); 
    } 
}; 

startButton.addKeyListener(binder); 

容易:)

0
private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {          
    System.out.println(evt.getKeyCode()); //showing code of released button 

    if(evt.isControlDown() && evt.getKeyCode()==40) // 40 is code for arrow down 
    { 
     //if ctrl is pressed and arrow down is released 
     System.out.println("Released " + evt.getKeyCode()); 
    } 

簡易型