语言

Menu
Sites
Language
Tizen 2.2b Hardware Buttons

I've just installed the 2.2b SDK and I'm trying to add the Menu/Back buttons to a pre-existing app.

According to the guide, we have to bind an event handler for the "tizenhwkey" event.
Do we just add <script> tags to the HTML file and then insert the code that handles the event?

If so, where? I've attempted to put the sample event handler code in the <head> of the index.html file and it still doesn't work.

 

In case it matters, I'm attempting this on a default emulator.

编辑者为: Brock Boland 17 3月, 2014 原因: Paragraph tags added automatically from tizen_format_fix module.

响应

22 回复
Marco Buettner
I have write this script, just include it in your pageinit oder init function main.js
document.addEventListener('tizenhwkey', function(e) {
var activePage = $.mobile.activePage().attr('id'); // read current page
switch(e.keyName)
{
	case 'back':
		switch(activePage)
		{
			case 'page1':
				tizen.application.getCurrentApplication().exit();
				break;
			default:
				parent.history.back();
				break;
		}
		break;
	case 'menu':
		switch(activePage)
		{
			case 'page2':
				console.log('you are on '+activePage);
				break;
			default:
				//TODO: Do something
				break;
		}
	break;
}});
If the page unknown it go back one step, like on the soft key. If you want to make another function (closing for example) you have to add a case. Its in the sample code already with "page1". When you press page1 the back key, it will close the application. The same can u do with the hardware menu button. the script is based on this html structur
<div data-role="page" id="page1">
	<div data-role="header" data-add-back-btn="false">
		<h1>Page 1</h1>
	</div>
	<div data-role="content">
		<h2>Welcome</h2>
		<p>This is the default start page</p>
	<div>
	<div data-role="footer">
		<h4>Footer (optionale)</h4>
	</div>
</div>

<div data-role="page" id="page2">
	<div data-role="header" data-add-back-btn="false">
		<h1>Page 2</h1>
	</div>
	<div data-role="content">
		<h2>Welcome on Page 2</h2>
		<p>This is the default page</p>
	<div>
	<div data-role="footer">
		<h4>Footer (optionale)</h4>
	</div>
</div>
Also u have to enable tizenhwkey support on your config file :)
Raghavendra Reddy Shiva
or simply add this code too... document.addEventListener( 'tizenhwkey', function(e) { if (e.keyName === 'back') { if ($.mobile.activePage.attr('id') === 'main') { tizen.application.getCurrentApplication().exit(); } else { history.back(); } } else if (e.keyName === 'menu') { // Handle menu options here } });
Fiach Reid

Hi,

   There is a bug in the code listed above. To get the current page in JQuery Mobile, it's $.mobile.activePage.attr("id") not $.mobile.activePage().attr("id")

- ActivePage is not a function, it's a property.

  This bug will cause the app to crash if the back button is pressed.

Fiach.

   

Patrick Casao
Thanks!
karray gargouri
Hi, I used this code to implement the back botton of the emulator(into the init function of the main.js): document.addEventListener('tizenhwkey', function(e) { alert("Tizen event listener"); var activePage = $.mobile.activePage.attr('id'); // read current page switch(e.keyName) { case 'back': switch(activePage) { case 'install': tizen.application.getCurrentApplication().exit(); break; default: parent.history.back(); break; } break; } }); But the back button doesn't work. Even the alert message didn't show up!!! What's wrong?
karray gargouri
I enabled the hwtizen buttons in my config.xml file and (the menu / the home) bottons works perfectly, but the back button remain useless! what's the cause in your opinion?
karray gargouri
here is my code into my main.js file: //////////////////////////////////////////////// //Initialize function var init = function () { // TODO:: Do your initialization job console.log("init() called"); document.addEventListener('tizenhwkey', function(e) { //$(window).on("tizenhwkey", function(e) { //alert("Tizen event listener"); alert(e.keyName); if (e.keyName == 'back') { if (document.URL.indexOf("menu.html") > 0) { tizen.application.getCurrentApplication().exit(); } else { history.back(); } } ///////////// }); }; // window.onload can work without window.onload = init; /////////////////////////////////////////////// Even the ( console.log("init() called");) instruction is never shown!!! I guess that the init function is never executed, and I don't know the reason!
Alexander AVSukhov
Hi karray gargouri, Try to use following code in main.js: $(document).ready(init);
karray gargouri
Hi, Same thing, this doesn't resolve my problem :(
Tiana Sweeney
Similar problem here. I enabled hw events in my config.xml and tried every solution in this thread. Marco Buettner's & Raghavendra Reddy Shiva's solutions work, though only on the Web Simulator. On a default emulator it does not work for me. Any ideas what might be the issue?
Marco Buettner
Which SDK version you have? My code runs proberly on simulator, emulator and device, I use them in all my applications
Tiana Sweeney
Thanks for your reply. I installed 2.2b a couple of days ago. Had some issues getting the IDE to discover the emulator at first. The only fix that worked is, running it in Windows 7 compatibility mode. I thought that might be it, but after turning off compat mode, they still didn't work.
Marco Buettner
Why 2.2b? 2.2 is official available :)
Tiana Sweeney
I updated with the install manager, that's what it updated to. ;)
Tiana Sweeney
I re-installed (x2) everything, and it works now. It installed 2.2 this time. Got a different problem now though. Whenever I use window.tizen and then run the Web app on the emulator, I get the error:
TypeError: 'undefined' is not an object. 
Though when I run it on the simulator it runs perfectly fine. You can find the complete description of the problem here: http://stackoverflow.com/questions/18468864/tizen-alarm-not-added-on-emulator Do you maybe have an idea what might be the problem?

Thanks for all the examples:

I have multiple popover in my Web App IQ FitFun ( http://iqfitfun.com/IQFitFunHtml/index.html ) and the following code does the trick (JQuery needed).

Take care (order of else if ...), if you have multiple div-layers.

----- Remark:  ----------------------

If you use web app checker:

https://source.tizen.org/compliance/application-compatibility/web-application-checker-user-guide

you will get error message "tizen:setting'', its property ''hwkey-event'' should not exist".

Just ignore, your app is Tizen 2.2 and the checker uses 2.1 (2.2 version is in preparation).

if (appStoreName == APPSTORE_Tizen) {

    document.addEventListener( 'tizenhwkey', function(e) {

        if (e.keyName === 'back') {

            if ($('#fullversion').is(":visible")) {

                // OK button

                game.toggleFullVersion();

                game.startWithFirstPuzzle();

            }

            else if ($('#resetGameProgressConfirm').is(":visible")) {

                // Cancel button

                game.toggleGameprogressConfirm();

                $('#resetGameProgressButton').focus();

            }

            else if ($('#gameprogress').is(":visible")) {

                // Close game progress popover

                game.toggleGameprogress();

            }

            else if ($('#puzzleshortcome').is(":visible")) {

                // Continue button

                game.startWithFirstPuzzle();                

            }

            else if ($('#about').is(":visible")) {

                // Close about popover

                game.toggleAbout();

                setFocusToFirstAnswer();

            }

            else if ($('#explanation').is(":visible")) {

                // Close explanation popover

                game.toggleExplanation();

            }

            else {

                // Exit app

                tizen.application.getCurrentApplication().exit();

            }

        }

        else if (e.keyName === 'menu') {

            // Currently no menu options 

        }

    });

}

 

Kilim A

I  make  a such game.
I need when you press a button 'back' on the device then user will go to the previous scene , and if it is the first page (level or Layout),user would be exiting  of the game ('exit').
   in principle at tizen sdk it's done in folder  js - main.js and it

var init = function () {
    // TODO:: Do your initialization job
    console.log("init() called");

    // add eventListener for tizenhwkey
    document.addEventListener('tizenhwkey', function(e) {
        if(e.keyName == "back")
            tizen.application.getCurrentApplication().exit();
    });
};

However,C2  ignores it somehow (main.js) , namely probably as follows:
  file index:

// Pause and resume on page becoming visible/invisible
        function onVisibilityChanged() {
            if (document.hidden || document.mozHidden || document.webkitHidden || document.msHidden)
                cr_setSuspended(true);
            else
                cr_setSuspended(false);
        };
        
        document.addEventListener("visibilitychange", onVisibilityChanged, false);
        document.addEventListener("mozvisibilitychange", onVisibilityChanged, false);
        document.addEventListener("webkitvisibilitychange", onVisibilityChanged, false);
        document.addEventListener("msvisibilitychange", onVisibilityChanged, false);
        
    </script>



Question - How to make exit of the game on the first page of the game by clicking the back button device, and in other cases would go to the previous page game (Layout)?

Marco Buettner

At first setup all "pages" a unique id

After that you can use

var init = function () {
    // TODO:: Do your initialization job
    console.log("init() called");

    // add eventListener for tizenhwkey
    document.addEventListener('tizenhwkey', function(e) {
        if(e.keyName == "back")
        {
            var page = $.mobile.activePage.attr('id');
            switch(page)
            {
                case 'firstPage':
                    tizen.application.getCurrentApplication().exit();
                    break;
                
                default:
                    $.mobile.back();
                    break;
            }
        }
    });
};

 

Kilim A

thank you.

fahad khatib

Hello Everyone !

First of all thanks everyone to share your knowledge. I am facing a problem regarding back key event like as -

1. I have a "First_Page.html" and my main.js contain backkey listener and have several div behave as page in "First_Page.html". I logically implement scroll view between those pages (div) and the back key navigation working perfectly.

2. Add another "Another_page.html" with simple html code and navigation between "First_Page.html" & "Another_page.html" is just OK. But 

3. Adding another html page "Problem_page.html" having some JS code which will genreate html element in java script and push through document.write.

But now neighter the main.js backkey listerner nor other implementation didnt works for "Problem_page.html". 

Once i get in "Problem_page.html" page i cant come out using back key. I just stuck over on that page. Please help with your knowledge and experience.

Thanks in advance.

Marco Buettner

Have you include main.js on the Problem_page.html? How do you change between the pages? do u use href="xyz.html" or $.mobile.changePage(xyz.html) (in 2.3: tau.changePage(xyz.html)?
 

AVSukhov

Hello,

You can create Tizen Web Project from Template in IDE, and see how it is implemented.