Mercurial > MadButterfly
changeset 730:e77ae2d23245
Add addKeyListener to handle key input.
author | wycc |
---|---|
date | Fri, 20 Aug 2010 07:21:24 +0800 |
parents | 299ed9319dc6 |
children | 1719484ed15d |
files | nodejs/mbapp.js nodejs/testsvg.js |
diffstat | 2 files changed, 22 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- 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;
--- 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();