/* * KeyPressTest.java * * Copyright (C) 2005-2012 Huseyin Boyaci. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * 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 version 2 for more details * (a copy is included in the LICENSE file that accompanied this code) * (also available at http://www.gnu.org) 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. * */ import java.awt.Font; import psychWithJava.FullScreen; /** * * Test the observer response through keyboard * * @author Huseyin Boyaci * @since 2013-03-18 * */ public class KeyPressTest extends FullScreen implements Runnable { /** countdown rate (milliseconds) */ final static long ISI_TIME = 500l; /** timeout duration (milliseconds)*/ final static long TIMEOUT = 1000l; public static void main(String[] args) { KeyPressTest kpt = new KeyPressTest(); new Thread(kpt).start(); } public void run(){ try { setFont(new Font("SansSerif", Font.BOLD, 26)); blankScreen(); displayText(100, 100, "Reaction time test"); displayText(100, 150, "In this test you will see a countdown"); displayText(100, 200, "Press a key as soon as the countdown reaches 0"); displayText(100, 250, "The program will report your reaction time"); displayText(100, 300, "Press q to finish the test and quit the program; Press ESC to quit the program"); displayText(100, 350, "Now, press any key to start"); updateScreen(); flushKeyPressed(); getKeyPressed(-1); boolean running = true; while (running) { int count = 4; long start = 0L; while (count>=0) { // pause for a while... Thread.sleep(ISI_TIME); blankScreen(); displayText(300, 400, "counter: " + String.valueOf(count--)); updateScreen(); } start = System.currentTimeMillis(); flushKeyTyped(); // wait for the specified amount of time - TIMEOUT - for // the observer to type a character. KeyTyped kt = getKeyTyped(TIMEOUT); if(kt != null){ String response = kt.getKey(); blankScreen(); displayText(300, 600, " You pressed: " + response + " Reaction time: " + (kt.getWhen() - start) + " ms"); if (response.equals("q")) running = false; } else displayText(300,600, "Time out! You are too slow!"); updateScreen(); Thread.sleep(2000); } blankScreen(); displayText("Thank you!"); updateScreen(); Thread.sleep(2000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { closeScreen(); } } }