Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keyboard language component #514

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

jawee
Copy link

@jawee jawee commented Jan 20, 2024

Component for showing current input language (keyboard layout).

@jawee jawee changed the title Feat/keyboard language component Jan 20, 2024
@Video-Nomad
Copy link
Contributor

Works for en-US and ja-JP but not for Ukrainian for some reason. It just returns an empty string.
Locale ID shows 61608 and LCIDToLocaleName returns an empty string for Ukrainian.

@jawee
Copy link
Author

jawee commented Jan 20, 2024

And ukrainian is supposed to be 1058 (0x422) according to https://www.ryadel.com/en/microsoft-windows-lcid-list-decimal-and-hex-all-locale-codes-ids/

For some reason the value from GetKeyboardLayout doesn't follow the same "format" as swedish and us that I tested with. Will try to figure it out.

uk-UA (0x422): 0xfffffffff0a80422;
sv-SE (0x41d): 0x00000000041d0809;
en-US (0x409): 0x0000000004092000;

@jawee
Copy link
Author

jawee commented Jan 23, 2024

Not sure if this will work for every possible language. But added a few at random, and those I tried seem to work fine with this solution. Feels a bit hacky, but seems to be working pretty good.

@Video-Nomad
Copy link
Contributor

Video-Nomad commented Jan 23, 2024

@jawee I actually just wanted to post another solution right before your commit. Worked for me so far. Haven't tested it extensively though.

  private const uint LOCALE_SNAME = 0x0000005c;

  private static string GetInputLanguage()
  {
    var layout = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero));

    var sb = new StringBuilder();
    GetLocaleInfoW((uint)layout & 0xFFFF, LOCALE_SNAME, sb, sb.Capacity);

    return sb.ToString();
  }
    [DllImport("kernel32.dll")]
    public static extern IntPtr GetLocaleInfoW(uint locale, uint LCType, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpLCData, int cchData);
@Video-Nomad
Copy link
Contributor

It also might be a good idea to expose the updateInterval for the Observable as a config option, similar to how CpuComponent handles it. 1 second is way too slow for me, and 250 milliseconds feels more responsive, but people might want to configure that themselves.

@jawee
Copy link
Author

jawee commented Jan 24, 2024

Your solution looks cleaner in my opinion, I'm open to using that. I'll try to do some more testing sometime within the next few days.

Had a few minutes before having to start work, so I made the refresh interval configureable, I defaulted it to 1 second though, but open to change that to 250ms as well if that's preferable. Once a second works fine for my use case :)

@Video-Nomad
Copy link
Contributor

Video-Nomad commented Jan 24, 2024

I feel like 1 second is a reasonable default value.
Feel free to update the code with the solution I provided if you find it acceptable.
Thanks for the component 👍

@jawee
Copy link
Author

jawee commented Jan 26, 2024

Did some more testing, with more languages, and my solution seems fine for the all languages I've tried.
I also tried your solution, but it didn't work for swedish, so I think my solution is a decent starting point for now. :)

@Video-Nomad
Copy link
Contributor

@jawee Very strange. I tested it on Swedish and it worked on my end.

4WRXh6ps1h

In any case, if it works it works 👍

@jawee
Copy link
Author

jawee commented Feb 1, 2024

Probably because I use english language, but swedish keyboard layout. :) (probably really weird..)

image

@jawee jawee mentioned this pull request Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants