• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Key Events / Which keys are which?

 
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been trying to figure out how to "capture" specific keys that are typed by a user for my application. For example, if a user hits the "Enter" key, have the app do somthing.
I figured out how to do this thanks to an api example. If you have the api, it is under the KeyListener api, in the See Also section, Tutorial: Writing a Key Listener
Is there a "master list" of standard keyboard codes that are given by e.getKeyChar(), and e.getKeyCode() below?:
<pre>
public void keyPressed(KeyEvent e)
{
char keyChar = e.getKeyChar() ;
int keyCode = e.getKeyCode() ;
</pre>
For example, the "Enter" key returns a keyCode of 10 when pressed.
The code located at the above url (in case you don't have it) is below. It's an applet found at and returns the codes for what you type in a text field: http://java.sun.com/docs/books/tutorial/uiswing/events/KeyEventDemo.html
<pre>
/*
* Swing version
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
public class KeyEventDemo extends JApplet
implements KeyListener,
ActionListener {
JTextArea displayArea;
JTextField typingArea;
static final String newline = "\n";
public void init() {
JButton button = new JButton("Clear");
button.addActionListener(this);
typingArea = new JTextField(20);
typingArea.addKeyListener(this);
displayArea = new JTextArea();
displayArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(displayArea);
scrollPane.setPreferredSize(new Dimension(375, 125));
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(typingArea, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
setContentPane(contentPane);
}
/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}
/** Handle the button click. */
public void actionPerformed(ActionEvent e) {
//Clear the text components.
displayArea.setText("");
typingArea.setText("");
//Return the focus to the typing area.
typingArea.requestFocus();
}
/*
* We have to jump through some hoops to avoid
* trying to print non-printing characters
* such as Shift. (Not only do they not print,
* but if you put them in a String, the characters
* afterward won't show up in the text area.)
*/
protected void displayInfo(KeyEvent e, String s){
String charString, keyCodeString, modString, tmpString;
char c = e.getKeyChar();
int keyCode = e.getKeyCode();
int modifiers = e.getModifiers();
if (Character.isISOControl(c)) {
charString = "key character = "
+ "(an unprintable control character)";
} else {
charString = "key character = '"
+ c + "'";
}
keyCodeString = "key code = " + keyCode
+ " ("
+ KeyEvent.getKeyText(keyCode)
+ ")";
modString = "modifiers = " + modifiers;
tmpString = KeyEvent.getKeyModifiersText(modifiers);
if (tmpString.length() > 0) {
modString += " (" + tmpString + ")";
} else {
modString += " (no modifiers)";
}
displayArea.append(s + newline
+ " " + charString + newline
+ " " + keyCodeString + newline
+ " " + modString + newline);
}
}
</pre>

[This message has been edited by Matt Midcap (edited October 05, 1999).]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look to CORE API documentation: api/java.awt.event.KeyEvent.html. There is a static final variable for each keycode named VK_xxx.
This contains the information about any possible keys you can get from a user.
Btw, one should not base his/her knowledge of keycodes, thay he/she found by testing the application or applet on some platform (for example with the application Matt pointed to) Sometimes values for that VK_* variables can be different even for alphanumeric characters.

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic