javascript - Why are arrow keys, tab, etc not received by input field's onkeypress handler in some browsers? - Stack Ove

I'm building a text input field specialized for entering and editing times. One of the parts of th

I'm building a text input field specialized for entering and editing times. One of the parts of the functionality calls for various ways to focus on the different ponents of the time (hours, minutes, seconds), which I indicate through a text selection. Direct selection is possible with the mouse and this is working great. The other feature is keyboard navigation.

Most of this functionality relies on the fact that I'm able to handle keyPress events, suppress the default behavior and substitute a special action instead.

In Firefox, I have this working nicely. The user may use left/right arrow keys or tab/shift-tab to move between parts of the time (and when they get to the end, the next tab key will leave the field and focus the next element normally).

In Internet Explorer 7 (potentially others?) the arrow keys and tab are not even received by the keypress handler. If arrow keys are pressed, the text selection is lost and the cursor moves by one. The effect of providing multiple fields disappears and it results in the control feeling broken. Tab also seems to skip the handler and just immediately flips to the next focusable element.

Are there any tricks to intercepting these keys?

I'm building a text input field specialized for entering and editing times. One of the parts of the functionality calls for various ways to focus on the different ponents of the time (hours, minutes, seconds), which I indicate through a text selection. Direct selection is possible with the mouse and this is working great. The other feature is keyboard navigation.

Most of this functionality relies on the fact that I'm able to handle keyPress events, suppress the default behavior and substitute a special action instead.

In Firefox, I have this working nicely. The user may use left/right arrow keys or tab/shift-tab to move between parts of the time (and when they get to the end, the next tab key will leave the field and focus the next element normally).

In Internet Explorer 7 (potentially others?) the arrow keys and tab are not even received by the keypress handler. If arrow keys are pressed, the text selection is lost and the cursor moves by one. The effect of providing multiple fields disappears and it results in the control feeling broken. Tab also seems to skip the handler and just immediately flips to the next focusable element.

Are there any tricks to intercepting these keys?

Share Improve this question edited Sep 23, 2019 at 20:20 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 25, 2009 at 13:26 Mark RenoufMark Renouf 31k19 gold badges96 silver badges125 bronze badges 2
  • I should add that I am testing IE7 within VMware hosted on Linux. – Mark Renouf Commented Jun 25, 2009 at 13:28
  • Found this reference too: unixpapa./js/key.html – Mark Renouf Commented Jun 25, 2009 at 19:08
Add a ment  | 

1 Answer 1

Reset to default 8

You need to use onkeydown for non-character keys. onkeypress in IE only handles keys that return a string.

To specifically quote the MSDN documentation:

As of Microsoft Internet Explorer 4.0, the onkeypress event fires and can be canceled for the following keys:

  • Letters: A - Z (uppercase and lowercase)
  • Numerals: 0 - 9
  • Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
  • System: ESC, SPACEBAR, ENTER

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745230254a4617645.html

相关推荐

  • 30年悬案告破,平均曲率流的奇点真相曝光,揭晓「冰块融化」的数学秘密

    选自quantamagazine作者:Steve Nadis机器之心编译一块冰块漂浮在水中,随着时间推移,它会逐渐融化成一个微小的冰粒,最终完全消失。在这个过程中,冰块表面变得越来越光滑,所有不规则形状和锐利边缘都会逐渐消失。对于你我来说,

    2小时前
    00
  • 全栈开发者硬核实测:明基 RD280U 编程显示器能否重塑编码体验?

    一、引言作为一名全栈开发,我踩过不少显示器的“坑”。要么分辨率低,代码字体发虚,看久了眼睛酸涩;要么屏幕比例不合适,代码显示行数有限,来回滚动查找代码片段浪费时间。直到遇到明基RD280U专业编程显示器,才感觉找到了真正的“得力助手”。它似

    1小时前
    00
  • 无headers爬虫 vs 带headers爬虫:Python性能对比

    一、Headers的作用及常见字段Headers是HTTP请求的一部分,用于传递客户端(如浏览器或爬虫)的元信息。常见的Headers字段包括:User-Agent:标识客户端类型(如浏览器或爬虫)。Referer:表示请求的来源页面。Ac

    1小时前
    00
  • 终于有人把生日悖论讲明白了

    生日悖论是一个概率论中的现象,它说明了在一定的条件下,一个不太可能的偶然事件发生的概率会比直觉认为的要大得多。具体来说,生日悖论指的是,在一个随机选择的23人群体中,至少有两个人生日相同的概率超过50%。假设一个班级有23个学生,我们可能会

    1小时前
    00
  • Arch&amp;Win10双磁盘双系统安装及相关知识

    前置win10 安装在第一块磁盘且有第二块磁盘分区以50G硬盘大小为例,使用cfdisk devsda命令进行分区:其中boot分区2G,交换分区4G,剩下全部分配给根目录。分区后完整目录如下:devsda12Gdevs

    1小时前
    20
  • 电脑换行键没反应

    1、如果不是键盘的原因&#xff0c;可能是因为你不小心按过insert键。你试试按下insert键看看管不管用&#xff0c;我的就是在编辑器里面按回车不管用&#xff0c;不过光标能移动。 2、如果只是回车键不能

    1小时前
    00
  • 优质GitHub项目推荐:助力开发与效率提升

    今天,为大家推荐三个各具特色的GitHub项目,涵盖了Linux命令学习、GitHub使用优化以及翻译辅助等多个方面。原文地址: GitHub精选1. linux-command项目地址:在线体验:项目简介 这是一个非盈利性的仓库,搜集了6

    1小时前
    00
  • 【用ChatGPT学编程】——如何让AI帮你写代码注释和Debug?

    【前言】 在软件开发的道路上,编写清晰的代码注释和高效Debug是每位开发者的必修课。随着人工智能技术的发展,ChatGPT这类强大的语言模型为我们提供了新的学习和工作方式。本文将详细介绍如何借助ChatGPT完成代码注释编写和Debug,

    1小时前
    00
  • 【数据结构】图论存储革新:十字链表双链设计高效解决有向图入度查询难题

    导读大家好,很高兴又和大家见面啦!!!在图的链式存储探索中,我们曾解析邻接表的灵活性与局限——它虽以链表动态管理边集,却难解有向图入度查询的效率困局。如何让存储结构在空间与时间上实现双重突破?本文将聚焦一种革新设计——十字链表,它通过顶点表

    1小时前
    00
  • 【赵渝强老师】TiDB的列存引擎:TiFlash

    TiDB的TiFlash提供列式存储,且拥有借助ClickHouse高效实现的协处理器层。除此以外,它与TiKV非常类似,依赖同样的Multi-Raft体系,以Region为单位进行数据复制和分散。TiFlash以低消耗不阻塞TiKV写入的

    1小时前
    00
  • 云函数采集架构:Serverless模式下的动态IP与冷启动优化

    爬虫代理在 Serverless 架构中使用云函数进行网页数据采集,不仅能大幅降低运维成本,还能根据任务负载动态扩展。然而,由于云函数的无状态特性及冷启动问题,加上目标网站对采集行为的反制措施(如 IP 限制、Cookie 校验等),开发者

    1小时前
    00
  • 出版社教学资源管理系统的开发

    出版社教学资源管理系统的开发是一个涉及多个环节的复杂项目。下面我将从需求分析、功能模块、技术选型、开发流程、注意事项等方面进行阐述。一、需求分析与规划在开发任何软件系统之前,充分的需求分析至关重要。对于出版社教学资源管理系统,需要考虑以下方

    53分钟前
    00
  • 高效阅读AI论文的秘诀——如何快速吸收最前沿的知识

    高效阅读AI论文的秘诀——如何快速吸收最前沿的知识引言近年来,人工智能(AI)的发展日新月异,每天都有大量新论文发布。对于想要深入理解AI技术的开发者、研究者甚至爱好者来说,阅读论文是获取最新知识的必要途径。但面对深奥的数学公式、复杂的实验

    45分钟前
    00
  • 5G到底有多牛?一文看懂它的原理与优势!

    5G到底有多牛?一文看懂它的原理与优势!在互联网时代,网络速度决定了一切。4G改变了我们的生活,让流媒体、移动支付、短视频成为现实。而如今,5G技术正以前所未有的速度,推动全球科技进入新的纪元。那么,5G到底有什么黑科技?为什么它能比4G快

    41分钟前
    00
  • 别等故障来了再救火,AI运维预测让问题提前暴露!

    别等故障来了再救火,AI运维预测让问题提前暴露!在传统运维中,很多问题都是“发生后才解决”。服务器崩了?紧急修复!网络卡了?赶紧排查!数据库炸了?全员救火!但这种被动处理模式,常常导致企业停摆、用户流失,甚至造成不可挽回的损失。这几年,AI

    40分钟前
    00
  • 《分布式软总线:不同频段Wi

    分布式软总线技术作为实现设备互联互通的关键,正逐渐成为构建万物互联世界的基石。然而,当分布式软总线面临不同频段Wi-Fi环境时,设备发现的兼容性问题成为了阻碍其广泛应用的一大挑战。这一问题不仅影响着用户体验,也制约着分布式软总线在智能家居、

    28分钟前
    00
  • 公网IP和内网IP区别

    1. 地址范围公网IP:全球唯一,由ICANN统一分配,范围涵盖所有未保留的IPv4地址(如1.0.0.0~223.255.255.255)。内网IP:仅限局域网内部使用,IPv4保留以下私有地址段:10.0.0.0 ~ 10.255.25

    21分钟前
    00
  • 超级炫酷的AI字体设计,学起来

    1.一些案例昨天阅读了一个老师写的这个文章,自己亲自动手实践了,确实非常的不错,结合我的实践结果和相关的经历,分享给大家,写的详细一些,包括提示词(参考的老师的)和对应的使用方法(超级详细);大家看完这个操作流程,同样可以实现你想要的文字,

    13分钟前
    00
  • 【PB建站教程】pboot网站的留言板标签调用

    pbootcms网站常用的参数标签汇总、以及操作过程中的一些bug问题解决方法,pboot网站二开,以下龙腾飞网络科技-小吴在建站实操中笔记记录,pbootcms建站教程pb网站建设教程 保存使用非常方便:【PB建站教程】pboot网站的

    4分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信