我该如何处理keydown事件?

时间:2012-11-08 14:11:19

标签: android sencha-touch-2 keydown

我正在Android设备上开发Sencha Touch 2应用程序。我的Android设备不是普通的手机,所以它也有硬件密钥。我可以对sencha touch中的keydown,keyup或keypress事件做出反应吗?如何?其中一个就够了。

如果它有用:在普通的JavaScript中,密钥可以由onkeydown Listener处理,keyCode为0。

[更新] 我正在研究tutorial for 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上调用函数。我要调用的函数在类控制器中。注释及其名称是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");
}

});

2 个答案:

答案 0 :(得分:2)

在活动

上试试

//按键

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
     if (i == KeyEvent.KEYCODE_BACK) {
          //back button key down
     }
return super.onKeyDown(keyCode, event);
}

//按键

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
      if (i == KeyEvent.KEYCODE_BACK) {
          //back button key up
      }
return super.onKeyUp(keyCode, event);
}

你可以看到所有关键事件 - > here

答案 1 :(得分:0)

我有解决方案! 我的控制器正在观看活动。事件在视图 NotesListContainer 中被触发。

我在我的变量notesListContainer中添加了一个id,因此我可以获取 NotesListContainer 的对象并触发myevent newScanCommand

Ext.application({
        name: "NotesApp",
        models: ["Note"],
        stores: ["Notes"],
        controllers: ["Notes"],
        views: ["NotesList", "NotesListContainer", "NoteEditor"],
        launch: function () {

            var notesListContainer = {
                id: 'nlistcontainer',
                xtype: "noteslistcontainer"
            };
             var noteEditor = {
                xtype: "noteeditor"
            };
            Ext.Viewport.add(notesListContainer, noteEditor);

            document.addEventListener("keydown", Ext.bind(onBackKeyDown, this ), false); 

            function onBackKeyDown(e) {

                // you are at the home screen
                if (Ext.Viewport.getActiveItem().xtype == notesListContainer.xtype ) {

                    //the solution
                    Ext.getCmp('nlistcontainer').fireEvent("newScanCommand", this);

                } else {
                    this.getApplication().getHistory().add(Ext.create('Ext.app.Action', {
                        url: 'noteslistcontainer'
                    }));
                }

            }
        }


    });

所以,这是我的救援:Ext.getCmp('nlistcontainer') ;-) 也许,它提供了更好的解决方案。