vendredi 31 août 2018

Timing user input keys

this is my first attempt at making a program. I want to time how long it takes for the user to type what is displayed on the screen, however, it is only timing once every two tries.

For example X might be displayed they type X and it doesn't time. When e.g. Y appears next and they type Y it will time it, and it just goes round in circles like that.

This is what i have so far sorry if it's bad.

answer.addKeyListener(new KeyAdapter() {
        long entryStart;
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
            {
                countTime++;
                if (countTime % 2 != 0) {
                    entryStart = System.currentTimeMillis();
                }
                if (countTime % 2 == 0)
                {
                    long entryEnd = System.currentTimeMillis();
                    long differenceEntry = (entryEnd - entryStart);
                    timeField.setText(String.valueOf(differenceEntry / 1000.0));
                }
                if (answer.getText().isEmpty()){
                    JOptionPane.showMessageDialog(answer, "Error!");        
                }
                if (answer.getText().equals(matchingField.getText())){
                    points++;
                    pointsField.setText(String.valueOf(points));
                }
                else {
                    incorrectpoints++;
                    incorrectField.setText(String.valueOf(incorrectpoints));
                    points--;
                    pointsField.setText(String.valueOf(points));
                }
                matchingField.setText("");
                answerField.setText("");
                StringGenerator sg = new StringGenerator(); 
                matchingField.setText(sg.randomString());
            }
        }

Also if you could provide any details on how I could improve my code that would also be appreciated. Thank you.

Aucun commentaire:

Enregistrer un commentaire