[轉]為Linux下的Lazarus添加中文輸入支持


在Linux環境下,Lazarus不支持中文輸入。這是一個臭名昭著的Bug,其根源為Lazarus所使用的SynEdit的問題。
經過一番搜索,我在 Lazarus中文社區上找到了一個解決的辦法,記錄如下。
1. 我是用fpcupdeluxe安裝的Lazarus,打開/home/pi/fpcupdeluxe/lazarus/ide/sourceeditor.pp。
2. 在界面下方放下一個TPanel, 里面放下一個TLabel,一個TEdit,適當安排界面,如下圖。
3. 在源碼里面查找InsertCVSKeyword,會先找到這么一行:
procedure InsertCVSKeyword(const AKeyWord: string);
在這一行下面加上一行:
procedure InsertKeyword(const AKeyWord: string);
 
F3繼續找其實現,找到以下代碼:
procedure TSourceEditor.InsertCVSKeyword(const AKeyWord: string);
begin
  if ReadOnly then Exit;
 FEditor.InsertTextAtCaret('$'+AKeyWord+'$'+LineEnding);
end;
復制它們,在下面粘貼,改成:
procedure TSourceEditor.InsertKeyword(const AKeyWord: string);
begin
  if ReadOnly then Exit;
  FEditor.InsertTextAtCaret(AKeyWord);
end;
 

 

 

這樣,就給TSourceEditor增加了一個叫“InsertKeyword”的過程,作用是可以通過編程語句在光標所在位置添加指定字符串。
 
4. 界面上新加的Edit在onKeyPress,輸入以下語句:
 

 

procedure TSourceNotebook.Edit1KeyPress(Sender: TObject; var Key: char);
begin
  if key=#13 then
  begin
    GetActiveSE.InsertKeyword(Edit1.text);
    Edit1.Text:='';
    FocusEditor;
  end;
end;   

這樣,需要輸入漢字時轉到下面輸入,然后點一下按鈕,就自動添到上面源程序光標所在位置,並且將輸入焦點轉到上面源程序里面。

最后重新編譯Lazarus。

 

 

在樹梅派和銀河麒麟arm 64測試通過。

 

 2021-12-06更新:

更便捷的方法是在需要插入中文的位置按Ctrl+Enter鍵,修改方法如下:

打開SourceEditor.pp,在TSourceNotebook.FormKeyDown增加下面的代碼,然后重新編譯lazarus就可以。

 

procedure TSourceNotebook.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var _sIns:String;
begin
  if (ssCtrl in Shift) and (key=13) then
  begin
    key:=0;
    _sIns:=trim(InputBox('輸入待插入中文字符','',''));
    if _sIns<>'' then
    begin
      GetActiveSE.InsertKeyword(_sIns);
      FocusEditor;
    end;
  end;
end;

在需要的位置按Ctrl+Enter彈出輸入框:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM