一个简单T9输入法的实现(有代码)

文章难度:初学者

关于T9输入法的核心程序(由我朋友Nix提供),共两个文件,一个是NixInputMethod.h,还有一个是T9.c; 参见附录。

具体输入法的实现方法如下:

1. 首先创建一个对话框,然后在其中放入两个控件:IDC_EDIT1 IDC_WORD STATIC控件)

2. 为了实现在Edit中一边输入,STATIC中会出现智能的功能,所以我们的实现如下:

a) 重载函数PreTranslateMessage ,使其能够捕捉到键盘的所有事件,故其实现如下:

BOOL CMy11Dlg::PreTranslateMessage(MSG* pMsg)

{

try {

CWnd* pCtrl = GetFocus();

/*

* 可以通过Enter建进行光标焦点的切换

*/

if (pMsg->message == WM_KEYDOWN && GetAsyncKeyState(VK_TAB))

{

// get the char index of the caret position

int nPos = LOWORD(m_MyEditCtl.CharFromPos(m_MyEditCtl.GetCaretPos()));

// select zero chars

m_MyEditCtl.SetSel(nPos, nPos);

// then replace that selection with a TAB

m_MyEditCtl.ReplaceSel("\t", TRUE);

// no need to do a msg translation, so quit.

// that way no further processing gets done

return TRUE;

} else

if (GetAsyncKeyState(VK_RETURN)) // Enter

{

switch(pCtrl->GetDlgCtrlID()) {

case IDOK:

if (!reset) {

AfxMessageBox("OK");

reset = 1;

} else {

reset = 0;

GetDlgItem(IDC_STATIC)->SetFocus();

}

break;

default:

pMsg->wParam = VK_TAB;

}

} else

/*

* 通过123....选择第几个字

*/

if (pMsg->message == WM_KEYDOWN && (isdigit(pMsg->wParam) || (pMsg->wParam >64 && pMsg->wParam < 91))) {

CString str;

if (isdigit(pMsg->wParam)) {

GetDlgItem(IDC_WORD)->GetWindowText(str);

char* temp = str.GetBuffer(100);

char zi[3];

zi[0] = temp[2*(pMsg->wParam - 48)-2];

zi[1] = temp[2*(pMsg->wParam - 48)-1];

zi[2] = '\0';

pCtrl->SetWindowText(zi);

str.ReleaseBuffer();

return TRUE;

}

UpdateData();

pCtrl->GetWindowText(str);

str += (pMsg->wParam + 32);

CShincoInputMethod pIME;

GetDlgItem(IDC_WORD)->SetWindowText(pIME.GetInputString((unsigned char*)str.GetBuffer(100)));

str.ReleaseBuffer();

}

return CDialog::PreTranslateMessage(pMsg);

} catch (...) {

// AfxMessageBox("Error");

}

return TRUE;

}

这样基本就可以实现一个输入法了,如果你加上词库的话,就可以实现类似智能ABC之类的东西了,当然那样的话,你还需要多努力努力!

上面程序只是测试用的,你自己可以重新试试看输入法的实现,如果需要代码,请联系我。

附录:

// NixInputMethod.h: interface for the CShincoInputMethod class.

//

//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SHINCOINPUTMETHOD_H__40148888_4731_4A49_87CC_4F9CFCAF77A5__INCLUDED_)

#define AFX_SHINCOINPUTMETHOD_H__40148888_4731_4A49_87CC_4F9CFCAF77A5__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

class CNixInputMethod

{

public:

unsigned long py_mb_begin;

CString GetInputString(BYTE input_py_val[]);

BYTE *py_ime(BYTE input_py_val[]);

CNixInputMethod();

virtual ~CNixInputMethod();

};

#endif // !defined(AFX_SHINCOINPUTMETHOD_H__40148888_4731_4A49_87CC_4F9CFCAF77A5__INCLUDED_)

/*

* T9.c

*/

#include "stdafx.h"

#include "NixInputMethod.h"

BYTE PY_mb_a[][255]=

{

{"阿啊吖锕嗄呵腌"},

{"哎哀唉埃挨皑癌矮蔼艾爱隘碍"},

{"安氨鞍俺岸按案胺暗"},

{"肮昂盎"},

{"凹敖熬翱袄傲奥澳懊"},

{"八巴叭扒吧芭疤捌笆拔跋把靶坝爸罢霸"},

{"白百佰柏摆败拜稗"},

{"扳班般颁斑搬板版办半伴扮拌绊瓣"},

{"邦帮梆绑榜膀蚌傍棒谤磅镑"},

{"包苞胞褒雹宝饱保堡报抱豹鲍暴爆剥薄瀑"},

{"卑杯悲碑北贝狈备背钡倍被惫焙辈"},

{"奔本苯笨夯"},

{"崩绷甭泵迸蹦"},

{"逼鼻比彼笔鄙币必毕闭庇毖陛毙敝痹蓖弊碧蔽壁避臂"},

{"边编鞭贬扁卞便变遍辨辩辫"},

{"彪标膘表"},

{"憋鳖别瘪"},

{"宾彬斌滨濒摈"},

{"冰兵丙秉柄炳饼并病"},

{"拨波玻钵脖菠播伯驳帛泊勃铂舶博渤搏箔膊卜"},

{"补哺捕不布步怖部埠簿"},

{"擦嚓礤"},

{"猜才材财裁采彩睬踩菜蔡"},

{"参餐残蚕惭惨灿"},

{"仓沧苍舱藏"},

{"操糙曹槽草"},

{"册侧厕测策"},

{"层蹭曾"},

{"叉插查茬茶搽察碴岔诧差刹"},

{"拆柴豺"},

{"掺搀谗馋缠蝉产铲阐颤"},

{"昌猖肠尝偿常厂场敞畅倡唱"},

{"抄钞超巢朝嘲潮吵炒绰"},

{"车扯彻掣撤澈"},

{"郴尘臣忱沉辰陈晨衬趁"},

{"称撑成呈承诚城乘惩程澄橙逞骋秤"},

{"吃痴弛池驰迟持尺侈齿耻斥赤炽翅"},

{"充冲虫崇宠"},

{"抽仇绸畴愁稠筹酬踌丑瞅臭"},

{"出初除厨滁锄雏橱躇础储楚处搐触矗畜"},

{""},

{"川穿传船椽喘串"},

{"闯疮窗床创"},

{"吹炊垂捶锤"},

{"春椿纯唇淳醇蠢"},

{""},

{"疵词茨瓷慈辞磁雌此次刺赐"},

{"囱从匆葱聪丛"},

{""},

{"粗促醋簇"},

{"蹿窜篡"},

{"崔催摧脆淬瘁粹翠"},

{"村存寸"},

{"搓磋撮挫措错"},

{"搭达答瘩打大"},

{"呆歹傣代带待怠殆贷袋逮戴"},

{"丹单担耽郸胆掸旦但诞弹惮淡蛋氮"},

{"当挡党荡档"},

{"刀导岛倒捣祷蹈到悼盗道稻"},

{"的地得德锝"},

{"灯登蹬等邓凳瞪"},

{"低堤滴狄迪敌涤笛嫡底抵地弟帝递第缔蒂"},

{"掂滇颠典点碘电佃甸店垫惦淀奠殿靛"},

{"刁叼凋碉雕吊钓掉"},

{"爹跌迭谍叠碟蝶"},

{"丁叮盯钉顶鼎订定锭"},

{""},

{"东冬董懂动冻侗恫栋洞"},

{"都兜斗抖陡豆逗痘"},

{"督毒读犊独堵赌睹妒杜肚度渡镀"},

你可能感兴趣的:(一个简单T9输入法的实现(有代码))