You are on page 1of 3

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

*;

public class KeyEventTest extends JFrame implements KeyListener { private String baris1="", baris2="", baris3=""; private JTextArea textArea;

public KeyEventTest() { super ("Mencoba Key Event");

textArea = new JTextArea (10,15); textArea.setText("Tekan sembarang tombol di keyboard..."); textArea.setEnabled(false); textArea.setDisabledTextColor(Color.BLACK); getContentPane().add(textArea);

addKeyListener (this);

setSize (300,150);

setLocationRelativeTo(null); setVisible(true); }

public void keyPressed (KeyEvent e) { baris1 = "Tombol yang ditekan : " + e.getKeyText(e.getKeyCode()); setLines2and3 (e); }

public void keyReleased (KeyEvent e) { baris1 = "Tombol yang dilepas : " + e.getKeyText(e.getKeyCode()); setLines2and3 (e); }

public void keyTyped (KeyEvent e) { baris1 = "Tombol yang ditulis : " + e.getKeyChar(); setLines2and3 (e); }

private void setLines2and3 (KeyEvent e) { baris2 = "This key is "+ (e.isActionKey() ? "" : "not ") + "an action key"; String temp = e.getKeyModifiersText(e.getModifiers()); baris3 = "Modifier key pressed : " + (temp.equals("") ? "none" : temp); textArea.setText(baris1 + "\n" + baris2 + "\n" + baris3 + "\n"); }

public static void main (String args[]) { KeyEventTest test = new KeyEventTest(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

You might also like