编辑:下面的这段代码正在工作。不幸的是,我无法在模拟器中测试这些关键事件,因此我犯了一个错误,并向手机发送了错误的apk。对于那个很抱歉。

我正在尝试以下给出的代码:http://developer.coronalabs.com/reference/index/events/key/eventkeyname

但是我无法检测到后退键事件。我尝试打印event.keyName,但是当我触摸android设备上的后退按钮时未检测到它。

你能帮我吗?谢谢。

这是我的代码:

  -- Key listener
    local function onKeyEvent( event )
        local phase = event.phase
        local keyName = event.keyName
        eventTxt.text = "("..phase.." , " .. keyName ..")"

        if(keyName=="back") then
        local a=display.newText("hello",100,600,nil,35)
        end
        -- we handled the event, so return true.
        -- for default behavior, return false.
        return true
     end

    -- Add the key callback
   Runtime:addEventListener( "key", onKeyEvent );

最佳答案

我的游戏的生产代码:

function onBackButtonPressedAtMap(e)
    if (e.phase == "down" and e.keyName == "back") then
        --Here the key was pressed
        downPress = true
        return true
    else
        if (e.phase == "up" and e.keyName == "back" and downPress) then
            --Here the key was released, put your print("hello!") here.
            storyboard.gotoScene( "mapscreen", "fade", 200 );
            --The next line is to disable this event
            --so the key is not trapped anymore on the "mapscreen"
            --because I want the back key make the APP quit.
            Runtime:removeEventListener( "key", onBackButtonPressedAtMap );
            return true
        end
    end
    return false;
end

09-04 12:53