All pastes #254271 Raw Edit

Anonymous

public java v1 · immutable
#254271 ·published 2006-11-21 17:54 UTC
rendered paste body
//Keystoke Detector 689//Coreimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class KeyDemo extends JFrame implements KeyListener {	private String line1 = "", line2 = "";	private String line3 = "";	private JTextArea textArea;		//gui	public KeyDemo()	{		super( "This is a test of keystoke events. Poop" );				//TextArea		textArea = new JTextArea( 10, 15 );		textArea.setText( "I dare you to press a key." );		textArea.setEnabled( false );		getContentPane().add( textArea );				//Frame process's key.		addKeyListener( this );				setSize( 350, 100 );		setVisible( true );	}		//handles key press.	public void keyPressed( KeyEvent event )	{		line1 = "What you pressed: " +			event.getKeyText( event.getKeyCode() );		setLined2and3( event );	}		//handles release of key.	public void keyReleased( KeyEvent event )	{		line1 = "Key released: " +			event.getKeyText( event.getKeyCode() );		setLined2and3( event );	}		//handles press of action key.	public void keyTyped( KeyEvent event )	{		line1 = "Key Typed: " + event.getKeyChar();		setLines2and3( event );	}		//Second and third output.	private void setLines2and3( KeyEvent event)	{		line2 = "This key is " +			( event.isActionKey() ? "" : "not " ) +			"an action key";						String temp =				event.getKeyModifiersText( event.getModifiers() );							line3 = "Modifier key pressed: " +				( temp.equals( "" ) ? "none" : temp );							textArea.setText(				line1 + "\n" + line2 + "\n" + line3 + "\n" );	}		//execute program	public static void main( String args[] )	{		KeyDemo application = new KeyDemo();				application.setDefaultCloseOperation(			JFrame.EXIT_ON_CLOSE );	}}