Skip to content

Commit ae39030

Browse files
committed
Implement keysdown()
1 parent b176dea commit ae39030

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

labextension/vpython/src/glowcommlab.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ var lastcenter = vec(0,0,0)
190190
var lastrange = 1
191191
var lastautoscale = true
192192
var lastsliders = {}
193+
var lastkeysdown = []
193194
var interval = 33 // milliseconds
194195

195196
function update_canvas() { // mouse location and other stuff
@@ -206,6 +207,23 @@ function update_canvas() { // mouse location and other stuff
206207
var ray = cvs.mouse.ray
207208
if (!ray.equals(lastray)) {evt.ray = [ray.x,ray.y,ray.z]; dosend=true}
208209
lastray = ray
210+
211+
var k = keysdown()
212+
var test = true // assume keysdown() is same as lastkeysdown
213+
if (k.length !== lastkeysdown.length) test = false
214+
else {
215+
for (var i=0; i<k.length; i++) {
216+
if (k[i] !== lastkeysdown[i]) {
217+
test = false
218+
break
219+
}
220+
}
221+
}
222+
if (!test) {
223+
evt.keysdown = lastkeysdown = k
224+
dosend = true
225+
}
226+
209227
// forward and range may be changed by user (and up with touch), and autoscale (by zoom)
210228
if (cvs.userspin) {
211229
var forward = cvs.forward

vpython/vpython.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
'local_light', 'menu', 'meta_canvas', 'points', 'pyramid',
2727
'quad', 'radio', 'ring', 'simple_sphere', 'sleep', 'slider', 'sphere',
2828
'standardAttributes', 'text', 'textures', 'triangle', 'vertex',
29-
'wtext', 'winput']
29+
'wtext', 'winput', 'keysdown']
3030

3131
__p = platform.python_version()
3232
_ispython3 = (__p[0] == '3')
@@ -40,6 +40,8 @@
4040
version = [__version__, 'jupyter']
4141
GSversion = [__gs_version__, 'glowscript']
4242

43+
keysdownlist = [] # list of keys currently pressed
44+
4345
# To print immediately, do this:
4446
# print(.....)
4547
# sys.stdout.flush()
@@ -3101,6 +3103,7 @@ def objz(self, obj, operation):
31013103

31023104
## key events conflict with notebook command mode; not permitted for now
31033105
def handle_event(self, evt): ## events and scene info updates
3106+
global keysdownlist
31043107
ev = evt['event']
31053108
if ev == 'pick':
31063109
self.mouse.setpick( evt )
@@ -3185,6 +3188,8 @@ def handle_event(self, evt): ## events and scene info updates
31853188
if 'autoscale' in evt and self.userzoom and not self._set_autoscale:
31863189
self._autoscale = evt['autoscale']
31873190
self._set_autoscale = False
3191+
if 'keysdown' in evt: keysdownlist = evt['keysdown']
3192+
31883193

31893194
def bind(self, eventtype, whattodo):
31903195
evts = eventtype.split()
@@ -4094,3 +4099,10 @@ def print_to_string(*args): # treatment of <br> vs. \n not quite right here
40944099
s += str(a)+' '
40954100
s = s[:-1]
40964101
return(s)
4102+
4103+
def keysdown():
4104+
global keysdownlist
4105+
keys = []
4106+
for k in keysdownlist: # return a copy of keysdownlist
4107+
keys.append(k)
4108+
return keys

vpython/vpython_libraries/glow.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vpython/vpython_libraries/glowcomm.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
var lastrange = 1
136136
var lastautoscale = true
137137
var lastsliders = {}
138+
var lastkeysdown = []
138139
var interval = 33 // milliseconds
139140

140141
function update_canvas() { // mouse location and other stuff
@@ -151,6 +152,23 @@
151152
var ray = cvs.mouse.ray
152153
if (!ray.equals(lastray)) {evt.ray = [ray.x,ray.y,ray.z]; dosend=true}
153154
lastray = ray
155+
156+
var k = keysdown()
157+
var test = true // assume keysdown() is same as lastkeysdown
158+
if (k.length !== lastkeysdown.length) test = false
159+
else {
160+
for (var i=0; i<k.length; i++) {
161+
if (k[i] !== lastkeysdown[i]) {
162+
test = false
163+
break
164+
}
165+
}
166+
}
167+
if (!test) {
168+
evt.keysdown = lastkeysdown = k
169+
dosend = true
170+
}
171+
154172
// forward and range may be changed by user (and up with touch), and autoscale (by zoom)
155173
if (cvs.userspin) {
156174
var forward = cvs.forward

vpython/vpython_libraries/glowcomm.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ var lastcenter = vec(0,0,0)
165165
var lastrange = 1
166166
var lastautoscale = true
167167
var lastsliders = {}
168+
var lastkeysdown = []
168169
var interval = 33 // milliseconds
169170

170171
function update_canvas() { // mouse location and other stuff
@@ -181,6 +182,23 @@ function update_canvas() { // mouse location and other stuff
181182
var ray = cvs.mouse.ray
182183
if (!ray.equals(lastray)) {evt.ray = [ray.x,ray.y,ray.z]; dosend=true}
183184
lastray = ray
185+
186+
var k = keysdown()
187+
var test = true // assume keysdown() is same as lastkeysdown
188+
if (k.length !== lastkeysdown.length) test = false
189+
else {
190+
for (var i=0; i<k.length; i++) {
191+
if (k[i] !== lastkeysdown[i]) {
192+
test = false
193+
break
194+
}
195+
}
196+
}
197+
if (!test) {
198+
evt.keysdown = lastkeysdown = k
199+
dosend = true
200+
}
201+
184202
// forward and range may be changed by user (and up with touch), and autoscale (by zoom)
185203
if (cvs.userspin) {
186204
var forward = cvs.forward

0 commit comments

Comments
 (0)