Canvas input keyboard events and input in general
When working out a javaScript project canvas keyboard events might sometimes need to be used with mouse and touch events when working out a user interface. Of course these days touch and mouse events should always be used first and foremost when working out an interface, however keyboard support would still be a nice addition for some projects. There are certain types of games that just translate better as a so called desktop application, with these kinds of projects I still make sure that I can be used on a mobile phone, bit of course I am going to want to add keyboard support for it.
Keyboard events in client side javaScript involve attaching event handlers to the window object, because these kinds of events have to do with key presses in general, and not just touch events that have to do with just a canvas element.
There are two main events of interest the keydown and keyup events, the names of which should speak for themselves. I often use the key down event to set a boolean value to true, and then the key up to set it back to false, then use those boolean values to change things in an update loop. Another approach would be to make something that is more event driven with or without an update loop.
There is also using keyboard events in conjunction with rather than a replacement for touch events, and working out a general system for capturing use input. So lets look at some examples of canvas keyboard events, and input in general with canvas and javaScript.
1 - The basics of Canvas keyboard events
The basic idea of canvas and keyboard events is that one or more event listeners should be attached to the window object using something like the add event listener method of the window object. When doing so I use the key down or key up event types, and attach a handler method where I define some logic as to what to do when such an event occurs. In this section I will be going over a few basic examples that should work out okay as starting points for canvas and keyboard events.
1.1 - canvas keyboard example with key up and key down events
So here I have a basic copy and past example where the html and javaScript are all wrapped up together. The result of this is a canvas example where the keydown property of a very basic state object is displayed in the center of the canvas. When any key is pressed the value of the keydown state property is set true and the canvas is redrawn. When any key is released the value of the keydown state object property is set back to false, and the canvas is redrawn yet again.
|
|
Very basic and crude, and there is a lot more that needs to be done until I have something that can be called an actual project of some kind. However the basic idea of having something happen when a key is pressed, and something else when it is released is there. This can also be thought of as an event driven kind of example, where user interaction is the only driving force for change of the application state, however getting into the more would be off topic.
Now that we have the basic idea of keydown and keyup events out of the way lest take a look at some of the properties of interest in the event object of a keyboard event.
1.2 - The keyCode and key properties of a keyboard event object
In the body of the event handler as with just about any other event hander the first argument given is an event object. This event object has many properties that are the same as with any other event object in client side javaScript, however there are some properties that are unique to keyboard events. The main properties of interest with keyboard events might be the key and keyCode properties.
Here I have some javaScript that is not all that also just displayed the current values of a state object in a canvas element. However now I am not just displaying if the key is down or up, but also the current key that was pressed.
|
|
2 - Multi key canvas keyboard input
One way to get going with multi key input with client side javaScript I just need to have an array of boolean variables for each key. I can then have an event handler that will set the boolean value for a key to true if it is a key down event, and false if it is a key up event. This array of boolean values could then be used to update the state of an object in different ways depending on the combination of keys pressed.
|
|
3 - Starting a comprehensive keyboard, mouse and touch control module
In many projects I might want to use some kind of javaScript module or framework that abstracts away all of this stuff when it comes to keyboard events, as well as mouse and touch events. Many popular game and front end frameworks do just that of course. However the appeal of using vanilla javaScript is to have control, over well, control. So with that said in this section I will be going over a module that is the beginnings of a module that handles input in general from keys, and pointer events and presets it as a standard object containing a key code array, and a single pointer position object.
3.1 - Control.js
For this kind of project I started out with a main control.js file that creates a single global variable and returns a single public function to it from within an IIFE. Inside the body of the IIFE I have some private methods including a get canvas relative method that I worked out in another post that I made after starting this post. This method just helps with getting a point that is relative to the upper left corner of the canvas rather than the window of the browser.
I then Have additional helpers that are used to create an input state object, as well as attach event handers to the canvas that is given when calling the main public method that is returned.
|
|
When using this module as expected the result is an object that can be used in the body of an update method to know the current status of things when it comes to keys, and a general pointer object that can be updated by mouse or touch events.
3.2 - trying this out with draw.js, main.js and some html
Now it is time to try out my control.js module to see if things work as expected.
|
|
|
|
|
|
4 - Conclusion
Although pointer events are what I should always start out with some projects just work better with keyboard input, or some kind of game pad input. Keyboard events can of course be used in conjunction with mouse events, touch events, and javaScript code that automates input to create a comprehensive, over all way to control the manipulation of state. If interested in reading more about input that is closly realted to canvas projects you might want to check out my canvas example post in which I make a general input controller.