【问题标题】:How can I handle a keydown event?如何处理 keydown 事件?
【发布时间】:2012-11-10 00:44:34
【问题描述】:

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

如果有帮助:在普通 JavaScript 中,key 可以由 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 上的函数。我要调用的函数在类 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");
}

});

【问题讨论】:

    标签: android sencha-touch-2 keydown


    【解决方案1】:

    在活动上试试这个

    // 按下键

    @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

    【讨论】:

    • 这就是它在 android/Java 中的工作方式,但我想在 sencha touch 2 中捕捉 keydown。
    • 抱歉造成误会。
    • 我在网络上搜索此博客显示后退按钮按下,也许它适合你 -> thanksmister.com/2012/07/06/…
    • 谢谢,所以我向前迈出了一小步。我会在几分钟后更新我的问题。
    【解决方案2】:

    我有办法! 我的控制器正在监视事件。事件在视图 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') ;-) 也许,它提供了更好的解决方案。

    【讨论】:

      猜你喜欢
      • 2011-10-12
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2011-01-09
      相关资源
      最近更新 更多