当我输入一个字段时,它会自动输入另一个字段

When i type in one field then its auto type another field

在我的项目中,我使用自定义 keyboard.The 问题是,当自定义键盘指令在两个输入字段中使用时,当我在一个输入字段中键入内容时,它会在另一个输入字段中自动键入。例如,有 A 和 B 是我的两个输入字段,两个 ng-model 不同但我的自定义键盘指令名称相同,就像我的文本一样,现在当我在 A 输入字段中输入内容时,它会自动输入B. 这是我完成的代码,请检查并纠正我的问题。

HTML代码

<div class="">
        <input id="attrcls" type="text" ng-model="test"  style="width: 40%;" class="form-control input-md" ng-focus="onFocus('attrcls')" my-text>

        <input id="attrcls1" type="text" ng-model="test1" style="width: 40%;" class="form-control input-md" ng-focus="onFocus('attrcls')">
          <div class="keyboard-container"  ng-if="!focused">
              <ul ng-repeat="row in layout track by $index" style="list-style-type: none;">
                  <li ng-if="element.action != 'esc'" ng-repeat="element in row.row track by $index" class="btn" ng-click="keyPressed(element.value, element.action)">
                    {{element.value}}
                  </li>
                  <li ng-if="element.action == 'esc'" ng-repeat="element in row.row track by $index" class="btn" ng-click="keyPressed(element.value, element.action)"  ng-blur = "onBlur()">
                    {{element.value}}
                  </li>
              </ul>
          </div>
    </div>

控制器代码

$scope.keyPressed = function(value, action){
        $scope.someInput = value;
        $rootScope.$broadcast('keyPressed', $scope.someInput, action);
    }

      $scope.focused = true;
      $scope.onFocus = (id)=>{
        if($scope.focused){
            $scope.focused = false;
        }
      }
      $scope.onBlur = ()=>{
        $scope.focused = true;
      }

指令代码

  .directive('myText', ['$rootScope', function($rootScope) {
    return {
        link: function(scope, element, attrs) {
            $rootScope.$on('keyPressed', function(e, val, action) {
                var domElement = element[0];
                if (document.selection) {
                    domElement.focus();
                    var sel = document.selection.createRange();
                    sel.text = val;
                    domElement.focus();
                }
                else if (domElement.selectionStart || domElement.selectionStart === 0) {
                    var startPos = domElement.selectionStart;
                    var endPos = domElement.selectionEnd;
                    var scrollTop = domElement.scrollTop;

                    if(action === 'del'){
                        if(startPos === endPos){
                            domElement.value = domElement.value.substring(0, startPos-1) + domElement.value.substring(endPos, domElement.value.length);
                            domElement.focus();
                            domElement.selectionStart = startPos - 1;
                            domElement.selectionEnd = startPos - 1;
                        }
                        else{
                            domElement.value = domElement.value.substring(0, startPos) + domElement.value.substring(endPos, domElement.value.length);
                            domElement.focus();
                            domElement.selectionStart = startPos;
                            domElement.selectionEnd = startPos;
                        }

                        domElement.scrollTop = scrollTop;
                    }else if(action === 'esc'){
                            $(".keyboard-container").css("display","none");
                            $("#attrcls").removeAttr('my-text');
                    }else{
                        domElement.value = domElement.value.substring(0, startPos) + val + domElement.value.substring(endPos, domElement.value.length);
                        domElement.focus();
                        domElement.selectionStart = startPos + val.length;
                        domElement.selectionEnd = startPos + val.length;
                        domElement.scrollTop = scrollTop;
                    }
                } else {
                    domElement.value += val;
                    domElement.focus();
                }
            });
        }
    }
}])

键盘(及其布局)通常由 operating system software (or some utility started by the OS). It could depend upon your desktop environment 的某个层管理。

在 Linux 上,您的 display server (e.g. Xorg) 知道键盘布局。

AngularJs. If you use that, some web browser is handling the keyboard layout (and the browser, e.g. firefox or chrome -not your application- would be aware of keyboard layout changes, and perhaps be notified by the display server or the window manager; for example look into X11 keymap-notify events that the Xorg server would send on Linux to the browser; on Windows see keyboard input functions and related in the WinAPI没有直接连接。