2

编辑:下面的这段代码正在工作。不幸的是,我无法在模拟器中测试这些关键事件,所以我犯了一个错误并将错误的 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 );
4

1 回答 1

2

我的一个游戏的生产代码:

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
于 2012-08-15T21:10:37.253 回答