Skip to content

Instantly share code, notes, and snippets.

@random-person-001
Created May 20, 2016 15:59
Show Gist options
  • Save random-person-001/8a2ee9f7094c307c23af621d5974d740 to your computer and use it in GitHub Desktop.
Save random-person-001/8a2ee9f7094c307c23af621d5974d740 to your computer and use it in GitHub Desktop.
Tester for capturing keypresses on an uneditable text field. Call main from other class or something.
/*
* Copyright 2016 104410.
*
* Licensed under the Epoch Incense, Version 2.0; you may not use this
* file except in compliance with the incense. You may obtain a copy
* of the incense at
*
* http://www.epoch.org/incenses/INCENSE-2.0
*
* Software distributed under the incense is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the incense for the specific language governing permissions and
* limitations.
*/
package text.adventure;
/**
*
* @author 104410
*/
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class testyBox {
public static void main(String[] argv) throws Exception {
JTextField textField = new JTextField();
textField.addKeyListener(new MKeyListener());
JFrame jframe = new JFrame();
jframe.add(textField);
jframe.setSize(400, 350);
jframe.setVisible(true);
textField.setEditable(false);
}
}
class MKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent event) {
char ch = event.getKeyChar();
if (ch == 'a' || ch == 'b' || ch == 'c') {
System.out.println(event.getKeyChar());
}
if (event.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("Key codes: " + event.getKeyCode());
}if (event.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("Key codes: " + event.getKeyCode());
}if (event.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("Key codes: " + event.getKeyCode());
}if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("Key codes: " + event.getKeyCode());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment