我使用的是第三方控件(Aspose.Cells.GridWeb)。它基本上是一个基于Web的Excel控件。当用户选择一个单元格时,控件会引发onCellSelected事件。
问题是,我需要知道用户按下了什么键才能引发此事件。我需要这些信息来确定如何处理事件。
我正在使用JQuery的keydown事件来捕获单击的按钮。
$(document).keydown(function(event) {
isKeyLastClicked = true;
keyLastClickedId = event.which;
isMouseButtonLastClicked = false;
mouseButtonLastClickedId = null;
});
问题是在引发keydown事件之前引发了控件的onCellSelected事件。这是预期的行为吗?
在引发控件事件之前,有没有办法获取密钥的ID?
提前致谢。
答案 0 :(得分:1)
经过SO聊天的长时间讨论......
由于绑定keydown
事件不会产生所需的结果,因此您必须更改onCellSelected
函数。它应该遵循下一个模式:
cell
)被分配给(临时)变量。keydown
事件时,将检查密钥代码:
ev.which
或ev.keyCode
)等于某个键,则再次调用onCellSelected
函数,将临时变量的值作为第一个参数传递。代码具有以下结构。修改它以适合您的应用程序:
var lastCell = null;
function onCellSelected (cell) {
if(lastCell == null) {
lastCell = cell;
return; //Wait for the next listener
}
... //Rest of code
}
$(document).keydown(function(e){
if(lastCell == null) return; //No need to do unnecessary calculations
//Whatever you want, example:
if(e.keyCode == 32) {
onCellSelected(lastCell);
lastCell = null; //Reset
}
... //Rest of code
});
注意:keyCode
,charCode
和which
,另见:http://asquare.net/javascript/tests/KeyCode.html
答案 1 :(得分:0)
我仍然不确定如何首先触发keydown
事件(或者甚至可能)。但是,我确实发现您可以随时访问最后一个Windows事件。因此,我只是访问window.event.type
,而不是依赖于事件来设置变量。
以下是一些示例代码:展示如何使用window.event
:
if (window.event.type == "keydown") { //keyboard click
if (IsKeyNextType(window.event.which)) { //checks which key was pressed. In this case a key qualifying as "Next" (ie: Enter, Tab, etc)
//PERFORM NEXT ACTIONS
}
else { //"Previous" keys (ie: Up arrow, Left arrow)
//PERFORM PREVIOUS ACTIONS
}
}
else if (window.event.type == "click") { //mouse click
//PERFORM MOUSE CLICK ACTIONS
}
答案 2 :(得分:0)
它的添加取决于代码的顺序。
代码中的第一个将首先执行。
最好使用回调而不是两个单独的函数(可能是易失性的)。