How to disable default key handler?

I have following problem:

My HD-DVD App contains some buttons. By default I can move between buttons use arrow keys. I want make own key handler for moving between buttons. I want to use own algorithm for move. I make following code:

addEventListener("controller_key_down", keyHandler, true);

function keyHandler(evt)
{
var disableDefaultKeyHandler = true;
switch (evt.key)
{
case 38: // VK_UP
// My Key Handler
break;

...


default:
disableDefaultKeyHandler = false;
break;
}
if(disableDefaultKeyHandler)
{
evt.stopPropagation();
evt.preventDefault();
}
}


But sometimes default key handler is called and focus moves to incorrect button. I think that my key handler(keyHandler function) sometimes is skiping and default handler is called.

Do you have any ideas how I can solve this problem

P.S.

iHDSim 0.1



Answer this question

How to disable default key handler?

  • atulmistry

    First you should download the new version of iHDSim, which is version 0.2 :-)

    Second, you should probably add some tracing (or use a breakpoint in the debugger) to detect when the default: case is being used.

    Three things I can think of:

    • The event's cancellable property is false (not sure why that would be)
    • Another event listener on a higher target has called stopPropagation, so you never get the event
    • You have an exception in your code and the script either stops running or jumps to a catch clause that is after the call to preventDefault call (I see no exception handling logic in your code, but I assume you simplified)


  • Antioch

    Actually it is probably because you are passing true to addEventListener; try passing false instead (you won't get events that are not targetted at any buttons if you attempt to capture, because capturing listeneres don't get notified of events on their own object... the application in this case)

  • How to disable default key handler?