简体   繁体   English

JavaScript或jquery触发左箭头键按下事件(模拟箭头键按下)

[英]JavaScript or jquery trigger left arrow key press event (Simulate arrow key press)

I am trying to simulate the left and the right arrow key press in a text area within a rad grid control (Telerik). 我正在尝试在rad网格控件(Telerik)内的文本区域中模拟向左和向右箭头键。

I have a bug that is specific to the browser Firefox where the tab event(got this part fixed) and the arrow keys wont work. 我有一个特定于浏览器Firefox的错误,其中选项卡事件(已修复此部分)和箭头键不起作用。 Every other browser works fine. 其他所有浏览器都可以正常工作。

So as a workaround i want to simulate the arrow keys using JavaScript or jquery. 因此,作为一种解决方法,我想使用JavaScript或jquery模拟箭头键。

Below is what i have working with the tab event fix included. 以下是我使用的包含选项卡事件修复程序的内容。 In addition to this I check if the key code 37 (this is the left arrow key) is pressed. 除此之外,我还要检查是否按下了键代码37(这是向左箭头键)。 At this point i want to simulate the left arrow press in the text area. 在这一点上,我想模拟在文本区域中按左箭头。

$(document).on('keydown', function (event) {
    if (event.keyCode == 9) {   //tab pressed
        event.preventDefault(); // stops default action
    }
});

$('body').keyup(function (e) {

   var code = e.keyCode || e.which;

   //Left arrow key press
   if (code == '37') 
   {
       moveLeft();// This does not trigger the left arrow event
   }

   //Tab button pressed
   if (code == '9') {
       //shift was down when tab was pressed focus -1
       if (e.shiftKey && code == 9) {                                    
           var focused = $(':focus');
           var inputs = $(focused).closest('form').find(':input');
           inputs.eq(inputs.index(focused) - 1).focus();
       }
       //tab was pressed focus +1
       else {
            var focused = $(':focus');
            var inputs = $(focused).closest('form').find(':input');
            inputs.eq(inputs.index(focused) + 1).focus();                                 
            }
   }      
});

Any help on this issue would be appreciated 在这个问题上的任何帮助,将不胜感激

I got this working by moving the cursor back one space, below is the code I used to simulate the left arrow key press. 我通过将光标向后移动一个空格来完成此工作,以下是我用来模拟向左箭头键按下的代码。

Note - To simulate right the key code is 39 and set the myval property to -1. 注–要正确模拟,密钥代码为39,并将myval属性设置为-1。

$('body').keydown(function (e) {

     var code = e.keyCode || e.which; //Find the key pressed

     if (code == '37') {

          e.preventDefault(); // stop default action
          var elementId = e.target.id;// get id of input text area                                     

          var el = document.getElementById(elementId),
          myval = 1 // set mouse cursor to move back one space
          cur_pos = 0;

          if (el.selectionStart) {
              cur_pos = el.selectionStart;
          } 
          else if (document.selection) {
              el.focus();

              var r = document.selection.createRange();
              if (r != null) {
                  var re = el.createTextRange(),
                  rc = re.duplicate();
                  re.moveToBookmark(r.getBookmark());
                  rc.setEndPoint('EndToStart', re);

                  cur_pos = rc.text.length;
               }
          }

          if (el.setSelectionRange) {
              el.focus();
              el.setSelectionRange(cur_pos - myval, cur_pos - myval);
          }
          else if (el.createTextRange) {
              var range = el.createTextRange();
              range.collapse(true);
              range.moveEnd('character', cur_pos - myval);
              range.moveStart('character', cur_pos - myval);
              range.select();
          }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM