SOLID Developper
SOLID Developper

Reputation: 816

How can I determine the codepage of the selected keyboard language in Win10?

I need for some reason the codepage of the language set by the currently selected keyboard layout in the current process. (I use Win10 with per app language settings)

getThreadLocale does not change when UI language changes. It gives back the default locale of the process.

getProcessInformation/getThreadInformation does not contain any information about the current language/locale.

I think the chain of the needed information is:

selected language => matching locale => codepage

if I have the current locale id (matching to the selected language) then I can fetch its codepage by:

getLocaleInfoW( idLocale, LOCALE_IDEFAULTANSICODEPAGE, buff, buffSize );

Is(Are) there any winapi call(s) to get the information described above?

Upvotes: 0

Views: 586

Answers (2)

The Bitman
The Bitman

Reputation: 1329

The TLabel caption sets to the CodePage associated with the current keyboard language by the TButton.OnClick event handler.

unit Unit3;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm3 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
var
  tid : word;
  lid : word;
  ndxLocale, buffSize : integer;
  localeName : string;
  buff : pchar;
begin
  tid := getCurrentThreadID;
  lid := getKeyboardLayout( tid );
  ndxLocale := languages.IndexOf( lid );
  localeName := languages.LocaleName[ndxLocale];
  buffSize := getLocaleInfoEx( pchar( localeName ), LOCALE_IDEFAULTANSICODEPAGE, NIL, 0 );
  getMem( buff, buffSize*sizeOf(char) );
  try
    getLocaleInfoEx( pchar( localeName ), LOCALE_IDEFAULTANSICODEPAGE, buff, buffSize );
    label1.caption := strPas( buff );
  finally
    freeMem( buff );
  end;
end;

Upvotes: 2

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

GetACP() returns "ansi" code page...lol, not really ansi, but that's what windows calls it. Can also use GetCPInfo() to get additional information after you call GetACP(). Things get trickier for Japanese, Chinese, and other far east languages that use double byte character set. I still work on an application that is MBCS. Would be nice if we could convert to Unicode, but it's not happening and it won't be my problem soon.

Upvotes: 0

Related Questions