View difference between Paste ID: u473t6Li and 23pTuDdS
SHOW: | | - or go back to the newest paste.
1
import random
2
from openexp.keyboard import keyboard
3
 
4
# Inter-stimulus interval
5
ITI = 20
6
 
7
# The number of cycles
8
n_cycles = 20
9
10
# Determines whether the canvas list is presented in random order
11
# or sequentially
12
random_order = False
13
 
14-
# Set-up a keyboard object with a timeout equal to the interval
14+
# Set-up a keyboard object with a very brief timeout so that we can
15-
# between the two stimuli
15+
# poll the keyboard continuously
16
my_keyboard = keyboard(exp, timeout=1)
17
 
18
# Copy a number of canvases from sketchpads and put them in a list
19
canvas_list = [
20
	self.copy_sketchpad('sketchpad_1'),
21
	self.copy_sketchpad('sketchpad_2'),
22
	self.copy_sketchpad('sketchpad_3')
23
	]
24
25
# Start the response interval
26
start_timestamp = self.time()
27
 
28
# We are going to keep a list of responses
29
resp_list = []
30
 
31
# Go into a loop where the two canvases are shown in alternation
32
for i in range(n_cycles):
33
34
	# Show the first canvas
35
	if random_order:
36
		my_canvas = random.choice(canvas_list)
37
	else:
38
		my_canvas = canvas_list[i % len(canvas_list)]
39
	
40
	# Show the canvas
41
	t1 = my_canvas.show()
42
43-
	# Use the get_key() function to sleep and monitor for keypresses
43+
	# Get in a loop that sleeps for the ITI period and polls for
44-
	# at the same time.
44+
	# keypresses at the same time.
45
	while self.time() - t1 < ITI:
46
		resp, t2 = my_keyboard.get_key()
47
		if resp != None:
48
			# If a key was pressed, add it to the list of responses.
49
			resp_list.append( (resp, t2) )
50
51
# If the response_list is empty, no response was given
52
if len(resp_list) == 0:
53
	response = 'timeout'
54
	response_time = 0
55
56
# Otherwise get the first response from the list
57
else:
58
	resp, resp_timestamp = resp_list[0]
59
	response = resp
60
	response_time = start_timestamp - resp_timestamp
61
	
62
# Set the response variables!
63
exp.set('response', resp)
64
exp.set('response_time', response_time)
65
 
66
# To maintain feedback variables, see
67
# <http://osdoc.cogsci.nl/usage/giving-feedback-to-participants/#inline-script>