/** JavaKeys version 2, prints information about keystrokes. Copyright (C) 2003-2006 Doyle B. Myers This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA As a special exception, Doyle B. Myers gives permission to link this program with The Java platform and distribute the resulting executable, without including the source code for The Java platform in the source distribution. The phrase "The Java platform" specifically means the contents of all java.* packages as provided by Sun for either Commercial Use or under the Sun Community Source Code License. Changes: Version 2: Use Java 1.1 compatible GridBagConstraints constructor, fixed typos. */ package javakeys; import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class JavaKeys extends Applet { private boolean isStandalone = false; private Label label1 = new Label(); private TextField mTextTarget = new TextField(); private TextArea mLogArea = new TextArea(); private Button mClearLogButton = new Button(); private Checkbox mCheckShowPressed = new Checkbox(); private Checkbox mCheckShowTyped = new Checkbox(); private Checkbox mCheckShowReleased = new Checkbox(); private Label label2 = new Label(); private Button mAddMarkButton = new Button(); private GridBagLayout gridBagLayout1 = new GridBagLayout(); /** * Get a parameter value. * * @param key Parameter name. * @param def Default value. * @return Value of parameter. May be null. */ public String getParameter( String key, String def ) { return isStandalone ? System.getProperty( key, def ) : ( getParameter( key ) != null ? getParameter( key ) : def ); } /** * Initialize the applet. */ public void init() { try { jbInit(); } catch( Exception e ) { e.printStackTrace(); } } /** * Component initialization. * * @throws Exception */ private void jbInit() throws Exception { label1.setBackground( Color.white ); label1.setFont( new java.awt.Font( "Dialog", 0, 12 ) ); label1.setText( "Type here:" ); setLayout( gridBagLayout1 ); mTextTarget.addKeyListener( new java.awt.event.KeyAdapter() { public void keyTyped( KeyEvent e ) { mTextTarget_keyTyped( e ); } public void keyPressed( KeyEvent e ) { mTextTarget_keyPressed( e ); } public void keyReleased( KeyEvent e ) { mTextTarget_keyReleased( e ); } } ); mLogArea.setEditable( false ); mClearLogButton.setFont( new java.awt.Font( "Dialog", 0, 12 ) ); mClearLogButton.setLabel( "Clear Log" ); mClearLogButton.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed( ActionEvent e ) { button1_actionPerformed( e ); } } ); mCheckShowPressed.setFont( new java.awt.Font( "Dialog", 0, 12 ) ); mCheckShowPressed.setLabel( "Pressed" ); mCheckShowPressed.setState( true ); mCheckShowTyped.setFont( new java.awt.Font( "Dialog", 0, 12 ) ); mCheckShowTyped.setLabel( "Typed" ); mCheckShowTyped.setState( true ); mCheckShowReleased.setFont( new java.awt.Font( "Dialog", 0, 12 ) ); mCheckShowReleased.setLabel( "Released" ); mCheckShowReleased.setState( true ); label2.setBackground( Color.white ); label2.setFont( new java.awt.Font( "Dialog", 0, 12 ) ); label2.setText( "Log:" ); mTextTarget.setBackground( Color.white ); mTextTarget.setFont( new java.awt.Font( "DialogInput", 0, 12 ) ); mAddMarkButton.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed( ActionEvent e ) { mAddMarkButton_actionPerformed( e ); } } ); mAddMarkButton.setLabel( "Mark Log" ); mAddMarkButton.setFont( new java.awt.Font( "Dialog", 0, 12 ) ); setBackground( Color.white ); setFont( new java.awt.Font( "Dialog", 0, 12 ) ); add( mLogArea, makeGridBagConstraints( 0, 2, 7, 1, 1.0, 1.0 , GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 15, 12, 15, 12 ), 592, 339 ) ); add( label1, makeGridBagConstraints( 0, 0, 2, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 12, 12, 0, 0 ), 6, 11 ) ); add( mTextTarget, makeGridBagConstraints( 2, 0, 5, 1, 1.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 12, 6, 0, 12 ), 531, 9 ) ); add( label2, makeGridBagConstraints( 0, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 17, 12, 0, 0 ), 8, 5 ) ); add( mCheckShowPressed, makeGridBagConstraints( 1, 1, 2, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 17, 7, 0, 0 ), 11, 2 ) ); add( mCheckShowTyped, makeGridBagConstraints( 3, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 17, 10, 0, 0 ), 10, 2 ) ); add( mCheckShowReleased, makeGridBagConstraints( 4, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 17, 9, 0, 0 ), 13, 2 ) ); add( mAddMarkButton, makeGridBagConstraints( 5, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 15, 104, 0, 0 ), 7, -2 ) ); add( mClearLogButton, makeGridBagConstraints( 6, 1, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 15, 10, 0, 12 ), 7, -2 ) ); } /** * Emulate the 1.2 GridBagConstraints constructor. * * @param gridx Same as GridBagConstraints constructor. * @param gridy Same as GridBagConstraints constructor. * @param gridwidth Same as GridBagConstraints constructor. * @param gridheight Same as GridBagConstraints constructor. * @param weightx Same as GridBagConstraints constructor. * @param weighty Same as GridBagConstraints constructor. * @param anchor Same as GridBagConstraints constructor. * @param fill Same as GridBagConstraints constructor. * @param insets Same as GridBagConstraints constructor. * @param ipadx Same as GridBagConstraints constructor. * @param ipady Same as GridBagConstraints constructor. * @return GridBagConstraints initialized as if the 1.2 GridBagConstraints constructor had been called. */ private static GridBagConstraints makeGridBagConstraints( int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady ) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = gridx; gbc.gridy = gridy; gbc.gridwidth = gridwidth; gbc.gridheight = gridheight; gbc.fill = fill; gbc.ipadx = ipadx; gbc.ipady = ipady; gbc.insets = insets; gbc.anchor = anchor; gbc.weightx = weightx; gbc.weighty = weighty; return gbc; } /** * Start the applet. */ public void start() { mTextTarget.requestFocus(); } /** * Stop the applet. */ public void stop() { } /** * Destroy the applet. */ public void destroy() { } /** * Get Applet information. * * @return Applet information. */ public String getAppletInfo() { return "JavaKeys version 2."; } /** * Get applet parameter info. * * @return Null. We don't need no stinkin' parameters. */ public String[][] getParameterInfo() { return null; } /** * Key listener. Reports key pressed events. * * @param e Key event from VM. */ void mTextTarget_keyPressed( KeyEvent e ) { if( mCheckShowPressed.getState() ) { mLogArea.append( e.toString() + "\n" ); } } /** * Key listener. Reports key typed events. * * @param e Key event from VM. */ void mTextTarget_keyTyped( KeyEvent e ) { if( mCheckShowTyped.getState() ) { mLogArea.append( e.toString() + "\n" ); } } /** * Key listener. Reports key released events. * * @param e Key event from VM. */ void mTextTarget_keyReleased( KeyEvent e ) { if( mCheckShowReleased.getState() ) { mLogArea.append( e.toString() + "\n" ); } } /** * Button listener. Listens to Clear Log button. * * @param e Button event from VM. */ void button1_actionPerformed( ActionEvent e ) { mLogArea.setText( "" ); } /** * Button listener. Listens to Mark Log button. * * @param e Button event from VM. */ void mAddMarkButton_actionPerformed( ActionEvent e ) { mLogArea.append( "------------\n" ); } }