Package VisionEgg :: Module DaqKeyboard
[frames] | no frames]

Source Code for Module VisionEgg.DaqKeyboard

 1  # The Vision Egg: DaqKeyboard 
 2  # 
 3  # Author(s): Hubertus Becker <hubertus.becker@uni-tuebingen.de> 
 4  # Copyright: (C) 2005 by Hertie Institute for Clinical Brain Research, 
 5  #            Department of Cognitive Neurology, University of Tuebingen 
 6  # URL:       http://www.hubertus-becker.de/resources/visionegg/ 
 7  # 
 8  # This library is free software; you can redistribute it and/or modify 
 9  # it under the terms of the GNU Lesser General Public License as 
10  # published by the Free Software Foundation; either version 2.1 of 
11  # the License, or (at your option) any later version. 
12   
13  """ 
14  Data acquisition and triggering over the keyboard. 
15   
16  This module was programmed using information from the web site 
17  http://www.pygame.org/docs/ref/pygame_key.html 
18   
19  """ 
20   
21  #################################################################### 
22  # 
23  #        Import all the necessary packages 
24  # 
25  #################################################################### 
26   
27  import VisionEgg 
28  import VisionEgg.Core 
29  import VisionEgg.FlowControl 
30  import VisionEgg.ParameterTypes as ve_types 
31  import sys 
32  import pygame 
33   
34  __version__ = VisionEgg.release_name 
35   
36  #################################################################### 
37  # 
38  #        KeyboardInput 
39  # 
40  #################################################################### 
41   
42 -class KeyboardInput:
43
44 - def get_pygame_data(self):
45 """Get keyboard input (return values are in pygame.locals.* notation).""" 46 keys = pygame.key.get_pressed() 47 return [k for k, v in enumerate(keys) if v]
48
49 - def get_string_data(self):
50 """Get keyboard input (return values are converted to keyboard symbols (strings)).""" 51 pressed = self.get_pygame_data() 52 keys_pressed = [] 53 for k in pressed: # Convert integers to keyboard symbols (strings) 54 keys_pressed.append(pygame.key.name(k)) 55 return keys_pressed
56 57 get_data = get_string_data # Create alias
58 59 #################################################################### 60 # 61 # KeyboardTriggerInController 62 # 63 #################################################################### 64
65 -class KeyboardTriggerInController(VisionEgg.FlowControl.Controller):
66 """Use the keyboard to trigger the presentation.""" 67
68 - def __init__(self,key=pygame.locals.K_1):
69 VisionEgg.FlowControl.Controller.__init__( 70 self, 71 return_type=ve_types.Integer, 72 eval_frequency=VisionEgg.FlowControl.Controller.EVERY_FRAME) 73 self.key = key
74
75 - def during_go_eval(self):
76 return 1
77
78 - def between_go_eval(self):
79 for event in pygame.event.get(): 80 # if (event.type == pygame.locals.KEYUP) or (event.type == pygame.locals.KEYDOWN): 81 if event.type == pygame.locals.KEYDOWN: 82 if event.key == self.key: 83 return 1 84 else: 85 return 0
86