Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting the Tilde keyEvent

611651Jul 20 2008 — edited Jul 20 2008
I'm trying to perform an action when the 'tilde key' is pressed on my keyboard it looks like �� on top of each other.

I first tried KeyEvent.VK_DEAD_TILDE but it is not generated.

When I run this demo on this page:
http://www.dil.univ-mrs.fr/~garreta/docJava/tutorial/uiswing/events/keylistener.html
http://java.sun.com/docs/books/tutorialJWS/uiswing/events/examples/KeyEventDemo.jnlp

then I get:
KEY PRESSED: 
    key code = 0 (Unknown keyCode: 0x0)
    extended modifiers = 0 (no extended modifiers)
    action key? NO
    key location: standard
KEY TYPED: 
    key character = '�'
    extended modifiers = 0 (no extended modifiers)
    action key? NO
    key location: unknown
KEY RELEASED: 
    key code = 0 (Unknown keyCode: 0x0)
    extended modifiers = 0 (no extended modifiers)
    action key? NO
    key location: standard
My test code:
public class Test implements KeyListener {

  public Test() {
    JFrame frame = new JFrame();
    frame.setSize(150, 200);
    frame.setVisible(true);
    frame.addKeyListener(this);
  }

  public void keyTyped(KeyEvent e) {
    System.out.println(e.getKeyCode());
    if (e.getKeyCode() == KeyEvent.VK_DEAD_TILDE) {
      System.out.println("hooray");
    } else {
      System.out.println("nope");
    }
  }

  public void keyPressed(KeyEvent e) {
    System.out.println(e.getKeyCode());
    if (e.getKeyCode() == KeyEvent.VK_DEAD_TILDE) {
      System.out.println("hooray");
    } else {
      System.out.println("nope");
    }
  }

  public void keyReleased(KeyEvent e) {
  }

  public static void main(String[] args) {
    new Test();
  }
}
How can I capture the tilde key being pressed?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2008
Added on Jul 20 2008
3 comments
1,008 views