可以模拟Android“硬件”吗?网络浏览器中的后退按钮? [英] Is it possible to simulate Android "hardware" back-button in web browser?

查看:74
本文介绍了可以模拟Android“硬件”吗?网络浏览器中的后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很高兴可以在浏览器中测试Cordova / Ionic应用程序中的许多案例。但是我还没有找到假冒Android(以前的硬件)后退按钮的方法。

It's great that it's possible to test many cases in a Cordova/Ionic app in the browser. But I haven't yet found a way to fake pressing Android's (formerly hardware-) back-button.

很高兴拥有一个带有后退按钮或按键组合的额外抽屉(例如Alt + Ctrl +<)会触发使Ionic认为按下Android后退按钮的事件。

Would be nice to have an extra drawer with a back-button or a key combination (e.g. Alt+Ctrl+<) which triggers an event that makes Ionic think the Android back-button was pressed.

是否可以使用JavaScript触发此类事件?怎么样?

Is it possible to trigger such event with JavaScript? How?

要明确:我只想在我的网络浏览器中测试离子应用时才这样做。所以Android玩家:不需要在这里提供Java代码 - 我们不在Android设备或模拟器上。并且:我非常确定类似 $ ionicHistory.goBack() $ window.history.back()不是我想要的。

To be clear: I only want this when testing ionic apps in my web-browser. So you Android guys: no need to provide Java code here - we're not on an Android device or emulator. And: I'm pretty sure something like $ionicHistory.goBack() or $window.history.back() is not what I want.

推荐答案

我有一个工作解决方案,我想与大家分享。按 Alt + Ctrl +< 时,会触发后退按钮事件。当然,诸如 navigator.app.exitApp()之类的东西将不起作用,但是简单的导航工作,例如关闭模态。

I have a working solution I'd like to share with you. When pressing Alt+Ctrl+< it triggers the backbutton event. Of course such things like navigator.app.exitApp() won't work, but simple navigation works, e.g. closing modals.

AppModule.run(function ($window, $document, $ionicPlatform) {
    'use strict';
    var document = $document[0];

    function triggerBackButton() {
        var backButtonEvent = document.createEvent('Events');
        backButtonEvent.initEvent('backbutton', false, false);
        document.dispatchEvent(backButtonEvent);
    }

    function registerBackButtonFake() {
        document.addEventListener('keyup', function (event) {
            // Alt+Ctrl+<
            if (event.altKey && event.ctrlKey && event.keyCode === 188) {
                triggerBackButton();
            }
        });
    }

    if (!$window.cordova) {
        $ionicPlatform.ready(registerBackButtonFake);
    }
});

这篇关于可以模拟Android“硬件”吗?网络浏览器中的后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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