forked from Benreuveni/Intro-to-Python
/
Text Input.py
38 lines (32 loc) · 1.36 KB
/
Text Input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from psychopy import visual, event
win = visual.Window(size=(700, 700), fullscr=False, screen=0, allowGUI=False, monitor=u'testMonitor',
allowStencil=False, color=[0,0,0], colorSpace=u'rgb', units='cm')
def textInput():
#until return pressed, listen for letter keys & add to text string
text = ''
while event.getKeys(keyList=['escape'])==[]:
letterlist=event.getKeys()
letterDict = {'space': ' '}
# letterlist=event.getKeys(keyList=['0','1','2','3','4','5','6','7','8','9','backspace'])
for l in letterlist:
if l == 'space':
text += ' '
elif l == 'return':
text += '\n'
#if key isn't backspace, add key pressed to the string
elif l != 'backspace':
text += l
#otherwise, take the last letter off the string
elif len(text)>0:
text = text[:-1]
#continually redraw text onscreen until return pressed
textObject = visual.TextStim(win=win, ori=0, name='pause', text=text, font=u'Arial', pos=[0, 0],
height=1.0, wrapWidth=None, color=u'white', colorSpace=u'rgb', opacity=1, depth=-1.0)
# textReport.draw()
textObject.draw()
win.flip()
text = str(text)
event.clearEvents()
print text
return text
textInput()