MV Tried to make a little custom plugin: Identifying user's keyboard

Hey,

I wanted to make a event where you input certain keys, but since german keyboards have keys like "ö,ä,.." and I'm pretty sure not everyone has them, I wanted to make an alternative, where you can use the keys you have.

Here is the code which should have solved that:
Spoiler: Code
Code:
var usLayout = false;
navigator.keyboard.getLayoutMap().then(keyboardLayoutMap => {
var zKey = keyboardLayoutMap.get('KeyZ');
usLayout = zKey === 'z';
});
if(usLayout == true){
Input.keyMapper[186] = ';' }
else
{ Input.keyMapper[186] = 'ü' }

Someone in a german forum made it and it worked fine with his game.
When I tried, I got the error:

Spoiler: error
Unbenannt.png

Anyone got an idea where the problem may lie?

Thanks in advance!
he might get it to work because it's probably not the entire code.

as " getLayoutMap() " is a function that has more keys to it.

even if we dont have ö and ü on a qwerty keyboard, and the keyCode
of 186 is different, than we need the entire code where the function
is used as well.

without the entire function we cannot really help you.
if that is the entire code, than the function is inside a different plugin
they use, which we dont know.

so to help you, ask them or get the entire code in order to help you.
The Keyboard interface and its getLayoutMap() method are a part of JavaScript, but an experimental part not supported by all browsers, according to the documentation (and certainly not by RPG Maker's NW.js).

That user must have updated their NW.js to a specific release version that supported that API.
Oh ok.

Do you know of an alternative way to solve it then?

What I originally wanted to do is add the keys in the rpg_core.js like that:
Spoiler
01.png

But having 2 [186] keycodes seemed to make it not work anymore.
I assume that's Input.keyMapper you are modifying.

There's no point in having 2 entries for the key - you only need 1. You can call it call it anything in the world you want like '; and ü' ' or 'semicolon + umlaut u' or even something crazy like 'CheckKeyboardLaterToDetermineIfSemiColonOrUmlautLetter'.

KeyMapper is only called in 2 places in the code, which are Input._onKeyDown and Input._onKeyUp. If I were doing it, I would modify those functions to then check whether the user's keyboard is German or not when it detects the key being pressed is 186 and then assign the correct label at that time.

I.E. Something like:
JavaScript:
// ...somewhere in Input._onKeyDown or Input._onKeyUp
var buttonName = this.keyMapper[event.keyCode]; // this line is part of the default code
if (buttonName == 'CheckKeyboardLaterToDetermineIfSemiColonOrUmlautLetter') {
  var isGermanKeyboard = //code to check user keyboard
  if (isGermanKeyboard) {
    buttonName = 'ü';
  } else {
    buttonName = ';'
  }
}
// ...function continues

Though if you have many keys you need to differentiate, it's probably neater to write a whole new function to reassign labels, that you call when detecting a German keyboard rather than shoving it all in the _onKeyDown/_onKeyUp functions.
Hey, so that I unterstand the code somewhat bevor trying it.

if (buttonName == 'CheckKeyboardLaterToDetermineIfSemiColonOrUmlautLetter')
this is if i where to write: 186: 'CheckKeyboard....', // ü or ;

var isGermanKeyboard = //code to check user keyboard
does it automaticly detect keyboard layouts? it looks like you do a variable but I don't understand how it can check if its german oder english keybaord
You need to understand that the part of the code where you tried to "add the keys in the rpg_core.js" is a dictionary data structure called Input.keyMapper.

A "dictionary" is not a "function", it doesn't run any if statements or do any processing, it just stores data so that whenever it is asked for the "key" on the left, it spits out the "value" on the right. If you put 2 entries for the same key in it with different values, the later entry will just overwrite the earlier one.

Whenever the user presses a button down, the function Input._onKeyDown gets called (and when it is released, Input._onKeyUp get called). This function itself is already has all the information of the pressed button when it is called - it just checks to see whether the button's keycode exists in Input.keyMapper. This is the line that goes "var buttonName = this.keyMapper[event.keyCode]"*.

If it finds an entry, it fetches the value corresponding to the keycode in the keyMapper dictionary and then updates some other internal variables to remember it was pressed. Otherwise, if there's no entry for it in keyMapper, it basically just ignores the button press.

But keyMapper simply cannot store multiple values for the same key. So if you want the same keycode to give you different values based on user keyboard type, you need to modify _onKeyDown and _onKeyUp to check the user's keyboard type whenever a button that could have a different letter is pressed (or write a whole new function to do that, which is then called by _onKeyDown and _onKeyUp when it detects that button is pressed).

What I said you COULD do was basically make it so the entry for the button in Input.keyMapper is one value that just indicates "this key can have multiple values, please run code to check user keyboard and determine which one it is". And then, when _onKeyDown and _onKeyUp check it and receive that value, make it run the keyboard check and assign the correct letter based on the results.

But in truth, you don't actually need to touch keyMapper at all - you could also just not have an entry at all in keyMapper and directly modify _onKeyDown or _onKeyUp to perform the check every time it looks at Input.keyMapper and fails to find an entry for the keycode of a questionable button.

* This line of code means: check the "keyCode" of the button being pressed (all of the pressed button's data is in the "event" variable) and get the value associated with its entry from "keyMapper", and then assign the value of the entry to a new variable called "buttonName".

var isGermanKeyboard = //code to check user keyboard
does it automaticly detect keyboard layouts? it looks like you do a variable but I don't understand how it can check if its german oder english keybaord
No. You are meant to run your own function to check whether the keyboard is German and store the yes/no result in the variable.

The first code snippet that you posted was code meant for checking whether the keyboard was US or not, but tries to use the keyboard.getLayoutMap() function to check it, which as @ATT_Turan pointed out doesn't work with RMMV, only in certain web browsers.

So you will have to find some other way to check what kind of keyboard the user has.

If nobody has done this before, you will probably need to write your own code for it. While I've never tried it myself and can't say with certainty what will work, you may want to check whether "event.key" when running Input._onKeyDown or Input._onKeyUp gives you different letters for different keyboard types.

If it does, you can use that in your code to differentiate between keyboard types.

Original thread: https://forums.rpgmakerweb.com/threads/tried-to-make-a-little-custom-plugin-identifying-users-keyboard.157162/