// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class KeyFun extends JFrame implements KeyListener {
    private int count;
    public KeyFun() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        count = 0;
        addKeyListener(this);
        setSize(300, 300);
        setTitle("Only every third keystroke works");
    }
    public static void main(String[] args) {
        JFrame f = new KeyFun();
        f.setVisible(true);
    }
    public void keyTyped(KeyEvent e) {
    }
    public void keyPressed(KeyEvent e) {
        if(active()) {
            JOptionPane.showMessageDialog(this, "You pressed " + (char)e.getKeyCode());
        }
    }
    private boolean active() {
        count++;
        return count % 3 == 0;
    }
    public void keyReleased(KeyEvent e) {
    }
}
