javascript - How to detect non-visible keys (ENTER, F1, SHIFT) altogether using JS or jQuery? - Stack Overflow

First of all Sorry, I don't know what to call those keys (ENTER, F1, HOME, etc).Actually, I'm

First of all Sorry, I don't know what to call those keys (ENTER, F1, HOME, etc).

Actually, I'm creating an input search box which onkeyup calls a function. When the user has input at least two keys my function is called and relevant search results are displayed using AJAX. The problem is when the user presses arrow key, HOME, END, etc then also my ajax is called which I don't want. And pressing F5 key to reload the page when focused on the input don't reloads the page, instead calls the AJAX, that's why it's a big issue for me.

$('input[name=\'search\']').on(keyup, function(e) {
  if ($('input[name=\'search\']').val().length >= 2) {
    // call ajax to display results..
  }
});

I want to add an extra expression in the if, that checks if the non-visible key is pressed or not. Like -

if ($('input[name=\'search\']').val().length >= 2 && (EXPRESSION FOR VISIBLE KEYS)) {
  // call ajax to display results..
}

If any visible key is pressed then ajax is called, else its not.

I don't know how to achieve this. As I cannot do e.keycode == '65' for each keys like A,B,C,\,=,+,1,2,3,etc

Is there a ready made library to check this or any other way to do this? Please help me.

First of all Sorry, I don't know what to call those keys (ENTER, F1, HOME, etc).

Actually, I'm creating an input search box which onkeyup calls a function. When the user has input at least two keys my function is called and relevant search results are displayed using AJAX. The problem is when the user presses arrow key, HOME, END, etc then also my ajax is called which I don't want. And pressing F5 key to reload the page when focused on the input don't reloads the page, instead calls the AJAX, that's why it's a big issue for me.

$('input[name=\'search\']').on(keyup, function(e) {
  if ($('input[name=\'search\']').val().length >= 2) {
    // call ajax to display results..
  }
});

I want to add an extra expression in the if, that checks if the non-visible key is pressed or not. Like -

if ($('input[name=\'search\']').val().length >= 2 && (EXPRESSION FOR VISIBLE KEYS)) {
  // call ajax to display results..
}

If any visible key is pressed then ajax is called, else its not.

I don't know how to achieve this. As I cannot do e.keycode == '65' for each keys like A,B,C,\,=,+,1,2,3,etc

Is there a ready made library to check this or any other way to do this? Please help me.

Share Improve this question edited Jul 12, 2015 at 19:39 Rohit Kumar asked Jul 7, 2015 at 18:37 Rohit KumarRohit Kumar 2,0311 gold badge15 silver badges30 bronze badges 1
  • window.event.shiftKey ? stackoverflow./questions/6178431/… – trainoasis Commented Jul 7, 2015 at 18:40
Add a ment  | 

4 Answers 4

Reset to default 3

You can check if e.which or e.keyCode is between 48 and 90, which corresponds to any number or letter of the alphabet. http://www.cambiaresearch./articles/15/javascript-char-codes-key-codes

if (+e.which >= 48 && +e.which <=90){ ... }

If you want to support other ones that arnt just alphanumeric, the easiest way i can see is to create an array of all the keycodes you want to whitelist, and check if the array contains the keycode. this is a good thread on how to do that: How to find if an array contains a specific string in JavaScript/jQuery?

Event keyCode is your answer:

$('input[name=\'search\']').keyup(function(event) {
   console.log( event.keyCode );
});

For example the keyCode for Enter key is 13. Also you can check Ctrl, Shift and Alt keys by the following variables:

if(event.ctrlKey) {
      if (event.ctrlLeft) {
        console.log('ctrl-left'); 
      }
      else {
        console.log('ctrl-right');
      }
}
event.altKey
...
event.altLeft
...
event.shiftKey
...

For the accepting only letters, you may use this way:

var letter = String.fromCharCode(e.which);
if (/[a-z\d]/i.test(letter)) {
    // send ajax call
}

I have got another solution to check whether a visible character key was pressed or not. Actually the basic thing I used in my code is that the visible character keys when pressed will change the length of the value of input field. I used keydown instead of keyup to get length of the string just before the current key is pressed. Have a look to my code -

$('input[name=\'search\']').on('keydown', function() {
      var orig_len = $('input[name=\'search\']').val().length; // this gets the original length just before the key was pressed
      setTimeout(function() {
          if ($('input[name=\'search\']').val().length >= 2 && $('input[name=\'search\']').val().length != orig_len) {
            // my ajax call
          }, 25);
        /* in 25ms the lenght is updated and the if checks
        whether length has changed from previous or not
        because visible char key changes length
        */
      });

The setTimeout was necessary because this 25ms elapsed time is more than enough to update the lenght of the input field after the current key is pressed.

Please have a discussion on this and tell me it is okay or not or have some bad impact or can cause issue on different browsers as I have not checked it yet on other browsers.

UPDATE: Its fully functional on all major browsers. Its working as I wanted it to be.

Another answer is a good JavaScript ponent to implement an advanced search box, like TypeAhead ponent, easy peasy ;)

Please have a look at : https://twitter.github.io/typeahead.js/examples/

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

相关推荐

  • UCB的硅光MEMS OCS控制技术

    昨天写的旭创与nEye合作了一个64端口硅光OCS一、光电路交换技术概述(一)电交换与分组交换电路交换的概念源于早期的电话交换机,通过物理连接实现设备间的通信,就像早期打电话时,接线员手动连接线路一样。而分组交换则是

    50分钟前
    00
  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    49分钟前
    00
  • 【Docker项目实战】使用Docker部署IT工具箱Team·IDE

    一、Team·IDE介绍1.1 Team·IDE简介Team IDE 是一款集成多种数据库(如 MySQL、Oracle、金仓、达梦、神通等)与分布式系统组件(如 Redis、Zookeeper、Kafka、Elasticsearch)管理

    46分钟前
    00
  • 人工智能与ai有什么区别

    一、引言:概念之辨的必要性在科技浪潮席卷全球的当下,人工智能(Artificial Intelligence,简称AI)已成为人们耳熟能详的词汇。然而,当我们深入探讨时,会发现“人工智能”与“AI”这两个表述在语义和使用场景上存在微妙差异。

    39分钟前
    00
  • 实现一个 MySQL 配置对比脚本需要考虑哪些细节?

    作者:李彬,爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查。爱好有亿点点多,吉他、旅行、打游戏爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。本文约 1500 字,预计阅读需要 3 分钟。引言想

    37分钟前
    00
  • 在VMware虚拟机中安装Windows 7全攻略(避坑指南)

    ⚠️写在前面 最近发现很多开发者在调试老旧系统时都需要用到Windows 7环境&#xff08;特别是银行、医疗等行业的遗留系统&#xff09;&#xff0c;但实体机安装既不现实也不安全。今天就手把手教你在虚拟机

    37分钟前
    00
  • Nat. Mater.

    大家好,今天给大家分享一篇近期发表在Nat. Mater.上的研究进展,题为:De novo design of self-assembling peptides with antimicrobial activity guided

    36分钟前
    00
  • 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版

    文件: 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版 install.esd 大小: 2920270404 字节 修改时间: 2025年4月8日, 星期二, 11 : 04 : 59 MD5: D5F8F0AD

    34分钟前
    00
  • Windows系统密钥检测工具PIDKey 2.1中文版

    Windows系统密钥检测工具PIDKey 2.1中文版 【下载地址】Windows系统密钥检测工具PIDKey2.1中文版 Windows系统密钥检测工具PIDKey 2.1中文版是一款功能强大的工具&#xff0c;专为管理Win

    32分钟前
    00
  • 雨晨 26200.5516 Windows 11 IoT 企业版 LTSC 2024 轻装版

    简述&#xff1a;以下为YCDISM (雨晨作品自2025年03月25日起通用介绍&#xff0c;若无重大更改不再额外敖述) 全程由最新YCDISM2025脱机装载26100.1742_zh-cn_windows_11_

    29分钟前
    00
  • VoidZero 的野心,开发者的福音!

    前言昨天分享了尤雨溪公司 VoidZero 最新的产品 TSDown,我相信肯定有同学和我一样好奇,尤雨溪为什么要推出这么多工具,来增加大家的学习压力!今天我们从整体上分析下,这些产品的功能和目的!正文VoidZero 是尤雨溪于 2020

    18分钟前
    00
  • 解决Windows 10家庭单语言版语言限制:升级专业版全攻略

    解决Windows 10家庭单语言版语言限制:升级专业版全攻略 在日常使用Windows 10系统时,部分用户可能会遇到系统提示“当前许可证仅支持单一显示语言”的困扰。这一问题通常出现在预装或激活了Windows 10家庭单语言版的设备上

    18分钟前
    00
  • 电子产品设计与电源优化实用策略

    产品降成本是商业活动中的常见行为,可贯穿于产品设计、研发、生产、运输、销售及维护的各个环节。然而,降成本策略必须建立在对产品品质要求不降低的基础上,确保设计参数满足要求并通过相关测试。以下是具体优化与深度分析。研发工程师通常从设计入手,选择

    17分钟前
    00
  • OpenAI“Agent 圣经”翻车?LangChain 创始人怒怼“全是坑”!

    整理 | Tina当前,AI 领域呈现出一种近乎“追星式”的热情氛围,每当有新的东西发布,便迅速引发广泛关注与高度评价,仿佛技术变革即将一触即发。同时大家情绪也波动剧烈,从“危机论”到“爆发论”频繁切换。OpenAI 最近出的《A Pra

    15分钟前
    00
  • IDEA 2025.1 版震撼发布,建议更新

    好久不见甚是想念,有小伙伴催更了。 各位编程大神们!今天我要给大家带来一个超级劲爆的消息!IntelliJ IDEA 2025.1 终极版震撼发布啦!就像一颗超级炸弹在编程界炸开,这些新特性能让咱们的开发效率直接翻倍,简直爽歪歪

    10分钟前
    10
  • 142页长文揭秘DeepSeek

    新智元报道编辑:定慧【新智元导读】DeepSeek-R1是近年来推理模型领域的一颗新星,它不仅突破了传统LLM的局限,还开启了全新的研究方向「思维链学」(Thoughtology)。这份长达142页的报告深入剖析了DeepSeek-R

    5分钟前
    10
  • 精选 5 款基于 .NET 开源、功能强大的编辑器

    前言今天大姚给大家分享 5 款基于 .NET 开源、免费、功能强大的编辑器,其中包含 C# 代码编辑器、文本编辑器、CSV 文本编辑器和 Markdown 编辑器。NetPadNetPad是一个基于.NET开源(MIT License)、跨

    4分钟前
    10
  • 通过Windows镜像离线升级或修复Win10系统

    通过Windows镜像离线升级或修复Win10系统 导航 文章目录 通过Windows镜像离线升级或修复Win10系统导航关键条件与原理查询系统版本下载镜像打开镜像进行更新修复 关键条件与原理 版本一致性 需确保镜像的**SKU&am

    3分钟前
    10
  • Windows 10 LTSC 2019 中文版下载及安装教程(附安装包)

    &#xff08;cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24&#xff09;涵盖常见疑问和注意事项&#xff1a; cn_windows_10_enterpr

    3分钟前
    10
  • Windows 10 64位系统安装全攻略:从工具选择到系统激活的完整指南

    Windows 10 64位系统安装全攻略:从工具选择到系统激活的完整指南 在数字化办公时代,Windows 10系统凭借稳定的内核、丰富的应用生态和持续的官方支持,仍然是个人用户和企业用户的首选操作系统。本文将以Windows 10 6

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信