讓HGE支持中文(2) - HGE中文輸入

 

 狂鬱悶ing...

昨天寫的東西發出來竟然沒有文字...重新編寫的時候也沒有看到文字...

哎狂倒啊,浪費我寫了那麼久.

今天還得重來...日..這個CSDN怎麼搞的噢?

算了.直接說怎麼搞了,打字累...

因爲我所說的方法是直接改HGE的內核..

所以改動的地方不多,但也不是幾行代碼就行了..

步驟是:

1:在 hge.h 中添加結構,用於保存我們的輸入數據.

struct hgeIMEEvent
{
    
bool    isIME;
    
char    value[3];
};

2:在 hge.h 中添加輸入數據獲取接口:

virtual bool        CALL    Input_GetIMEEvent(hgeIMEEvent *ime) = 0;

3:接下來就需要一個表來存目前輸入的數據了.我們採用HGE通用風格,在 hge_impl.h 中添加結構:

struct CIMEEventList
{
    hgeIMEEvent            ime;
    CIMEEventList
*        next;
};

4:完成接口申明.在 hge_impl.h 中添加接口:

virtual bool        CALL    Input_GetIMEEvent(hgeIMEEvent *ime);

5:在 hge_impl.h 中添加數據來源及處理了.所有即時輸入的數據都將保存在這裏處理,但不通知你.

    CIMEEventList*        imeEvent;
    
void                _BulidIMEEvent(char key[3], bool isIME);
    
void                _ClearIMEEvent();

6:完成上面幾個接口的函數體.在 input.cpp 中添加

(歡迎轉載:原創作者BLOG(ShowLong))

void HGE_Impl::_BulidIMEEvent(char key[3], bool isIME)
{
 CIMEEventList* newEvent = new CIMEEventList;
 newEvent->next = 0;
 newEvent->ime.isIME = isIME;
 if (isIME)
 {
  newEvent->ime.value[0] = key[0];
  newEvent->ime.value[1] = key[1];
  newEvent->ime.value[2] = '/0';
 }
 else
 {
  newEvent->ime.value[0] = key[0];
  newEvent->ime.value[1] = '/0';
  newEvent->ime.value[2] = '/0';
 }
 if (!imeEvent)
  imeEvent = newEvent;
 else
 {
  CIMEEventList* last = imeEvent;
  while (last->next)
   last = last->next;
  last->next = newEvent;
 }
}
bool HGE_Impl::Input_GetIMEEvent(hgeIMEEvent *ime)
{
 CIMEEventList *eptr;
 if (imeEvent)
 {
  eptr = imeEvent;
  memcpy(ime, &eptr->ime, sizeof(hgeIMEEvent));
  imeEvent = eptr->next;
  delete eptr;
  return true;
 }
 return false;
}
void HGE_Impl::_ClearIMEEvent()
{
 CIMEEventList* next,* ptr = imeEvent;
 while (ptr)
 {
  next = ptr->next;
  delete ptr;
  ptr = next;
 }
 imeEvent = 0;
}

7:查找所有調用 _ClearQueue(); 的地方.然後在其下面加上代碼:

_ClearIMEEvent();

8:在 system.cpp 中找到 System_Start 函數體.將被注消掉的代碼啓動.下面:

TranslateMessage(&msg);

9:不要忘了在構造 hge_impl 的時候寫上這樣一句:

imeEvent = 0;

10:這就是關鍵了. 有了上面的代碼.就可以做怎麼獲取輸入了.

在主 WindowProc 函數體的 case WM_SETCURSOR: 的下一個case 前插入代碼:

  case WM_IME_CHAR:
   {
    char imeChar[3];
    imeChar[0] = (char)(wparam>>8);
    imeChar[1] = (char)wparam;
    imeChar[2] = '/0';
    pHGE->_BulidIMEEvent(imeChar, true);
   }
   break;
  case WM_CHAR:
   {
    if (wparam == VK_BACK ||
     wparam == VK_TAB ||
     wparam == VK_RETURN ||
     (char)wparam <= 0)
     break;
    char imeChar[3];
    imeChar[0] = (char)wparam;
    imeChar[1] = '/0';
    imeChar[2] = '/0';
    pHGE->_BulidIMEEvent(imeChar, false);
   }
   break;

至此.所有代碼就添加完成了...

如果想製作一個GUI來調用一下試試呢..行的.

只需要三句代碼.

        hgeIMEEvent    imeEvent;
        
while (tKernelServer::Instance()->Device()->Input_GetIMEEvent(&imeEvent))
        {
            AddChar(imeEvent.value, imeEvent.isIME);
        }

夠簡單吧.至於爲什麼imeEvent要這樣定義..這裏暫不做解釋...如果你覺得不需要可以改的.

如果還有問題..只好來問我QQ:29774874.

或加到我的羣裏來:32800303

發佈了67 篇原創文章 · 獲贊 6 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章