My Code Docs

My Code Docs

  • Docs
  • Projects
  • Components
  • Help

›Javascript

Javascript

  • Promises and Asnyc/Await
  • Local Storage
  • Firebase
  • JS Language Basics
  • JS Array Functions
  • Keyboard and Mouse Input
  • ES6 Cheatsheet
  • ESLint Setup
  • Data Structures
  • Naming Conventions
  • Javascript Resources
  • Javascript Snippets
  • npm Module Creation

Node

  • Node JS Basics
  • Server Config and SSH

NextJS

  • NextJS Basics

JS/React Libs

  • Lodash Library
  • Axios
  • Ramda
  • Moment JS
  • Overmind
  • Redux Forms npm Module
  • React Beautiful DnD
  • Ant Design
  • Zustand State Managment

React

  • React Basics
  • React Hooks
  • React With Classes
  • Reach Router
  • React Router
  • React Components

Redux

  • Redux in React
  • My Redux Pattern
  • Redux with React (First Doc)
  • Simple Redux Package

ReactNative

  • React Native
  • React Navigation V5
  • React Navigation V4

HTMLCSS

  • HTML and CSS Snippets
  • Tailwind CSS

Electron

  • Electron React Boiler
  • Electron Basics
  • Electron Packaging
  • Electron Tooling

Keyboard and Mouse Input

Keyboard

keydown vs keypress

First things first, react has it's own syntetic events onKeyDown and onKeyPress that map to keydown and keypress respectivly.

Also, the event object passed to the callback when one of these events is triggered has different properties if you are using React's event or the browsers event listeners.

The other distinction to make is that the keypress event is fired when a key is pressed down and that key normally produces a character value. This means that the keypress will not give you keys like "escape", "alt", "ctrl", etc.

However keydown is fired when a key is pressed down. This is the guy to use if you need to get access to these no character producing keys.

Browser Event Listener

In the example below, we are setting up an event listener to fire on keydown and then in the callback checking for the esc key.

function handleKeyDown(event) {
    if ( event.keyCode === 27 ) {
        //Do something, the escape key was pressed
    }
}

document.addEventListener("keydown", handleKeyDown);

React Synthetic Events

React Docs on Events

← JS Array FunctionsES6 Cheatsheet →
  • Keyboard
    • keydown vs keypress
    • Browser Event Listener
    • React Synthetic Events
My Code Docs
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Facebook Open Source
Copyright © 2020 McCoidCo