You cannot get a keyboard layout ...
There is no way (with a pure JS solution) to detect a userโs keyboard layout.
Please note that "en-US" or "en-GB" does not match any specific / consistent keyboard layout for these languages. The user's language does not depend on how their keys are mapped to a character.
You can make assumptions based on their language / language code , but this does not give any guarantee - it simply increases the likelihood that you are eating the appropriate layout.
Also see the following questions:
Convert Javascript keyCode to charCode for keyboard layout other than US ( )
Detecting keyboard layout using javascript
... but you can get the entered character:
However, depending on your required browser support (which uses the canvas, it should be nice), you can try using the charCode property:
http://www.w3schools.com/jsref/event_key_charcode.asp
var unicodeCharCode = e.charCode;
Please note that this will not return the actual character - you will get a number representing it (NOT a key):
The Unicode character code is the character number (for example, the number "97" denotes the letter "a").
You can easily match these codes with characters as needed, or simply use the built-in function:
var userInput = String.fromCharCode(unicodeCharCode);
- To be smart / do the work for yourself, you could (quite trivially) write a library to guess the keyboard layout from the charCode returned for a particular keyCode. Not worth it :)
Alternative approach:
One suggestion for using as a workaround would be to use a hidden (or at least discrete) text input / text field that your application focuses on.
When your application detects a keystroke / keydown, etc., you can capture the value entered by the user and display it on your canvas.
Since you are already dealing with keyboard events, handling combinations should be easy (if necessary). In some cases, even this is not required, since you only need to monitor the input length of the user input (your application does not need to worry about whether the user needs to use a combination of three keys to enter a character, you just capture the result).