I have a need to monitor the state of the Shift key, whether it is up or down. Its purpose is to notify the user that while the shift key is held down, the drag and drop operation they are about to perform is going to COPY the node(s), and not move them.
I have it working perfectly with the code below, however, if I hold the Shift key and perform the drag and drop, the hook no longer exists; the screen no longer responds to the key press and remains in the "pressed" state.
我猜要么是操作顺序问题,要么是缺少一块。 Javascript高手请指教。
<form id="form1" runat="server">
<div>
<table>
<tr>
<td valign="top"><ASP:Literal id="treeLeft" EnableViewState="false" runat="server" /></td>
</tr>
</table>
<asp:Label ID="lblCopyEnabled" runat="server" BackColor="Green" Text="Item will be Copied" ForeColor="White" Font-Bold="true" style="padding: 0px 10px 0px 10px; display: none" />
</div>
<script type="text/javascript">
document.onkeydown = KeyDownHandler;
document.onkeyup = KeyUpHandler;
var SHIFT = false;
function KeyDownHandler(e) {
var x = '';
if (document.all) {
var evnt = window.event;
x = evnt.keyCode;
}
else {
x = e.keyCode;
}
DetectKeys(x, true);
ShowReport();
}
function KeyUpHandler(e) {
var x = '';
if (document.all) {
var evnt = window.event;
x = evnt.keyCode;
}
else {
x = e.keyCode;
}
DetectKeys(x, false);
ShowReport();
}
function DetectKeys(KeyCode, IsKeyDown) {
if (KeyCode == '16') {
SHIFT = IsKeyDown;
}
else {
if (IsKeyDown)
CHAR_CODE = KeyCode;
else
CHAR_CODE = -1;
}
}
function ShowReport() {
var copyLabel = document.getElementById("<%= lblCopyEnabled.ClientID %>");
if (SHIFT) {
copyLabel.style.display = "inline";
ob_copyOnNodeDrop = true;
}
else {
copyLabel.style.display = "none";
ob_copyOnNodeDrop = false;
}
}
</script>
</form>
我不确定为什么您的代码失败,因为您没有包含拖放代码,但有一种更简单的方法可以完成您想要的操作。对于浏览器触发的任何事件,您可以访问 shiftKey 属性,如果按下 Shift 键,该属性将为 true:
window.onmousemove = checkShift;
function checkShift(e)
{
if (!e) var e = window.event;
if (e.shiftKey)
{
....Copy....
}
else
{
....Move....
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)