我正在使用LoadKeyboardLayout函数以这种方式加载和激活键盘布局:
procedure TfrmMain.eSearchEnter(Sender: TObject);
begin
LoadKeyboardLayout('00000429', KLF_ACTIVATE);
end;
它工作得很好,但它会冻结活动表单 1-2 秒,因为此更改需要一些时间。为了防止这种情况,我已将此代码移至这样的后台线程:
type
FLangChangeThread = class(TThread)
private
FLang: string;
protected
procedure Execute; override;
public
property Lang: string read FLang write FLang;
end;
implementation
procedure FLangChangeThread.Execute;
begin
if FLang = 'EN' then
LoadKeyboardLayout('00000409', KLF_ACTIVATE)
else
if FLang = 'FA' then
LoadKeyboardLayout('00000429', KLF_ACTIVATE);
end;
然后我以这种方式运行这个后台线程:
procedure TfrmMain.ChangeWritingLanguage(ALang: string);
begin
with FLangChangeThread.Create(True) do
begin
FreeOnTerminate := True;
Lang := ALang;
Resume;
end;
end;
procedure TfrmMain.eSearchEnter(Sender: TObject);
begin
ChangeWritingLanguage('FA');
end;
问题是,它没有按预期改变键盘布局。我已经调试了代码并且所有的行都被执行了;只是LoadKeyboardLayout功能还没有发挥作用。
如何使LoadKeyboardLayout函数从后台线程工作?