# HG changeset patch # User wycc # Date 1282260084 -28800 # Node ID e77ae2d232459df5916f53cc51ae23e38f0c4009 # Parent 299ed9319dc61258b5178b50f18761c6e46f11ab Add addKeyListener to handle key input. diff -r 299ed9319dc6 -r e77ae2d23245 nodejs/mbapp.js --- a/nodejs/mbapp.js Tue Aug 17 10:36:36 2010 +0800 +++ b/nodejs/mbapp.js Fri Aug 20 07:21:24 2010 +0800 @@ -1,5 +1,6 @@ var mbfly = require("mbfly"); var svg = require("./svg"); +var sys = require("sys"); var mb_rt = new mbfly.mb_rt(":0.0", 720,480); var ldr = mbfly.img_ldr_new("."); var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); @@ -8,12 +9,21 @@ mb_rt.root.add_shape(background); app=function() { + var self = this; this.mb_rt = mb_rt; + this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); + this.keymap={}; + this.onKeyPress = null; } app.prototype.loadSVG=function(fname) { svg.loadSVG(this.mb_rt,this.mb_rt.root,fname); } +app.prototype.KeyPress = function(evt) { + if (this.onKeyPress) this.onKeyPress(evt.sym); + if (evt.sym in this.keymap) this.keymap[evt.sym](); +} + app.prototype.loop=function() { this.mb_rt.redraw_all(); this.mb_rt.flush(); @@ -28,6 +38,13 @@ this.mb_rt.redraw_all(); this.mb_rt.flush(); } +app.prototype.dump=function() { + sys.puts(this.onKeyPress); +} + +app.prototype.addKeyListener=function(key,f) { + this.keymap[key] = f; +} exports.app=app; diff -r 299ed9319dc6 -r e77ae2d23245 nodejs/testsvg.js --- a/nodejs/testsvg.js Tue Aug 17 10:36:36 2010 +0800 +++ b/nodejs/testsvg.js Fri Aug 20 07:21:24 2010 +0800 @@ -9,8 +9,7 @@ item=1; lightbar[5] = app.get("item"+item)[5]; -app.addKeyboardListener(mbapp.EVT_KB_PRESS, function(evt) { - if (evt.sym == mbapp.KEY_UP) { +app.addKeyListener(mbapp.KEY_UP, function() { item = item - 1; if (item == 0) item = 1; else { @@ -18,7 +17,9 @@ var an = new animate.linear(app,lightbar,target[2],target[5],0.3); an.start(); } - } else if (evt.sym == mbapp.KEY_DOWN) { +}); + +app.addKeyListener(mbapp.KEY_DOWN, function() { item = item + 1; if (item == 10) { item = 9; @@ -27,6 +28,6 @@ var an = new animate.linear(app,lightbar,target[2],target[5],0.3); an.start(); } - } }); + app.loop();