-
Notifications
You must be signed in to change notification settings - Fork 888
/
keyboard.py
173 lines (147 loc) · 6.79 KB
/
keyboard.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Demo for iohub keyboard
Displays information from ioHub Keyboard Events vs. psychopy.event.geKeys().
"""
from __future__ import absolute_import, division, print_function
from builtins import str
from psychopy import core, visual, event
from psychopy.iohub import launchHubServer
WINDOW_SIZE = 1024, 768
# Start iohub process. The iohub process can be accessed using `io`.
io = launchHubServer()
# A `keyboard` variable is used to access the iohub Keyboard device.
keyboard = io.devices.keyboard
dw = WINDOW_SIZE[0] / 2
dh = WINDOW_SIZE[1] / 2
unit_type = 'pix'
win = visual.Window(WINDOW_SIZE, units=unit_type,
color=[128, 128, 128], colorSpace='rgb255')
# constants for text element spacing:
ROW_COUNT = 10
TEXT_ROW_HEIGHT = (dh * 2) / ROW_COUNT
TEXT_STIM_HEIGHT = int(TEXT_ROW_HEIGHT / 2)
MARGIN = 25
LABEL_COLUMN_X = -dw + MARGIN
VALUE_COLUMN_X = MARGIN
LABEL_WRAP_LENGTH = dw - MARGIN / 2
VALUE_WRAP_LENGTH = dw - MARGIN / 2
TEXT_ROWS_START_Y = dh - MARGIN
# Create some psychoPy stim to display the keyboard events received...
# field labels:
title_label = visual.TextStim(win, units=unit_type,
text=u'Press and Releases Keys for ioHub KB Event Details',
pos=[0, TEXT_ROWS_START_Y],
height=TEXT_STIM_HEIGHT,
color='black', wrapWidth=dw * 2)
title2_label = visual.TextStim(win, units=unit_type,
text=u'Press "Q" Key to Exit Demo',
pos=[0, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT],
height=TEXT_STIM_HEIGHT,
color=[0.25, 0.2, 1],
alignText='center', anchorHoriz='center', anchorVert='top',
wrapWidth=dw * 2)
key_text_label = visual.TextStim(win, units=unit_type, text=u'event.key:',
pos=[LABEL_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 2],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
char_label = visual.TextStim(win, units=unit_type, text=u'event.char:',
pos=[LABEL_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 3],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
modifiers_label = visual.TextStim(win, units=unit_type,
text=u'event.modifiers',
pos=[LABEL_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 4],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
keypress_duration_label = visual.TextStim(win, units=unit_type,
text=u'Last Pressed Duration:',
pos=[LABEL_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 5],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
all_pressed__label = visual.TextStim(win, units=unit_type,
text=u'All Pressed Keys:',
pos=[LABEL_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 6],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
event_type_label = visual.TextStim(win, units=unit_type,
text=u'Last Event Type:',
pos=[LABEL_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 7],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
psychopy_key_label = visual.TextStim(win, units=unit_type,
text=u'event.getKeys():',
pos=[LABEL_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 8],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
# Dynamic stim:
key_text_stim = visual.TextStim(win, units=unit_type, text=u'',
pos=[VALUE_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 2],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
char_stim = visual.TextStim(win, units=unit_type, text=u'',
pos=[VALUE_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 3],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
modifiers_stim = visual.TextStim(win, units=unit_type, text=u'',
pos=[VALUE_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 4],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
keypress_duration_stim = visual.TextStim(win, units=unit_type, text=u'',
pos=[VALUE_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 5],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
all_pressed_stim = visual.TextStim(win, units=unit_type, text=u'',
pos=[VALUE_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 6],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
event_type_stim = visual.TextStim(win, units=unit_type, text=u'',
pos=[VALUE_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 7],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=LABEL_WRAP_LENGTH)
psychopy_key_stim = visual.TextStim(win, units=unit_type, text=u'',
pos=[VALUE_COLUMN_X, TEXT_ROWS_START_Y - TEXT_ROW_HEIGHT * 8],
color='black', alignText='left', anchorHoriz='left',
height=TEXT_STIM_HEIGHT, wrapWidth=dw * 2)
# Having all the stim to update / draw in a list makes drawing code
# more compact and reusable
STIM_LIST = [title_label, title2_label, key_text_label, char_label,
modifiers_label, keypress_duration_label, all_pressed__label,
event_type_label, psychopy_key_label,
key_text_stim, char_stim, modifiers_stim, keypress_duration_stim,
all_pressed_stim, event_type_stim, psychopy_key_stim]
# Clear all events from the global and device level ioHub Event Buffers.
io.clearEvents('all')
QUIT_EXP = False
demo_timeout_start = core.getTime()
# Loop until we get a 'escape' key is pressed,
# or until 15 seconds passed since last keyboard event.
# Note that keyboard events can be compared to a string, matching when
# the event.key or .char == basestring value.
events = []
flip_time = demo_timeout_start = 0
while not 'q' in events and flip_time - demo_timeout_start < 15.0:
for s in STIM_LIST:
s.draw()
flip_time = win.flip()
events = keyboard.getKeys()
for kbe in events:
key_text_stim.text = kbe.key
char_stim.text = kbe.char
psychopy_keys = event.getKeys()
if psychopy_keys:
psychopy_key_stim.text = psychopy_keys[0]
elif kbe.type == "KEYBOARD_PRESS":
psychopy_key_stim.text = ''
modifiers_stim.text = str(kbe.modifiers)
all_pressed_stim.text = str(list(keyboard.state.keys()))
if kbe.type == "KEYBOARD_PRESS":
keypress_duration_stim.text = ''
else:
keypress_duration_stim.text = "%.6f" % kbe.duration
event_type_stim.text = kbe.type
demo_timeout_start = kbe.time
win.close()
core.quit()
# The contents of this file are in the public domain.