I have a trial where participants are asked to enter a string of text. Special characters are defined as in the chunk of code below.
My problem is that I can’t seem to enter capital letters. Does anyone know how to go about doing this?
shift_flag = False
for key in event.getKeys():
if key in ['escape']:
core.quit()
elif key in ['return']:
if len(captured_string) > 1:
subject_response_finished=True
final_response=captured_string
continueRoutine=False
elif key in ['delete','backspace']:
captured_string = captured_string[:-1]
elif key in ['space']:
captured_string = captured_string+' '
elif key in ['period']:
captured_string = captured_string+'.'
else:
captured_string = captured_string+key
Thanks, Xiaotong and Michael. I solved it like this shortly after posting (sorry for not updating quicker), so essentially like Xiaotong suggests:
try:
shift_flag
except NameError:
shift_flag = False
for key in event.getKeys():
if key in ['escape']:
core.quit()
elif key in ['lshift','rshift']:
shift_flag=True
pass
elif key in ['return']:
if len(captured_string) > 1:
subject_response_finished=True
final_response=captured_string
continueRoutine=False
elif key in ['delete','backspace']:
captured_string = captured_string[:-1]
elif key in ['space']:
captured_string = captured_string+' '
elif key in ['period']:
captured_string = captured_string+'.'
elif shift_flag == True:
captured_string = captured_string+key.upper()
shift_flag = False
else:
captured_string = captured_string+key
Michael, could you explain which part is old-fashioned and why? (the way of handling capitals, or the whole way of handling special characters?) Special characters with umlauts work ok with this (by giving e.g. ü as input when the left bracket is pressed).
The code might rely on old-style approaches with text being assumed to be ASCII rather than Unicode. But if it works for you in practice, it doesn’t matter if it’s old-fashioned. Go for it.