diff nodejs/mbapp.js @ 730:e77ae2d23245

Add addKeyListener to handle key input.
author wycc
date Fri, 20 Aug 2010 07:21:24 +0800
parents 299ed9319dc6
children 1719484ed15d
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;