实现命令行输入及键盘消息处理
2012-09-22 01:06
357 查看
// StringTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" // string::empty // string::clear #include <conio.h> #include <iostream> #include <string> using namespace std; void ShowHelp() { printf("==========Help Infomation============\n"); printf(">"); } int main (char * argv[],int argc) { string str = ""; char ch = 0; cout << "Please type some lines of text. Enter a period to finish\n>"; do { ch=getch(); str += ch; printf("%c",ch); //处理按键 switch(ch) { case 8://处理 Backspace 键 //cout << endl << "size=" << str.size() << endl; if (str.size() == 1) //str中不存储退格键,置空 { str = ""; } if (str.size() >= 2)//空格+将要删除的字符,所以至少要有两个字符 { string strTemp = ""; int iCnt=0; strTemp = str.erase(str.size()-2);//删除倒数第二个字符,倒数第一个字符是'\0',不能删除 printf("\r>"); while(1) { printf(" "); if (iCnt++ > str.size()) { break; } } printf("\r>%s",strTemp.c_str()); } continue; break; case 3://处理ctrl+c键 printf("\n<Ctrl>+<c>\n"); exit(-1); case 4://处理ctrl+d键 printf("\n<Ctrl>+<d>\n"); exit(-1); break; default: //printf("%d",ch); break; } //printf("\n%d",ch); if ( ('?' == ch) && (1 == str.size()) ) { printf("\n"); ShowHelp(); str = ""; continue; } if (ch == 13) { string strTemp = str; //cout << endl << str << endl; //cout << "str.size()=" << str.size() << endl; cout << endl << str ; str = ""; //只有空格一个字符,不处理 // if (1 == strTemp.size()) // { // //cout << ">"; // } //除了空格还有一个字符 if (2 == strTemp.size()) { char cTemp = strTemp.at(0); switch(cTemp) { case 1: cout << "====1======" << endl; break; default: cout << "====" << cTemp << endl; break; } } //两个或两个以上字符处理 if ( 3 <= strTemp.size()) { cout << endl; //.... } cout << ">"; //str.clear();//string 没有clear()成员函数? } } while (1); return 0; }
相关文章推荐
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 请教一个KEYDOWN的处理,想实现输入符A,但面界面上出现B(想实现在英文键盘输入其它语言的字符),重载了CEdit,但不会没出现我要的效果?
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- 游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- [原]【Visual C++】游戏开发笔记十二 游戏输入消息处理(一) 键盘消息处理
- MFC 菜单处理及消息截获(利用虚函数的特性实现)
- (转)Android高仿微信表情输入与键盘输入(详细实现分析)
- 第二人生的源码分析(二十三)人物行走的键盘消息处理
- 【Visual C++】游戏开发笔记十三 游戏输入消息处理(二) 鼠标消息处理 (转)
- 使用ImessageFilter接口实现截获键盘或者鼠标的消息
- 使用JS实现的可变动态密码输入键盘控件源代码
- java 键盘输入的多种实现方法
- Mailbox:六星期实现从零到百万用户及日处理亿条消息