0

Clean way to handle keyboard event locally?

Hi everyone,
I've developed a custom class which extends the KONtx.element.Container class, it has a couple of buttons inside and it has a function to intercept the keyboard event (such as up, down, left, right...) to make the buttons inside blurred or focused according. Here is the code of the class (stub code only):
CODE
var MyClass = new KONtx.Class({
ClassName: 'MyClass',
Extends: KONtx.element.Container,
config: {
},
initialize: function() {
this.parent();
this.button1 = new KONtx.element.Button({
// code to define the button
}).appendTo(this);
this.button2 = new KONtx.element.Button({
// code to define the button
}).appendTo(this);
this.button3 = new KONtx.element.Button({
// code to define the button
}).appendTo(this);
this._onKeyPress.subscribeTo(KONtx.application, 'onWidgetKeyPress', this);
},
_isFocused: function() {
return this.element.hasFocus
|| this.button1.element.hasFocus
|| this.button2.element.hasFocus
|| this.button3.element.hasFocus;
},
_onKeyPress: function(event) {
if (this.visible && this._isFocused()) {
var keyCode = event.payload.keyCode;
var eventHandled = false;
switch (keyCode) {
case KONtx.utility.KeyHandler.map.RIGHT:
// Code to handle the event
eventHandled = true;
break;
case KONtx.utility.KeyHandler.map.LEFT:
// Code to handle the event
eventHandled = true;
break;
case KONtx.utility.KeyHandler.map.UP:
// Code to handle the event
eventHandled = true;
break;
case KONtx.utility.KeyHandler.map.DOWN:
// Code to handle the event
eventHandled = true;
break;
default:
break;
}

if (eventHandled) { // Only cancel the event which is handled by this container
event.stopPropagation();
event.preventDefault();
}
}
}
});

As you can see from the above code snippet, I have to intercept the keyboard event from the KONtx.application object globally. If I have 2 instances of this class on the screen, I have to make sure that only one instance is handle the keyboard events by checking if the instance itself is focused or one of its children UI element is focused. I think this keyboard event intercepting mechanism is *not* elegant, but I'm still using it, because it's the only way I know so far.
My question is: "Is there any way to make a container element to automatically handle the keyboard event when itself or one of its child elements is focused and don't intercept the keyboard events when both itself and its child elements are not focused?"
I'm really new to KONtx! So any advices are welcome!

Tinh

by
2 Replies
  • Check out the element.js file, and you'll see that a container (which buttons extend from), fires several events.

    You'll be interested in the onNavigate event, which passes in the navigtion (up, down, left, right) direction. Just setup a handler like you would for the onSelect event.

    CODE
     752                case 'gainfocus':
    753 this.fire('onFocus', payload, nodeEvent);
    754 break;
    755 case 'losefocus':
    756 this.fire('onBlur', payload, nodeEvent);
    757 break;
    758 case 'keydown':
    759 if (nodeEvent.keyCode == map.SELECT) this.fire('onSelect', payload, nodeEvent);
    760 break;
    761 case 'navigate':
    762 payload = payload || {};
    763 payload.direction = payload.direction || ['up','right','down','left'][nodeEvent.direction];
    764 this.fire('onNavigate', payload, nodeEvent);
    765 break;
    0
  • Many thanks Steve!
    This is exactly what I'm looking for, my code is now much better with onNavigate event handler

    Tinh
    0

Recent Posts

in General - Yahoo! TV Widgets