开发者

How do I ignore a keyEvent in Javascript?

I have a DataTable in YUI. I'm trying to get the table to ignore all keyEvents. I've tried these methods:

YAHOO.util.Event.addListener(singleSelectDataTable, "keydown", function(oEvent) {
    YAHOO.util.Event.stopPropagation(oEvent); 
});

OR

YAHOO.util.Event.preventDefault(singleSelectDataTable.tableKeyEvent);    

OR

singleSelectDataTable.subscribe('tableKeyEvent', function(oArgs) { 
  YAHOO.util.Event.preventDefault(oArgs.event); 
});

I've looked at a couple of YUI examples to intercept click events, but they don't analogize to this specific scenario. I created a standalone HTML test file if that will help: http://pastebin.com/khfR4Stk. The foundational problem is that we don't want to support arrow key up or arrow key down in our tables; it's a scrolling table and in order for it to work properly we would have to adjust the scrolling thumb once the selection goes past the 'shown-window'.

The only other solution I could think of is to subscribe to the tableKeyEvent and then if the keypress is up-arrow, then unselect the newly selected row, selecting the previous row, doing the appropriate analogue for a down-arrow (basically undoing what the keypress just did). This didn't seem like the r开发者_开发知识库ight solution…


The tableKeyEvent is raised after the up/down arrow key has been handled. So trying to stop that event will not help.

Looking at the _onTbodyKeydown function of the DataTable widget, I noticed that setting the selection mode to an invalid mode disables key arrow key navigation. Luckily it doesn't seem to break the other selection handling. At least not in your example.

So just change selectionMode:"single" to selectionMode:"" and you should be fine :-) (Of course there is no guarantee that this will work in future versions)


Try creating the equiv of this onclick

function noenter(evt)
{
    var k = evt.keyCode||evt.which;
    return k != 13;
}

Get the syntax for getCharCode and tell the script that when it receives input, it needs to deny it.


can't you just add an eventhandler that returns false to the keydown event?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜