VKeyboard=Core.class(EventDispatcher) local KEYS={"back", "menu", "search", "center", "left", "right", "up", "down"} function VKeyboard:init() self.visible=false local keys=Sprite.new() local function onKeyDown(b, event) if b:hitTestPoint(event.x, event.y) then local e=Event.new(Event.KEY_DOWN) e.keyCode=KeyCode[b:getText():upper()] stage:dispatchEvent(e) end end local function onKeyUp(b, event) if b:hitTestPoint(event.x, event.y) then local e=Event.new(Event.KEY_UP) e.keyCode=KeyCode[b:getText():upper()] stage:dispatchEvent(e) end end for k,v in ipairs(KEYS) do local s=TextField.new(nil, v) keys:addChild(s) s:setPosition(k*40, 0) s:addEventListener(Event.MOUSE_DOWN, onKeyDown, s) s:addEventListener(Event.MOUSE_UP, onKeyUp, s) end keys:setVisible(true) self.keys=keys --stage:addChild(keys) keys:setPosition(20, 10) self:_draw() end function VKeyboard:_draw() if self.button then self.button:removeFromParent() self.button:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) end local button=Shape.new() button:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) local color=0xff0000 if self.visible then color=0x00ff00 end button:setFillStyle(Shape.SOLID, color) button:beginPath() button:moveTo(0, 0) button:lineTo(20, 0) button:lineTo(20, 20) button:lineTo(0, 20) button:endPath() button:setPosition(0, 0) stage:addChild(button) self.button=button end function VKeyboard:show() self.visible=true self:_draw() stage:addChild(self.keys) return self end function VKeyboard:hide() self.visible=false self:_draw() stage:removeChild(self.keys) return self end function VKeyboard:onMouseDown(event) if self.button:hitTestPoint(event.x, event.y) then if self.visible then self:hide() else self:show() end end end VK=VKeyboard.new()