# HG changeset patch # User wycc # Date 1281870456 -28800 # Node ID 01d02382dea7202f1921509a1c3598090ce7a27e # Parent 9c5abb4e114b1ae38e33b58c8db9418d65cd57a5 Refactory the mbapp and testsvg to use keyboard events diff -r 9c5abb4e114b -r 01d02382dea7 nodejs/mbapp.js --- a/nodejs/mbapp.js Sun Aug 15 01:44:38 2010 +0800 +++ b/nodejs/mbapp.js Sun Aug 15 19:07:36 2010 +0800 @@ -9,15 +9,24 @@ app=function() { this.mb_rt = mb_rt; - } app.prototype.loadSVG=function(fname) { - svg.loadSVG(mb_rt,mb_rt.root,fname); + svg.loadSVG(this.mb_rt,this.mb_rt.root,fname); } app.prototype.loop=function() { - mb_rt.redraw_all(); - mb_rt.flush(); + this.mb_rt.redraw_all(); + this.mb_rt.flush(); +} +app.prototype.get=function(name) { + return this.mb_rt.mbnames[name]; +} +app.prototype.addKeyboardListener=function(type,f) { + return this.mb_rt.kbevents.add_event_observer(type,f); } exports.app=app; + +// Put all key definition here +exports.KEY_UP=111; +exports.KEY_DOWN=116; diff -r 9c5abb4e114b -r 01d02382dea7 nodejs/testsvg.js --- a/nodejs/testsvg.js Sun Aug 15 01:44:38 2010 +0800 +++ b/nodejs/testsvg.js Sun Aug 15 19:07:36 2010 +0800 @@ -1,6 +1,23 @@ var svg = require("./svg"); var mbapp = require("./mbapp"); +var sys=require("sys"); app = new mbapp.app(); app.loadSVG("test.svg"); +lightbar = app.get("item_lightbar") +item=1 +lightbar[5] = app.get("item"+item)[5] +app.addKeyboardListener(6, function(evt) { + if (evt.keycode == mbapp.KEY_UP) { + item = item - 1; + if (item == 0) item = 9; + lightbar[5] = app.get("item"+item)[5] + } else if (evt.keycode == mbapp.KEY_DOWN) { + item = item + 1; + if (item == 10) { + item = 1; + } + lightbar[5] = app.get("item"+item)[5] + } +}); app.loop();