What are the Javascript Keycodes for the letter 'a' - is it 65 or

I am currently working with JavaScript on my MacBook Pro running OSX 10.11.x, using the Chrome browser. The function I am using is:

window.onkeypress = function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("keypressed = " + key);
}

When I press the 'a' key on my keyboard, it logs as 97, which is different from the widely accepted keyCode of 65 found on the internet.

This discrepancy extends to other keys as well. For example, 's' logs as 115 for me, while it is commonly known to have a keyCode of 83.

Could there be a missing dependency causing this inconsistency? If I assume that a == 95 and fire an event, will it work on other browsers as well?

Thank you.

Answer №1

After conducting research, I have determined that the ASCII value for a capital A is 65.

Similarly, the ASCII value for a lowercase a is 97.

Take a look at this informative chart:

https://i.sstatic.net/voeVM.gif

To access the original chart, please visit:

Answer №2

Uppercase letters are distinct from lowercase letters and generate different codes.

Moreover, the keypress event behaves in a unique way compared to the keyup or keydown events. The keypress event responds to printable characters and provides the code of the character that was produced. In contrast, with keyup and keydown, the code corresponds to the physical hardware key on the keyboard that was pressed. For instance, if you execute the code snippet below and solely press the SHIFT key, you will not observe the keypress event log message because that event does not trigger for that particular key.

window.addEventListener("keyup", function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("key up = " + key, e.key);
});

window.addEventListener("keydown", function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("key down = " + key, e.key);
});

window.addEventListener("keypress", function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    console.log("key pressed = " + key, e.key);
});
Simply click on this area to give it focus, then press some keys.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When you hover over HTML tables, they dynamically rearrange or shift positions

Issue: I am encountering a problem with multiple tables where, upon hovering over a row, the tables are floating rather than remaining fixed in place. Additionally, when hovering over rows in the first or second table, the other tables start rendering unex ...

Using res.sendfile in a Node Express server and sending additional data along with the file

Can a Node.JS application redirect to an HTML file using the res.sendFile method from express and include JSON data in the process? ...

A guide on traversing a HTML table and cycling through its contents with JavaScript

I'm currently in the process of constructing a grid with 20 rows and 20 columns of squares, but I am encountering difficulties with looping through table values to effectively create the grid. For more detailed information on the html code, please se ...

Tips for updating the filename in a file input using AngularJS

Is it possible to dynamically change the name of a chosen file? For instance, if I select a file with the name "img1", can it be automatically changed to a different dynamic name upon selection? <input type="file" fd-input/> https://i.sstatic.net/d ...

JS backbone require global models in js

I am looking to implement a UserSession model that will handle loading and saving session IDs into cookies using the jQuery cookie plugin. Below is the code for my UserSession model module: define(['jQuery', 'Underscore', 'Backbo ...

Dealing with the challenges posed by middleware such as cookie access and memory leaks

Utilizing express with cookieParser() where my client possesses the cookie named: App.Debug.SourceMaps. I've crafted a middleware as follows: app.get('/embed/*/scripts/bundle-*.js', function(req, res, next) { if (req.cookies['App ...

What is the best way to display the angular bootstrap modal only upon the initial page load?

Even though I am only intending to show the bootstrap modal on page load, it is triggering every time the route changes. For example, if I return to the same page from another page using the browser's back or forward buttons, the modal is shown again. ...

Threejs: Illuminating the spotlight with visibility

For my current project, I am attempting to create a visible spotlight similar to the one used by Batman. I want that cone of light that pierces through the night sky. Unfortunately, I do not have any experience with graphics or 3D design, so I am strugglin ...

Explore a JSON array within another JSON array

Here is a JSON format that I have: { "Project [id=1, dateDebut=2017-01-13, dateFin=2017-01-18, description=qsd, sponsor=qsd ]" : [ {"id":1,"title":"qsd ","description":"qsdqsd","dateFin":"2017-01-26"}, {"id":2,"title":"sss ","descriptio ...

Iterating through the startPrivateConversation method in Botkit for a multitude of users

In order to send an update to all my users, I created a new bot controller in a separate js file. To achieve this successfully, I needed to implement a notification function inside the controller's rtm_open function. The goal was to iterate through th ...

Arranging Text and Images in HTML/CSS to Ensure Optimal Placement When the Window Size Changes

Hello fellow coders! I've recently started diving into the world of website development and I have a query regarding positioning elements and handling window resizing. Can anyone help me out? I'm trying to center an image, a message, and a passw ...

Issue - Following error occurred in the file connection.js located in node_modules/mysql2 library: Module not found: Unable to locate 'tls' module

I've encountered an error in our Next JS applications where tls is not found. I have tried several troubleshooting steps including: 1. Updating Node modules 2. Using both mysql and mysql2 3. Running npm cache clean --force 4. Removing node_modules di ...

Incorporating diverse objects in JavaScript through click events

In my Javascript code, I work with two arrays named "array1" and "array2". Initially, the currentArray is set to array1. However, when the user clicks on a certain button, the currentArray switches to array2, and then back to array1 if clicked again. Thi ...

JavaScript class name modifications are not functioning as expected

Testing a sprite sheet on a small web page I created. The code for each sprite in sprites.css is structured like this... .a320_0 { top: 0px; left: 0px; width: 60px; height: 64px; background: url("images/sprites.png") no-repeat -787 ...

Using Vue.js: loading a template within an asynchronous component

Recently delving into Vue.js, I am eager to set up a basic website using it. Each page (About Us, Contact Us, etc) has its corresponding component. Here is my functional starting point Vue.component('about-us', { template: '<div> ...

Identifying the presence of vertical scrolling

Is there a way to achieve the same functionality in JavaScript without using jQuery? I am looking to detect the visibility of the scrollbar. $(document).ready(function() { // Check if body height is higher than window height :) if ($("body").heigh ...

Compatibility of Typescript with local storage

Can Typescript interact with local storage in any way? I have been setting items using the following code: localStorage.setItem('someString', JSON.stringify(MyTypescriptObject)); This method stores the object as a plain string. For example: ...

Can Vuejs delay the calculation of a computed property until the component is "ready"?

Within my Vue.js application, I have a `computed` property that relies on a value fetched from an AJAX call. I am looking for a way to delay the calculation of this `computed` property until after the `ready` method has completed. While everything is fun ...

Start the jQuery animation only when the ajax information differs

I am using setInterval to create a timer that runs every 20 seconds. It utilizes jQuery's ajax function load() to populate the toparticles div with data from another URL on the site. After the data is successfully loaded, it applies a jQuery highlight ...

I require assistance in troubleshooting and repairing my HTML and JavaScript code

I am working on creating a feature where users can input their upcoming birthday date and receive an alert showing how many days are left until then. However, I encountered an issue where the alert displays NaN (Not a Number) before the birthday is entered ...