DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Back to Basics – Using Keyboard Events in JavaScript

Back to Basics – Using Keyboard Events in JavaScript

Gil Fink user avatar by
Gil Fink
·
Sep. 17, 12 · Interview
Like (0)
Save
Tweet
Share
49.73K Views

Join the DZone community and get the full member experience.

Join For Free

A few months ago I was implementing a POC for a customer and was in a need for reading keyboard presses through JavaScript. I was asked the same question today so I guess that means a post.

The Scenario

You need to read all the keyboard key presses up until the Enter key is used.
You should output the typed word without the Enter key.

Wiring The Events

In order to listen to key presses, you will wire an event listener to the keypress event. That event listen to all key presses and in its handler you will store the key pressed (but won’t store the Enter key press). Here is an example for doing that:

window.addEventListener('keypress', function (e) {
    if (e.keyCode !== 13) {
        chars.push(e.key);
    }
}, false);

In the example, you wire the keypress event. If the key press isn’t Enter (Enter key code is 13), you push the pressed key to a character array.

In order to catch the Enter key press and output the typed word, you will wire an event listener to the keyup event. That event listen to the end of key pressing (when the user stop pressing a key). Here is an example for wiring the event:

window.addEventListener('keyup', function (e) {
    if (e.keyCode === 13) {
        container.textContent = chars.join('');
        chars = [];
    }
}, false);

In the example, you wire the keyup event. If the key is released, you check whether the pressed key was Enter. If the key was Enter, you put the content of the characters array into a container div and you clear the array.

The Full Example

Here is all the code for the example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Keyboard Reading</title>

    <script type="text/javascript">
    (function () {
        function contentLoaded () {    
                var chars = [],
                container = document.getElementById('container');

                window.addEventListener('keypress', function (e) {
                if (e.keyCode !== 13) {
                            chars.push(e.key);
                }
                }, false);

                window.addEventListener('keyup', function (e) {
                    if (e.keyCode === 13) {
                        container.textContent = chars.join('');
                chars = [];
                       }
                }, false);
        }

        window.addEventListener('DOMContentLoaded', contentLoaded, false); 
    }());
    </script>
</head>
<body>
    <div id="container">
    </div>
</body>
</html>

Summary

In order to read keyboard pressed keys in JavaScript you can use events such as keypress and keyup. The example that I showed is very basic but it was the starting point for reading data sent from a barcode reader that was acting like a keyboard in the POC I mentioned.

Event JavaScript

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Three SQL Keywords in QuestDB for Finding Missing Data
  • Automated Performance Testing With ArgoCD and Iter8
  • Why Every Fintech Company Needs DevOps
  • Cloud Native London Meetup: 3 Pitfalls Everyone Should Avoid With Cloud Data

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: