There are two problems with your code. First your mykeyboard function should not be inside of you handlemyhotspots function. You also need a viz.callback line. Try the following code.
Once you enter the safe zone the variable openDoor will be set to true. When you leave the safe zone openDoor is set to false. When you press a key the onKeyDown function will be called. If openDoor is true and the keypressed is 'e' you will be able to open the door.
Code:
import viz
viz.go()
viz.MainView.setPosition(0,1.8,-5)
SAFE = 1
UNSAFE = 2
TARGET = 3
viz.add('tut_ground.wrl')
CIRCLE_X = 0
CIRCLE_Z = 3
CIRCLE_RADIUS = 0.5
openDoor = False
def handlemyhotspots(id,x,y,z):
global openDoor
if id == SAFE:
print 'inside safe zone'
openDoor = True
viz.clearcolor(1,1,1)
viz.starthotspot(UNSAFE,viz.RECTANGLE_HOTSPOT_OUT,0,3,2.4,6)
elif id == UNSAFE:
print 'outside safe zone'
openDoor = False
viz.clearcolor(0,0,0)
viz.starthotspot(SAFE,viz.RECTANGLE_HOTSPOT_IN,0,3,2.4,6)
elif id == TARGET:
print 'at target'
viz.callback(viz.HOTSPOT_EVENT, handlemyhotspots)
viz.starthotspot(UNSAFE,viz.RECTANGLE_HOTSPOT_OUT,0,3,2.4,6)
viz.starthotspot(TARGET,viz.CIRCLE_HOTSPOT_IN,CIRCLE_X,CIRCLE_Z,CIRCLE_RADIUS)
import math
viz.startlayer(viz.LINES)
viz.linewidth(3)
viz.vertexcolor(viz.RED)
viz.vertex(CIRCLE_X,0,CIRCLE_Z)
viz.vertex(CIRCLE_X,2,CIRCLE_Z)
viz.startlayer(viz.LINE_LOOP)
for a in range(0,360,10):
x = math.sin(viz.radians(a))*CIRCLE_RADIUS
z = math.cos(viz.radians(a))*CIRCLE_RADIUS
viz.vertex(x+CIRCLE_X,0.1,z+CIRCLE_Z)
viz.endlayer()
def onKeyDown(key):
if key == 'e' and openDoor == True:
print "open the door"
viz.callback(viz.KEYDOWN_EVENT,onKeyDown)