PDA

View Full Version : How Do I Trap KEY_BACK please?



indifference_engine
2009-08-22, 12:47
I've got the following in a SBC applet and it works for all buttons except KEY_BACK. I need the listener's code to run before the screen changes and to be able to block the button's default action. It works great for KEY_HOME, The code runs and I can allow or prevent the home event by setting the return value of my code to EVENT_UNUSED or EVENT_CONSUME respectively. I just can't figure out KEY_BACK though. It looks like the screen is changing before the listener is fired so do I need to trap a different event? I only want to be able to stop the screen changing if back is pressed, screensavers and other full-screen popups I want to be unaffected.



window:addListener(EVENT_KEY_ALL,
function(event)
local keyCode = event:getKeycode()

if myFunction(keyCode) == true then
-- allow the key's default action.
return EVENT_UNUSED
else
-- do something else and over-ride the key's default action.
self:myOtherFunction()
return EVENT_CONSUME
end
end
)


it looks from various strategic bit of logging that my code runs after the screen changes if KEY_BACK is pressed so the event is picked up by the new window rather than the old one which is not what I want. However, it works perfectly for KEY_HOME and other keys. I'm really at my wits end and would appreciate some help!

Thanks.

Update:
From a bit more debugging it looks like the top window that is going catches EVENT_KEY_DOWN whilst the new window (the one underneath) catches EVENT_KEY_UP. I've tried adding a listener for (EVENT_HIDE | WINDOW_INACTIVE | WINDOW_POP) and returning EVENT_CONSUME but to no avail. I just can't seem to stop the window from being closed. Any adeas? Please.

Tom Wadzinski
2009-08-24, 20:24
I've got the following in a SBC applet and it works for all buttons except KEY_BACK. I need the listener's code to run before the screen changes and to be able to block the button's default action. It works great for KEY_HOME, The code runs and I can allow or prevent the home event by setting the return value of my code to EVENT_UNUSED or EVENT_CONSUME respectively. I just can't figure out KEY_BACK though. It looks like the screen is changing before the listener is fired so do I need to trap a different event? I only want to be able to stop the screen changing if back is pressed, screensavers and other full-screen popups I want to be unaffected.



window:addListener(EVENT_KEY_ALL,
function(event)
local keyCode = event:getKeycode()

if myFunction(keyCode) == true then
-- allow the key's default action.
return EVENT_UNUSED
else
-- do something else and over-ride the key's default action.
self:myOtherFunction()
return EVENT_CONSUME
end
end
)


it looks from various strategic bit of logging that my code runs after the screen changes if KEY_BACK is pressed so the event is picked up by the new window rather than the old one which is not what I want. However, it works perfectly for KEY_HOME and other keys. I'm really at my wits end and would appreciate some help!

Thanks.

Update:
From a bit more debugging it looks like the top window that is going catches EVENT_KEY_DOWN whilst the new window (the one underneath) catches EVENT_KEY_UP. I've tried adding a listener for (EVENT_HIDE | WINDOW_INACTIVE | WINDOW_POP) and returning EVENT_CONSUME but to no avail. I just can't seem to stop the window from being closed. Any adeas? Please.
The typical reason I see for this is that the focused widget gets the first crack at consuming the widget. The focused widget is the last widget added or the last widget focused with window:focusWidget(widget).

Solutions:
1) (if practical for your situation) unfocus all widgets with window:focusWidget(nil)
2) Add your listener instead to the focused widget

indifference_engine
2009-08-27, 16:09
The typical reason I see for this is that the focused widget gets the first crack at consuming the widget. The focused widget is the last widget added or the last widget focused with window:focusWidget(widget).

Solutions:
1) (if practical for your situation) unfocus all widgets with window:focusWidget(nil)
2) Add your listener instead to the focused widget

Number 1 did the trick nicely! Thanks.
One further question, I think it would be prudent for me to remember which widget had the focus and then give it back after my code has executed. Can I do something like this?


local focusedWidget = window:getFocusedWidget()
window:focusWidget(nil)
window:setFocusedWidget(focusedWidget)