我正在 Android 设备上开发 Sencha Touch 2 应用程序。我的安卓设备不是普通的手机,所以它也有硬件键。我可以对 sencha touch 中的 keydown、keyup 或 keypress 事件做出反应吗?如何?其中一个就足够了。
如果有帮助:在普通 JavaScript 中,键可以由 onkeydown 监听器处理,并且 keyCode 为 0。
[更新] 我为 sencha touch 编写本教程。我已经完成了所有步骤,并且效果很好。现在我想抓住硬件钥匙,我上面描述的。我在Handle Android Hardware...中放置了一个 addEventListener 。
所以我的代码看起来像:
Ext.application({
name: "NotesApp",
models: ["Note"],
stores: ["Notes"],
controllers: ["Notes"],
views: ["NotesList", "NotesListContainer", "NoteEditor"],
launch: function () {
var notesListContainer = {
xtype: "noteslistcontainer"
};
var noteEditor = {
xtype: "noteeditor"
};
Ext.Viewport.add(notesListContainer, noteEditor);
document.addEventListener("keydown", Ext.bind(onKeyDown, this), false);
function onKeyDown(e) {
// you are at the home screen
if (Ext.Viewport.getActiveItem().xtype == notesListContainer.xtype ) {
alert('aha'); // give out 'aha'
// LABEL1
} else {}
}
}
});
如何调用位置 LABEL1 上的函数。我要调用的函数在类 controllers.Notes 中,它的名称是 onNewScanCommand:
Ext.define("NotesApp.controller.Notes", {
extend: "Ext.app.Controller",
config: {
refs: {
// We're going to lookup our views by xtype.
notesListContainer: "noteslistcontainer",
noteEditor: {
selector: 'noteeditor',
xtype: 'noteeditor',
autoCreate: true
}
},
control: {
notesListContainer: {
// The commands fired by the notes list container.
newNoteCommand: "onNewNoteCommand",
editNoteCommand: "onEditNoteCommand",
newScanCommand: "onNewScanCommand"
}
}
},
onNewScanCommand: function () {
console.log("onNewScanCommand");
var now = new Date();
var noteId = (now.getTime()).toString() + (this.getRandomInt(0, 100)).toString();
var newNote = Ext.create("NotesApp.model.Note", {
id: noteId,
dateCreated: now,
title: "",
narrative: ""
});
// here I call a function in PhnoeGap, then this call a nativ function
scan("echome", function(echoValue) {
newNote.set("title", echoValue.scanNummer);
newNote.set("narrative", echoValue.scanType);
});
var notesStore = Ext.getStore("Notes");
/*
if (null == notesStore.findRecord('id', newNote.data.id)) {
notesStore.add(newNote);
}*/
notesStore.add(newNote);
notesStore.sync();
notesStore.sort([{ property: 'dateCreated', direction: 'DESC'}]);
//this.activateNotesList();
},
// Base Class functions.
launch: function () {
this.callParent(arguments);
Ext.getStore("Notes").load();
console.log("launch");
},
init: function () {
this.callParent(arguments);
console.log("init");
}
});