读书人

c语言获取键盘内容并以ASCII格式输出

发布时间: 2012-06-19 14:45:20 作者: rapoo

c语言获取键盘内容,并以ASCII格式输出问题
我在网上找到的代码,除了辅助键shift,alt,ctrl,capslock,esc不能获取,其他都能获取,只是有些键会输出两部分ascii码,这些两部分的ASCII码都代表啥?

C/C++ code
#include <stdio.h>void main(){    while(1)    {    char a;    a=getch();    printf("//%x---\n",a);    }}


现在要达到的要求是正确识别键盘ASCII值,正确输出。求高人指点。

[解决办法]
C/C++ code
//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);    }} 

读书人网 >C语言

热点推荐