Advertisement
smathot

Mouse and keyboard OpenSesame

Jan 21st, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. # Generated by OpenSesame 0.27 (Frisky Freud)
  2. # Mon Jan 21 12:00:42 2013 (posix)
  3. # <http://www.cogsci.nl/opensesame>
  4.  
  5. set foreground "white"
  6. set subject_parity "even"
  7. set description "A template containing a practice and an experimental phase"
  8. set title "Extended template"
  9. set compensation "0"
  10. set coordinates "relative"
  11. set height "768"
  12. set mouse_backend "xpyriment"
  13. set width "1024"
  14. set sampler_backend "legacy"
  15. set keyboard_backend "legacy"
  16. set background "black"
  17. set subject_nr "0"
  18. set canvas_backend "xpyriment"
  19. set start "experiment"
  20. set synth_backend "legacy"
  21.  
  22. define loop patch_loop
  23. set repeat "1"
  24. set description "A single block of trials"
  25. set skip "0"
  26. set offset "no"
  27. set item "stream_of_patches"
  28. set column_order ""
  29. set cycles "1"
  30. set order "random"
  31. run stream_of_patches
  32.  
  33. define sequence vigilance_task
  34. set flush_keyboard "yes"
  35. set description "A sequence containing a single block of trials followed by feedback to the participant"
  36. run patch_loop "always"
  37.  
  38. define form_multiple_choice mc
  39. set allow_multiple "yes"
  40. set description "A simple multiple choice item"
  41. set question "Do you like bunnies?"
  42. set button_text "Ok"
  43. set advance_immediately "yes"
  44. set form_title "Form title"
  45. set form_var "response"
  46. __options__
  47. Yes
  48. No
  49. Maybe
  50. __end__
  51.  
  52. define sequence stream_of_patches
  53. set flush_keyboard "yes"
  54. set description "A single trial"
  55. run mc "never"
  56. run inline_script "always"
  57.  
  58. define loop experimental_loop
  59. set repeat "1"
  60. set description "A loop containing one or more experimental blocks"
  61. set item "vigilance_task"
  62. set column_order "practice"
  63. set cycles "1"
  64. set order "random"
  65. setcycle 0 practice "no"
  66. run vigilance_task
  67.  
  68. define sequence experiment
  69. set flush_keyboard "yes"
  70. set description "The main sequence of the experiment"
  71. run constants "always"
  72. run experimental_loop "always"
  73.  
  74. define inline_script constants
  75. set _run ""
  76. ___prepare__
  77. # Here we'll define the visual stimulus list and the
  78. # auditory stimulus list.
  79.  
  80.  
  81. global patch_list
  82. patch_list = ["horizontal.png"] * 100 + ["vertical.png"] * 20
  83.  
  84. global sound_list
  85. sound_list = ["sound1.wav", "sound2.wav"]
  86.  
  87. global wait, ISI
  88. # Wait between blank screen and patch (to make it
  89. # flickering):
  90. wait = 100
  91.  
  92. # Inter stimulus interval:
  93. ISI = 1000
  94. __end__
  95. set description "Executes Python code"
  96.  
  97. define inline_script inline_script
  98. ___run__
  99. import pygame
  100. from openexp.exceptions import response_error
  101.  
  102. # Start by playing the sound file:
  103. my_sampler.play()
  104. sampler_paused = False
  105.  
  106. for i in range(len(patch_list)):
  107.  
  108. if sampler_paused:
  109. my_sampler.resume()
  110. # Determine the stimulus by selecting one item from the
  111. # stimulus list (without replacement).
  112. stim = patch_list.pop()
  113. # Set the stimulus for future use in the GUI (notably, the logger
  114. # item):
  115. exp.set("stim", stim)
  116. # Determine the absolute path to the image in
  117. # the file pool:
  118. path = exp.get_file(stim)
  119.  
  120. # Show the stimulus:
  121.  
  122. # Start with a gray background for a certain duration:
  123. my_canvas.show()
  124. self.sleep(1000)
  125. # Present the patch
  126. my_canvas.image(path)
  127. my_canvas.show()
  128.  
  129. start_time = self.time()
  130. time_passed = 0
  131.  
  132. while time_passed < ISI:
  133.  
  134. # Keep checking whether the duration of this
  135. # while loop is still below the maximum duration
  136. # (i.e. ISI).
  137. time_passed = self.time()-start_time
  138. print time_passed
  139.  
  140. # To poll the keyboard and mouse at the same time, we use pygame
  141. # directly. This means that this piece of code requires the xpyriment
  142. # or pygame backend! See:
  143. # <http://www.pygame.org/docs/ref/event.html>
  144. button = False
  145. key = False
  146. # Loop through all 'events', which are key presses or button clicks
  147. for event in pygame.event.get([pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN]):
  148. if event.type == pygame.MOUSEBUTTONDOWN:
  149. button = True
  150. elif event.type == pygame.KEYDOWN:
  151. if event.key == pygame.K_ESCAPE:
  152. raise response_error("The escape key was pressed.")
  153. key = True
  154. # If a key was pressed or a button was clicked, exit the loop
  155. if button or key:
  156. break
  157.  
  158. # If a mouse response was collected, the for loop will be
  159. # continued. However, if the spacebar was pressed, we need to
  160. # do something else:
  161. if key:
  162.  
  163. # Pause the sampler:
  164. my_sampler.pause()
  165. sampler_paused = True
  166.  
  167. # Show the in-the-GUI-prepared form items:
  168. self.experiment.items['mc'].prepare()
  169. self.experiment.items['mc'].run()
  170. __end__
  171. ___prepare__
  172. # Create a canvas item to display the stimulus:
  173. global my_canvas
  174. from openexp.canvas import canvas
  175. my_canvas = canvas(exp, bgcolor = "gray")
  176.  
  177. # Create a sampler item that we'll need for playing
  178. # the sounds.
  179. global my_sampler
  180. from openexp.sampler import sampler
  181.  
  182. # Determine the to-be-played sound file:
  183. sound_stim = sound_list.pop()
  184. sound_file = exp.get_file(sound_stim)
  185.  
  186. my_sampler = sampler(exp, sound_file)
  187. __end__
  188. set description "Executes Python code"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement