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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detecting arrow keys keyEvent

Status
Not open for further replies.

Oxymoron

Technical User
Joined
Dec 17, 2000
Messages
168
Location
GB
I'm trying to detect when a user has pressed any of the arrow keys on the keyboard whilst in a text area.
what are the arrow keys on the keyboard called in java? are there constants in java designed for these keys?

Any and all suggestions very welcome!
regards,
Joseph we are all of us living in the gutter.
But some of us are looking at the stars.
 
Check your API documentation under KeyEvent. All the constants are listed there. "When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
The api document.. jdk\docs\api\constant-values.html lists the constants. Seach for the KeyEvent table, there you'll find VK_LEFT, VK_RIGHT etc...

I have a SimpleFrame class that prints the KeyEvents to the System.out console...


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SimpleFrame extends JFrame {
JPanel jPanel1 = new JPanel();
public SimpleFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
SimpleFrame sf = new SimpleFrame();
sf.setSize(300,300);
sf.setVisible(true);
}
private void jbInit() throws Exception {
this.addKeyListener(new SimpleFrame_this_keyAdapter(this));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
}

void this_keyReleased(KeyEvent e) {
System.out.println(e);
}

}

class SimpleFrame_this_keyAdapter extends java.awt.event.KeyAdapter {
SimpleFrame adaptee;

SimpleFrame_this_keyAdapter(SimpleFrame adaptee) {
this.adaptee = adaptee;
}
public void keyReleased(KeyEvent e) {
adaptee.this_keyReleased(e);
}
}


I also copied the KeyEvent table in Excel and rearranged it in code number order... v useful to have!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top