

On one hand, the value of event.key is a character, it changes depending on the language. There’s a dilemma here: in such a listener, should we check the value of event.key or de? We can set a listener on keydown and check which key is pressed. Most text editors hook the “Undo” action on it. Let’s say, we want to handle a hotkey: Ctrl +Z (or Cmd +Z for Mac). The de tells us exactly which one was pressed, and event.key is responsible for the “meaning” of the key: what it is (a “Shift”). For instance, most keyboards have two Shift keys: on the left and on the right side. Please note that de specifies exactly which key is pressed. For those keys, event.key is approximately the same as de: Key What if a key does not give any character? For instance, Shift or F1 or others. The check like de="keyZ" won’t work: the first letter of "Key" must be uppercase. Please evade mistypes: it’s KeyZ, not keyZ. Seems obvious, but people still make mistakes. That will become the value of event.key, while de is always the same: "KeyZ". If a user works with different languages, then switching to another language would make a totally different character instead of "Z". The event.key is exactly the character, and it will be different. That gives us two different characters: lowercase z and uppercase Z.
ADDEVENTLISTENER KEYUP CODE
The key property of the event object allows to get the character, while the code property of the event object allows to get the “physical key code”.įor instance, the same key Z can be pressed with or without Shift.

The keydown events happens when a key is pressed down, and then keyup – when it’s released. Window.Focus on the input field and press a key. tSize( window.innerWidth, window.innerHeight ) ĭ( renderer.domElement ) Ĭonst white = new THREE.Color( 0xffffff ) Ĭonst grey = new THREE.Color( 0xdddddd ) Ĭonst geometry = new THREE.BoxGeometry() Ĭonst material = new THREE.MeshStandardMaterial() Ĭonst cube = new THREE.Mesh( geometry, material ) Ĭonst directionalLight = new THREE.DirectionalLight( 0xffffff ) Ĭ(state.cubeColor) I passed in a config object to the WebGLRenderer constructor setting the alpha transparency to true: const renderer = new THREE.WebGLRenderer() // adds canvas transparency I wanted to have the canvas transparent so I could just set the background color of the page. const white = new THREE.Color( 0xffffff ) // hexadecimalĬonst white = new THREE.Color('rgb(255,255,255)') // RGB stringĬonst white = new THREE.Color(1, 1, 1) // Separate RGB values between 0 and 1 Changing scene background color There are different color formats you can pass into the constructor but hexadecimal is recommended. Here’s the Color constructor: Color( r : Color_Hex_or_String, g : Float, b : Float ) Using the creating a scene introduction code as a starter, I initialize a few colors: const white = new THREE.Color( 0xffffff ) Ĭonst black = new THREE.Color( 0x000000 ) Ĭonst green = new THREE.Color( 0x00ff00 ) Ĭonst blue = new THREE.Color( 0x0000ff )
ADDEVENTLISTENER KEYUP HOW TO
After seeing how to create and render a scene and then do a simple cube animation I wanted to explore and learn a little more of how to use color in Three.js. I found this creating a scene introduction in the docs and decided to start there. A few weeks back I wanted to dig into learning WebGL with Three.js and started poking through the site to see if I could start with an example to get things rolling.
