@HostListener('keydown', ['$event', '$event.keyCode'])
onKeyDown($event: KeyboardEvent, keyCode) { if(keyCode !== TAB) {
$event.preventDefault();
} // get value for the key
const val = String.fromCharCode(keyCode);
// get position
const cursorPos = this.input.selectionStart; switch(keyCode) {
case LEFT_ARROW:
this.handleLeftArrow(cursorPos);
return;
case RIGHT_ARROW:
this.handleRightArrow(cursorPos);
return;
} overWriteCharAtPosition(this.input, val, cursorPos);
this.handleRightArrow(cursorPos);
} handleRightArrow(cursorPos) {
const valueBeforeCursor = this.input.value.slice(cursorPos + );
const nextPos = findIndex(valueBeforeCursor, (char) => !includes(SPECIAL_CHARACTERS, char));
if(nextPos > -) {
const newNextPos = cursorPos + nextPos + ;
this.input.setSelectionRange(newNextPos, newNextPos);
}
} handleLeftArrow(cursorPos) {
const valueAfterCursor = this.input.value.slice(, cursorPos);
const previousPos = findLastIndex(valueAfterCursor, (char) => !includes(SPECIAL_CHARACTERS, char));
if(previousPos > -) {
this.input.setSelectionRange(previousPos, previousPos);
}
}

We can use 'setSelectionRange(start, end)' to set cursor postion, in which start postion = end position.

最新文章

  1. linux压缩和解压缩命令大全
  2. php学习手记
  3. 左边图标右边文字,在div里居中
  4. 测试卡尔曼滤波器(Kalman Filter)
  5. appdata文件夹有什么用途?C盘appdata可以删除吗?
  6. 以下各节已定义,但尚未为布局页“~/Views/Shared/_Layout.cshtml”呈现:“Scripts”。
  7. 在ASP.NET项目中使用CKEditor
  8. 【phpcms-v9】如何实现在含有子栏目的栏目下添加内容?
  9. Java 六种异常处理的陋习(转)
  10. Ajax (AppServ服务器练习)
  11. mac查看git路径
  12. PHP跨域jsonp方式
  13. 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)
  14. pycharm的常用快捷键
  15. html样式表格
  16. Java 简明教程
  17. 《Blue_Flke》团队项目软件系统设计改进
  18. 正则表达式,清除HTML标签,但要保留 <br>和<img>标签,其他的清除
  19. scrum站立会议介绍
  20. HDU 5699 二分+线性约束

热门文章

  1. 基于x86平台的Solaris安装视频(时长25分钟)
  2. Dubbo简易学习
  3. c#程序打包、机器代码生成(Ngen.exe)
  4. 【Uva 10163】Storage Keepers
  5. ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)
  6. stm8开发环境配置及测试
  7. Maven中央仓库信息速查
  8. Manning.EJB.3.in.Action.2nd.Edition
  9. golang sync.Mutex(2)
  10. BZOJ2119: 股市的预测(后缀数组)