- 注册时间
- 2005-5-3
- 最后登录
- 1970-1-1
|
转贴输入函数
//按键定义
#define KEY_PAGEUP 19
#define KEY_PAGEDOWN 14
#define KEY_LEFT 23
#define KEY_RIGHT 22
#define KEY_UP 20
#define KEY_DOWN 21
#define KEY_ENTER 13
#define KEY_SHIFT 18
#define KEY_CAPS 26
#define KEY_ESC 27
#define KEY_HELP 25
#define KEY_SPC 32
#define KEY_F1 28
#define KEY_F2 29
#define KEY_F3 30
#define KEY_F4 31
//相关常量
#define EDIT_LOW 0//小写
#define EDIT_UPP 1//大写
#define EDIT_ENG 0//英文(数字)
#define EDIT_CHN 1//中文
#define EDIT_INS 0//插入
#define EDIT_OVR 1//覆盖
#define FONT_SMA 0//小字体
#define FONT_LAR 1//大字体
//全局变量
//常用英文符号
char symbol[32]={'.',',',';','-','/','?','!','$','%',
'(',')',':','`','\'','\"','<','>','{',
'}','[',']','#','~','@','^','&','*',
'+','=','_','|','\\'};
char textBuf[50]={0};
char editCase,editLang,editMode;//编辑时的大小写、语言、模式(插入、覆盖)
//函数定义
int GetWord(char lastKey);//返回一个字符,取消输入返回0
int GetSymbol();//显示符号选择并返回,取消输入返回0
char Input(addr textAdd,char length,char textX, char textY, char font);//获取一个字符串保存到textAdd,取消输入返回0
//函数实现
int GetSymbol(){
char cho;
char key;
char i;
char index;
char sum;
index = 0;
sum = 31;
cho = 0;
key = 0;
do{
Block(1,66,159,78,0x40);
TextOut(2,67,"符",0x41);
Block(1,66,13,78,0x42);
memset(textBuf,0,20);
for(i=0;i<9;i++){
textBuf[i*2] = symbol[index+i];
textBuf[i*2+1] = ' ';
if (index+i>sum) break;
}
TextOut(52,67,textBuf,0x41);
key = getchar();
if (key == KEY_DOWN || key == KEY_RIGHT && index < sum - 9){
index= index +9;
}
if (key == KEY_UP || key == KEY_LEFT && index >= 9){
index = index -9;
}
if (key >= '1' && key <='9'){
if (key-'1'+index>sum){
continue;
}else{
return symbol[key-'1'+index];
}
}
if (key == KEY_ESC){
return 0;
}
}while(TRUE);
}
int GetWord(char lastKey){
char key;
char charSum,charID;
char pyCode[10];
char pyPt;
char charCode[20];
long codeRight;
int wideChar;
key = lastKey;
memset(pyCode,0,10);
memset(charCode,0,20);
charSum = 0;
charID = 0;
pyPt = 0;
do{
if (key == KEY_SHIFT){//切换输入语言
if (editLang == EDIT_ENG){
editLang = EDIT_CHN;
Block(1,66,159,78,0x40);
TextOut(2,67,"拼",0x41);
Block(1,66,13,78,0x42);
memset(pyCode,0,10);
memset(charCode,0,20);
charSum = 0;
charID = 0;
pyPt = 0;
}
else{
editLang = EDIT_ENG;
Block(1,66,159,78,0x40);
TextOut(2,67,"英",0x41);
Block(1,66,25,78,0x42);
}
}//切换大小写
if (key == KEY_CAPS){
if (editCase == EDIT_UPP){
editCase = EDIT_LOW;
}
else{
editCase = EDIT_UPP;
}
}
if (editLang == EDIT_ENG){//英文数字输入
if (key >= 'a' && key <= 'z'){
if (editCase == EDIT_UPP){
return key - 32;
}else{
return key;
}
}else if (key >= '0' && key <= '9'){
return key;
}else if (key == KEY_SPC){//空格
return key;
}else if (key == '.'){//.
return key;
}else if (key == '-'){//输入符号
return GetSymbol();
}else{
return 0;
}
}else if (editLang == EDIT_CHN){//中文输入
if ((key == KEY_LEFT || key == KEY_F2) && pyPt > 0){//删除最后一个拼音
pyCode[--pyPt] = 0;
charID = 0;
charSum = 0;
}else if (key == KEY_DOWN && charID + 9 < charSum ){//下翻页
charID = charID + 9;
}else if (key == KEY_UP && charID > 9){//上翻页
charID = charID - 9;
}else if (key >= 'a' && key <= 'z' && pyPt < 6){//输入拼音
pyCode[pyPt++] = key;
pyCode[pyPt] = 0;
charID = 0;
charSum = 0;
}else if (key >= '1' && key <= '9'){//选字
wideChar = charCode[(key - '1') * 2 + 1];
wideChar = (wideChar << 8) + charCode[(key - '1') * 2];
if (wideChar != 0){
return wideChar;
}
}
Block(15,67,159,79,0x40);
TextOut(15,67,pyCode,0x41);
memset(charCode,0,20);
codeRight = System.PY2GB(charID,pyCode,charCode);
if (codeRight != -1){
charSum = codeRight >> 16;
TextOut(52,67,charCode,0x41);
}
}
key = getchar();
}while (key != KEY_ESC)
return 0;
}
char Input(addr textAdd,char length,char textX, char textY, char font)
{
char strPt;//字串指针、长度
char key;
char curX,curY,curL;//光标坐标、长度
int word;
strPt = 0;
key = 0;
if (font == FONT_SMA){
curL = 6;
curY = textY + 11;
}
else{
curL = 8;
curY = textY + 15;
}
while (TRUE)
{
Refresh();
TextOut(textX,textY,textAdd,0x40+0x80 * font);//显示当前字串
curX = textX + strPt * curL;
if (editMode == EDIT_INS){//绘制光标
if (*(textAdd+strPt) >= 0xa1){
Line(curX,curY,curX + curL * 2 - 1,curY,2);
}else{
Line(curX,curY,curX + curL - 1,curY,2);
}
}else{
if (*(textAdd+strPt) >= 0xa1){
Box(curX,curY - curL * 2,curX + curL * 2 - 1,curY,1,2);
}else{
Box(curX,curY - curL * 2,curX + curL - 1,curY,1,2);
}
}
if (editLang == EDIT_CHN){
Block(1,66,159,78,0x40);
TextOut(2,67,"拼",0x41);
Block(1,66,13,78,0x42);
}
else{
Block(1,66,159,78,0x40);
TextOut(2,67,"英数",0x41);
Block(1,66,25,78,0x42);
}
key = getchar();
if (key == KEY_ENTER || key == KEY_ESC){
break;
}
if (key == KEY_F1){//切换插入、覆盖
if (editMode == EDIT_INS){
editMode = EDIT_OVR;
}else{
editMode = EDIT_INS;
}
continue;
}
if (key == KEY_LEFT){//左移光标
if (strPt > 0){
if (*(textAdd+strPt-1) >= 0xa1){
strPt = strPt - 2;
}else{
strPt--;
}
}
continue;
}
if (key == KEY_RIGHT){//右移光标
if (strPt < strlen(textAdd)){
if (*(textAdd+strPt) >= 0xa1){
strPt = strPt + 2;
}else{
strPt++;
}
}
continue;
}
if (key == KEY_F2){//删除字符
if (*(textAdd+strPt) == NULL){
if (strPt > 0){
if (*(textAdd + strPt - 1) >= 0xa1){
strPt = strPt - 2;
}else{
strPt--;
}
}
}
if (*(textAdd+strPt) >= 0xa1){
strcpy(textBuf,textAdd + strPt + 2);
}else if (*(textAdd+strPt) != NULL){
strcpy(textBuf,textAdd + strPt + 1);
}
strcpy(textAdd + strPt,textBuf);
continue;
}
//加入字符
word = GetWord(key);
if (word == 0){
continue;
}
if (editMode == EDIT_INS || *(textAdd+strPt) == NULL){//插入模式,或是光标在最后为新加的字符腾出空间
if (word >> 8 != 0 && strlen(textAdd) >= length - 1){//判断长度是否超过规定长度
continue;
}
if (word >> 8 == 0 && strlen(textAdd) >= length){
continue;
}
strcpy(textBuf,textAdd + strPt);
if (word >> 8 != 0){
strcpy(textAdd + strPt + 2,textBuf);
}else{
strcpy(textAdd + strPt + 1,textBuf);
}
}
if (word >> 8 != 0){//汉字
if (*(textAdd+strPt) < 0xA1 && editMode == EDIT_OVR){//覆盖英文或数字
if (strlen(textAdd) >= length) continue;
strcpy(textBuf,textAdd + strPt);
strcpy(textAdd + strPt + 1,textBuf);
}
*(textAdd+strPt)=word & 0xff;
*(textAdd+strPt+1)=word >> 8;
strPt=strPt+2;
}else{//英文或数字
if (strlen(textAdd) >=length && *(textAdd+strPt) == NULL) continue;
if (*(textAdd+strPt)>=0xA1 && editMode == EDIT_OVR){//覆盖汉字
strcpy(textBuf,textAdd + strPt + 1);
strcpy(textAdd + strPt,textBuf);
}
*(textAdd+strPt)=word & 0xff; strPt++;
}
}
if (key == KEY_ESC) return 0;
return !0;
}
//测试函数
void main(){
char text[100];
Input(text,20,0,0,FONT_SMA);
}
请问各位高手,能否解释红色部分的意思吗?对于那个word&0xff 也弄不明白,为何要加&0xff?
[em11][em11][em11] |
|