结合JavaScript的关键preSS事件 [英] Binding javascript keypress events

查看:174
本文介绍了结合JavaScript的关键preSS事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要监控的Shift键的状态,无论是涨还是跌。它的目的是通知用户,同时shift键不放,拖放操作,他们即将完成将要复制的网络节点,而不是移动它们。

我把它与下面的code完美的工作,但是,如果我按住Shift键并进行拖放,钩不再存在;屏幕不再响应的关键preSS和保持pressed状态。

我猜有任何操作问题,订单或缺失的那一块。 JavaScript的expers请告知。

谢谢!

 <表ID =form1的=服务器>
< D​​IV>
    <表>
        &所述; TR>
            < TD VALIGN =顶>< ASP:文字的id =treeLeft的EnableViewState =假=服务器/>< / TD>
        < / TR>
    < /表>    < ASP:标签ID =lblCopyEnabled=服务器背景色=绿色文本=项目将被复制前景色=白字体粗体=真的风格=填充:0像素10px的0像素10px的;显示:无/>
< / DIV> <脚本类型=文/ JavaScript的>
     document.onkeydown = KeyDownHandler;
     document.onkeyup = KeyUpHandler;     VAR SHIFT = FALSE;     功能KeyDownHandler(五){
         VAR X ='';
         如果(的document.all){
             VAR EVNT = window.event;
             X = evnt.key code;
         }
         其他{
             X = e.key code;
         }
         DetectKeys(X,真);
         ShowReport();
     }
     功能KeyUpHandler(五){
         VAR X ='';
         如果(的document.all){
             VAR EVNT = window.event;
             X = evnt.key code;
         }
         其他{
             X = e.key code;
         }
         DetectKeys(X,FALSE);
         ShowReport();
     }
     功能DetectKeys(主要code,IsKeyDown){
         如果(主要code =='16'){
             SHIFT = IsKeyDown;
         }
         其他{
             如果(IsKeyDown)
                 CHAR_ code =键code;
             其他
                 CHAR_ code = -1;
         }
     }
     功能ShowReport(){
         变种copyLabel =的document.getElementById(&下;%= lblCopyEnabled.ClientID%gt;中);
         如果(SHIFT){
             copyLabel.style.display =内联;
             ob_copyOnNodeDrop = TRUE;
         }
         其他{
             copyLabel.style.display =无;
             ob_copyOnNodeDrop = FALSE;
         }     }
< / SCRIPT>< /表及GT;


解决方案

我不知道为什么你的code失败,因为你没有包括拖放code,但有一个更简单的方法做你想做的。对于浏览器发射任何情况下,您可以访问shiftKey属性,这将是真实的,如果shift键pressed:

  window.onmousemove = checkShift;功能checkShift(五)
{
  如果VAR E = window.event(E!);
  如果(e.shiftKey)
  {
    ....复制....
  }
  其他
  {
    ....移动....
  }
}

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.

I'm guessing there is either an order of operations issue or a missing piece. Javascript expers please advise.

Thanks!

<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>

解决方案

I'm not sure why your code is failing, since you didn't include drag and drop code, but there's an easier way to do what you want. For any event fired by the browser, you can access shiftKey property, which is going to be true, if the shift key is pressed:

window.onmousemove = checkShift;

function checkShift(e)
{
  if (!e) var e = window.event;
  if (e.shiftKey)
  {
    ....Copy....
  }
  else
  {
    ....Move....
  }
}

这篇关于结合JavaScript的关键preSS事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆