
package my3;
/**
 *
 * @author Viliam Krizan
 */



import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
//simulacia zataze
public class KeyboardTyping extends JFrame implements KeyListener, ActionListener {

    
    JTextArea displayArea;
    JTextField typingArea;
    static final String newline = System.getProperty("line.separator");
    Date date;
     
    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
        
        Runnable job1 = new AnotherThread(1);
        Runnable job2 = new AnotherThread(2);
        Runnable job3 = new AnotherThread(3);
        Runnable job4 = new AnotherThread(4);
        Runnable job5 = new AnotherThread(5);
        Runnable job6 = new AnotherThread(6);
        Runnable job7 = new AnotherThread(7);
        
        Thread thread1 = new Thread(job1);
        Thread thread2 = new Thread(job2);
        Thread thread3 = new Thread(job3);
        Thread thread4 = new Thread(job4);
        Thread thread5 = new Thread(job5);
        Thread thread6 = new Thread(job6);
        Thread thread7 = new Thread(job7);
        
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        thread6.start();
        thread7.start();
    }
     
    private static void createAndShowGUI() {
        //Create and set up the window.
        KeyboardTyping frame = new KeyboardTyping("KeyEvent");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
        //Set up the content pane.
        frame.addComponentsToPane();
         
         
        //Display the window.
        //frame.pack();
        frame.setSize(500, 300);
        frame.setVisible(true);
    }
     
    private void addComponentsToPane() {
         
        JButton button = new JButton("Clear");
        button.addActionListener(this);
         
        typingArea = new JTextField(20);
        typingArea.addKeyListener(this);
         
        typingArea.setFocusTraversalKeysEnabled(false);
         
        displayArea = new JTextArea();
        displayArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(displayArea);
        scrollPane.setPreferredSize(new Dimension(375, 125));
         
        getContentPane().add(typingArea, BorderLayout.PAGE_START);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        getContentPane().add(button, BorderLayout.PAGE_END);
    }
     
    public KeyboardTyping(String name) {
        super(name);
    }
     
     
    
    public void keyTyped(KeyEvent e) {
    }
     
    
    public void keyPressed(KeyEvent e) {
        displayInfo(e);
    }
     
    
    public void keyReleased(KeyEvent e) {
    }
     
    /** Handle the button click. */
    public void actionPerformed(ActionEvent e) {
        //Clear the text components.
        displayArea.setText("");
        typingArea.setText("");
         
        //Return the focus to the typing area.
        typingArea.requestFocusInWindow();
    }
    
    private void displayInfo(KeyEvent e){
        
        String keyString;
        
        int keyCode = e.getKeyCode();
            keyString = "code = " + keyCode
                    + " ("
                    + KeyEvent.getKeyText(keyCode)
                    + ")";
         
        date = new Date();
        int milis = (int) (date.getTime() % 1000l);
         
        displayArea.append("KEY PRESSED:" 
                + "   " + keyString 
                + "   " + "ms: " + milis + newline);
        displayArea.setCaretPosition(displayArea.getDocument().getLength());
        
        KeyBoardData.addData(milis);
    }
    
}
