SadConsole icon indicating copy to clipboard operation
SadConsole copied to clipboard

Keyboard layout from OS not respected

Open MortenChristiansen opened this issue 7 years ago • 5 comments

I'm not sure if this has something to do with SadConsole, MonoGame or XNA, but my console does not use the keyboard layout that my OS uses, which is definitely the behavior I would expect. It seems to be either hard coded to American layout or determine the layout from my OS display language. I user Windows 10 with Danish keyboard layout and American UI language.

MortenChristiansen avatar Apr 10 '19 19:04 MortenChristiansen

Do you get a crash or anything like that? There is nothing I can really do about it. As this engine uses the classic 256 characters mapped to a graphic spritesheet. So when you push a character on your keyboard that ends up being number 65 in the spritesheet. So the look of that character is hardcoded. You would need to provide a new spritesheet that had danish characters placed in the correct position in the spritesheet to make it look like what you expect.

You can read more about it here: http://sadconsole.com/articles/basic-font-information.html

ghost avatar Apr 11 '19 15:04 ghost

Hah, I feel a bit stupid now. I actually already read that link, but I guess it didn't quite sink in.

Actually, I'm not that concerned about Danish letters (which appear as blank spaces), but what does concern me is that the American keyboard layout maps certain keys differently. For example Shift + . will result in > in SadConsole even though it should have been : for my keyboard layout.

Ideally, the console would automatically set the correct mappings for the symbols that do exist in the spritesheet. However, this is obviously a big task to do for all keyboard layouts. So my proposal is to make it as easy as possible for developers to do this themselves in a way that is configured globally for the console application.

Just off the top of my head, it could look something like this:

public class DanishKeyboardLayoutMapping : KeyboardLayoutMapping
{
    public override int Map(Keyboard info)
    {
        if(info.IsKeyPressed(Keys.OemPeridod) && info.IsKeyPressed(Keys.Shift))
            return 58; // The ASCII code for ':'
        ...
        return base.Map(info);
    }
}

Game.Instance.KeyboardLayoutMapping = new DanishKeyboardLayoutMapping();

If you won't be doing this (or have something similar which I have missed), could you give me a few pointers on how to best achieve this goal across the entire application?

MortenChristiansen avatar Apr 12 '19 08:04 MortenChristiansen

I tested it yesterday on my machine and I have a Swedish keyboard, the only problem I saw was Alt + Shift + 2 which always gave me "@".

This should be handled by the core window framework, I believe that MonoGame uses either KeysConverter or the Windows API, but that is only supported by Windows.

It could be that SadConsole is taking the wrong value on key press, but I couldn't see any problems on my machine so I didn't dig more.

erictuvesson avatar Apr 12 '19 08:04 erictuvesson

That is a tough thing to fit in. I dont think I want to do this with the current version, but something I can add for the next big version. Or if you want to do it, I would welcome a pull request to https://github.com/SadConsole/SadConsole/blob/master/src/SadConsole/Input/Keyboard.cs

MonoGame gives me the keys. As far as it is concerned, that is the key you pressed :)

Thraka avatar Apr 14 '19 02:04 Thraka

Thats fine 👍. If I choose to tackle this I will certainly consider it as a PR. In the end, I might just avoid the use of special characters, since there is no problem with normal text input.

MortenChristiansen avatar Apr 15 '19 07:04 MortenChristiansen

We've looked at this a lot over the last few years, and there simply isn't anything that MonoGame does to address this. If MonoGame adds this in the future, we can come back to it. FNA does do something with this, but it's not a target right now.

Thraka avatar Jul 09 '23 21:07 Thraka