Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyEvents - how to register a Ctrl+Shift+A key combination?

Status
Not open for further replies.

Marinova

Programmer
May 4, 2003
4
DE
Hello everyone,

a program I am writing needs to recognize keystrokes like CTRL pressed + Shift pressed + A typed.
For this purpose i implemented a java.awt.event.KeyListener, but have problems to define the above-mentioned key situation, because the methods keyPressed() and KeyTyped() don't seem to interact with each other.

Can you give me some helpful advice about listening to more complex key events like a combination of keys?

Thanks a lot,

Maria
 
When you press a key combination like Ctrl-Shift-A, you get a series of KeyPressed events followed by a KeyTyped event.

You should watch for both events. Here's a small frame that will capture key combinations:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Frame1 extends JFrame 
{
   protected JTextField jTextField1 = new JTextField();

   public Frame1()
   {
      this.getContentPane().setLayout(null);
      this.setSize(new Dimension(400, 300));
      jTextField1.setText("jTextField1");
      jTextField1.setBounds(new Rectangle(39, 39, 264, 36));
      jTextField1.addKeyListener(new java.awt.event.KeyAdapter()
         {
            public void keyPressed(KeyEvent e)
            {
               jTextField1_keyPressed(e);
            }

            public void keyTyped(KeyEvent e)
            {
               jTextField1_keyTyped(e);
            }
         });
      this.getContentPane().add(jTextField1, null);
   }
   
   boolean ctrl = false;
   boolean shift = false;
   boolean alt = false;
   String keyHit = "";
   private void jTextField1_keyPressed(KeyEvent e)
   {
      if (e.getKeyCode()==KeyEvent.VK_CONTROL)  ctrl = true;
      else if (e.getKeyCode()==KeyEvent.VK_SHIFT)  shift = true;
      else if (e.getKeyCode()==KeyEvent.VK_ALT)  alt = true;      
      else keyHit = KeyEvent.getKeyText( e.getKeyCode() );
   }

   private void jTextField1_keyTyped(KeyEvent e)
   {
      StringBuffer sb = new StringBuffer("Key was "); 
      if (ctrl)  sb.append("ctrl-");
      if (shift)  sb.append("shift-");   
      if (alt)  sb.append("alt-");                
      sb.append( keyHit );
      System.out.println(sb.toString());
      ctrl = false;
      shift = false;
      alt = false;
      keyHit = "";
   }
}

This sets the flags in KeyPressed and clears them in KeyTyped. You'll get multiple KeyPressed events fired (holding down ctrl while hunting for the other keys will fire multiple events all by itself), followed by a final KeyTyped event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top