开发者

How to check for keyboard modifiers like "shift" on click handler?

When using a jQuery click event, I would would check the event object that is passed to the click handler to determine if the user was holding the shift key when they clicked.

Is it possible to check for keyboard开发者_如何学JAVA modifiers like the shift key when using the click binding in KnockoutJS? If so, how?


In current Knockout code, the click binding is actually passed the jQuery event object. This object has shiftKey, altKey, and ctrlKey.

So, the function that your click binding is bound to could look like:

click: function(event) { if (event.shiftKey) { doSomething(); } }

Here is a sample: http://jsfiddle.net/rniemeyer/ak4vL/

If the function that you are passing to your click binding is anonymous, then you can write it like:

<button data-bind="click: function(event) { viewModel.click(event); }">More Hide Anonymous Function</button>

Like in this sample: http://jsfiddle.net/rniemeyer/YUhzk/


You can do this with regular javascript, assuming knockoutjs has captured the click event, you can check if any of the following are true:

 e.shiftKey
 e.altKey
 e.ctrlKey
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜