求高手指教

xuanxuan683 2011-12-19 03:08:12
如何用C语言程序来获取来自键盘的消息,并对其进行处理,各位大神帮帮忙啊
...全文
98 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-12-20
  • 打赏
  • 举报
回复
//The _getch function reads a single character from the console without echoing.
//Function can not be used to read CTRL+Break.
//When reading a function key or an arrow key,
//_getch must be called twice; the first call returns 0 or 0xE0,
//and the second call returns the actual key code.
#include <conio.h>
#include <windows.h>
void main() {
unsigned short k;

while (1) {
Sleep(100);
k=getch();
if (27==k) break;//按Esc键退出
if (0==k||0xe0==k) k|=getch()<<8;//非字符键
cprintf("%04x pressed.\r\n",k);
}
}
赵4老师 2011-12-20
  • 打赏
  • 举报
回复
//The _getch function reads a single character from the console without echoing.
//Function can not be used to read CTRL+Break.
//When reading a function key or an arrow key,
//_getch must be called twice; the first call returns 0 or 0xE0,
//and the second call returns the actual key code.
#include <conio.h>
#include <windows.h>
void main() {
unsigned short k;

while (1) {
Sleep(100);
k=getch();
if (27==k) break;//按Esc键退出
if (0==k||0xe0==k) k|=getch()<<8;//非字符键
cprintf("%04x pressed.\r\n",k);
}
}
赵4老师 2011-12-19
  • 打赏
  • 举报
回复
The _getch function reads a single character from the console without echoing. _getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
#include <conio.h>
#include <windows.h>
void main() {
unsigned short k;

while (1) {
Sleep(100);
k=getch();
if (27==k) break;//按Esc键退出
if (0==k||0xe0==k) k|=getch()<<8;//非字符键
cprintf("%04x pressed.\r\n",k);
}
}
鲤鱼 2011-12-19
  • 打赏
  • 举报
回复
上下左右键的输入
#include <conio.h>
#include <stdio.h>
int main()
{
int c;
while(1)
{
c=getch();
switch(c)
{
case 72:
printf("up\n");
break;
case 75:
printf("left\n");
break;
case 77:
printf("right\n");
break;
case 80:
printf("down\n");
break;
case 27: // ESC
return 1;
}
}
return 0;
}
鲤鱼 2011-12-19
  • 打赏
  • 举报
回复
应该把问题说清楚一些啊~
赵4老师 2011-12-19
  • 打赏
  • 举报
回复
#include <conio.h>
#include <windows.h>
void main() {
unsigned short k;

while (1) {
Sleep(100);
k=getch();
if (27==k) break;//按Esc键退出
if (0==k) k=getch()<<8;//非字符键
cprintf("%04x pressed.\r\n",k);
}
}
猪头小哥 2011-12-19
  • 打赏
  • 举报
回复
键盘的消息??一般的输入也算消息吧?不就是用if()来检测的么?难道你想输入其他的键?比如上下键 左右键?
cocoabird 2011-12-19
  • 打赏
  • 举报
回复

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧