抓住PhoneGap for Android的按键 [英] Capturing back key press on PhoneGap for Android

查看:99
本文介绍了抓住PhoneGap for Android的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PhoneGap进行Android应用程序开发,我被困在捕获设备上的后退按键!我从其他线程和官方wiki页面尝试了一切,但似乎没有任何工作! :(

I am using PhoneGap for Android Application development and I'm stuck at capturing the back key press on the device! I tried everything from other threads and from the official wiki page but none seem to work! :(

我可以用keyEvent.menuTrigger和keyEvent.searchTrigger捕获菜单和搜索键,但似乎无法捕获后退按钮!我尝试了keyEvent.backTrigger但是它没有起作用!为'backKeyDown'事件添加事件监听器不起作用!

I could capture menu and search keys with keyEvent.menuTrigger and keyEvent.searchTrigger but can't seem to capture the back button press! I tried keyEvent.backTrigger similarly but it didn't work! Adding event listener for 'backKeyDown' event doesn't work either!

这是我的代码:

BackButton.override();
    document.addEventListener('backKeyDown', function(e) {
  console.log('Caught it!');
}, false);
keyEvent.searchTrigger= searchPressed;
keyEvent.menuTrigger=menuPressed;
keyEvent.backTrigger=backPressed;

控制台说:

Line 1 : Uncaught TypeError: Cannot call method 'backTrigger' of undefined

这不意味着something.backTrigger )被调用,但是对象的东西不存在,那是什么东西?还是有什么办法?

Doesn't that mean something.backTrigger() is being called but the object something doesn't exist. What is that something? Or is there any way around?

与KeyEvent一样,没有运气!
请帮助!

Tried the same with KeyEvent too, with no luck! Please help!

提前感谢! :)

推荐答案

PhoneGap Wiki 页面关于Android按钮处理程序已更新为PhoneGap 0.9.5:

PhoneGap Wiki page about Android button handler has been updated for PhoneGap 0.9.5 :

// This is your app's init method. Here's an example of how to use it
function init() {
    document.addEventListener("deviceready", onDR, false);
} 

function onDR(){
    document.addEventListener("backbutton", backKeyDown, true);
    //boot your app...
}

function backKeyDown() {
    // do something here if you wish
    // alert('go back!');
}

通过PhoneGap 0.9.6为我工作

Works for me with PhoneGap 0.9.6

您还可以使用 menubutton 搜索按钮事件来处理其他按钮。

You can also handle others buttons with menubutton and searchbutton events.

这篇关于抓住PhoneGap for Android的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆