Get key combinations
Asked Answered
D

4

5

How can I get key combination of keys on keyboard E.G. (Ctrl+somekey, Alt+somekey) with Java?

I use KeyEvent listener, MouseEvent listener for all keys on keyboard. I can catch all key event on keyboard by using that listener. But, I cannot catch key combination such as (Ctrl+Alt+Del)....etc.

Disenfranchise answered 13/1, 2013 at 8:14 Comment(2)
Maybe this #7852005 answers some of your questions? BTW special OS key combinations like Ctrl + Alt + Del on windows cannot be captured by a user application because the OS captures them before the application gets the chance. That's why VNC clients and such programs often have a menu option to send Ctrl+Alt+Del to the host.Jillian
#753499 check this answer tooHalfway
D
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);
}
Disenfranchise answered 16/1, 2013 at 11:16 Comment(0)
S
10
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
}

Use the above code, use any character If you want to check if Alt+C+E is pressed do the following

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
       }
     }

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

     public void keyTyped(KeyEvent kvt) {
     }
 }

}
Schnauzer answered 13/1, 2013 at 8:30 Comment(8)
Thanks you for your answer govindo. What I want is overall key combinations means (Ctrl+All keys) (Alt+All keys) . It is not OK for me to check every keys like that.Disenfranchise
@Juno whats your need for capturing all ? your question is answered here.Halfway
@Halfway Yes, I want to capture images all key events and all key combination events images. So, I need to know overall key combinations eventsDisenfranchise
@Juno then you have to check all the cases.Halfway
@Juno what do you want know? I don't really understand.Schnauzer
@govindo Thanks you for your patient. I want to monitor the server that is remote by other client or server. Client or server remote desktop by my server and use my server. I want to capture all actions. So I need to capture all events. If user click alt. I capture image for alt. If user click Ctrl+C, I capture images for Ctrl+C event. So I cannot check every keys that you told me before. Your answer is right for check alittle key event. But it cannot be work for me. Thanks you.Disenfranchise
@Halfway I havn't all yet. If I can catch key combinations, I cannot capture all key events. If I can catch all key events, I cannot capture key combinations. I want to capture all key and key combinations events. :(Disenfranchise
I can capture all. Whatever thanks you very much @govindo and vels4jDisenfranchise
D
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);
}
Disenfranchise answered 16/1, 2013 at 11:16 Comment(0)
C
1

Many of these answers seem very complicated, just thought I'd add my solution.

I wrote a KeyBinder class:

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

And then on my control:

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

startButton.addKeyListener(binder);

Easy :)

Collude answered 31/1, 2015 at 9:11 Comment(0)
F
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());
    }

Simple version

Finical answered 23/2, 2016 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.