| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1333 人关注过本帖
标题:用于TC下面的键码预定义。(适用于处理键盘的程序)
只看楼主 加入收藏
hoodlum1980
Rank: 2
来 自:浙江大学
等 级:论坛游民
威 望:2
帖 子:289
专家分:23
注 册:2008-2-24
收藏
 问题点数:0 回复次数:2 
用于TC下面的键码预定义。(适用于处理键盘的程序)
我们在这里定义了大部分按键的扫描码,这些扫描码可以方便的转换为ascii码,同时,也可以查询到控制键状态,例如ctrl和shift按键是否按下的状态等。可以把下面的内容保存为一个头文件,例如KEYS.H,放到INCLUDE文件夹下面:
--------------------------分割线:KEYS.H ---------------------------------------------------------
/********************************/
/* Desc:    键码                                    */
/* By:        hoodlum1980                        */
/* Email:    jinfd@                    */
/* Date:    2008.02.25                        */
/* Note:    需要bios.h                        */
/********************************/

/*Scan Codes Define*/

enum KEYS
{
    K_ESC                =0x011b,
    K_F1                =0x3b00,
    K_F2                =0x3c00,
    K_F3                =0x3d00,
    K_F4                =0x3e00,
    K_F5                =0x3f00,
    K_F6                =0x4000,
    K_F7                =0x4100,
    K_F8                =0x4200,
    K_F9                =0x4300,
    K_F10                =0x4400,

    /*主键盘区*/
    K_TILDE            =0x2960,        /* ~ */
    K_D1                =0x0231,        /* Number 1 */
    K_D2                =0x0332,
    K_D3                =0x0433,
    K_D4                =0x0534,
    K_D5                =0x0635,
    K_D6                =0x0736,
    K_D7                =0x0837,
    K_D8                =0x0938,
    K_D9                =0x0a39,
    K_D0                =0x0b30,
    K_SUB                =0x0c2d,        /* - */
    K_EQU                =0x0d3d,        /* = */
    K_BACKSLASH    =0x2b5c,        /* \ */
    K_BACK            =0x0e08,        /* Backspace */
    K_TAB                =0x0f09,
    K_Q                    =0x1071,
    K_W                    =0x1177,
    K_E                    =0x1265,
    K_R                    =0x1372,
    K_T                    =0x1474,
    K_Y                    =0x1579,
    K_U                    =0x1675,
    K_I                    =0x1769,
    K_O                    =0x186f,
    K_P                    =0x1970,
    K_LBRACKET    =0x1a5b,         /* [ */
    K_RBRACKET    =0x1b5d,        /* ] */
    K_A                    =0x1e61,
    K_S                    =0x1f73,
    K_D                    =0x2064,
    K_F                    =0x2166,
    K_G                    =0x2267,
    K_H                    =0x2368,
    K_J                    =0x246a,
    K_K                    =0x256b,
    K_L                    =0x266c,
    K_SEMICOLON    =0x273b,
    K_QUOT            =0x2827,        /* ' */
    K_RETURN        =0x1c0d,        /* Enter */
    K_Z                    =0x2c7a,
    K_X                    =0x2d78,
    K_C                    =0x2e63,
    K_V                    =0x2f76,
    K_B                    =0x3062,
    K_N                    =0x316e,
    K_M                    =0x326d,
    K_COMMA            =0x332c,        /* , */
    K_DOT                =0x342e,        /* . */
    K_DIVIDE        =0x352f,        /* / */
    K_SPACE            =0x3920,

    /*右边数字键盘*/

    K_INSERT        =0x5200,
    K_HOME            =0x4700,
    K_PAGEUP        =0x4900,
    K_DEL                =0x5300,
    K_END                =0x4f00,
    K_PAGEDOWN    =0x5100,
    K_UP                =0x4800,        /* upward arrow */
    K_LEFT            =0x4b00,
    K_DOWN            =0x5000,
    K_RIGHT            =0x4d00,
    K_NUMPADMUL    =0x372a,        /* * */
    K_NUMPADSUB    =0x4a2d,        /* - of numberpad */
    K_NUMPAD7        =0x4737,
    K_NUMPAD8        =0x4838,
    K_NUMPAD9        =0x4939,
    K_NUMPAD4        =0x4b34,
    K_NUMPAD5        =0x4c35,
    K_NUMPAD6        =0x4d36,
    K_NUMPADADD    =0x4e2b,        /* + */
    K_NUMPAD1        =0x4f31,
    K_NUMPAD2        =0x5032,
    K_NUMPAD3        =0x5133,
    K_NUMPAD0        =0x5230,
    K_NUMPADDEL    =0x532e
};

/*Control Keys Flags*/
typedef struct _tagCTLKEYS
{
    int rShift;
    int lShift;
    int Ctrl;
    int Alt;
    int ScrollLock;
    int NumLock;
    int CapsLock;
    int Insert;
} CTLKEYS;

int GetKeyCode(void);

int GetKeyCodeEx(CTLKEYS *ck);

/*Get Key Code */
int GetKeyCode(void)
{
    int key=0;
    if(bioskey(1))
    {
        key=bioskey(0);
    }
    return key;
}

/*Get Key Code and CtrlKey Flags*/
int GetKeyCodeEx(CTLKEYS *ck)
{
    int flags,key=0;
    if(bioskey(1))
    {
        key=bioskey(0);
        flags=bioskey(2);
        if(ck!=NULL)
        {
            ck->rShift = flags&0x01;
            ck->lShift = flags&0x02;
            ck->Ctrl = flags&0x04;
            ck->Alt    = flags&0x08;
            ck->ScrollLock    = flags&0x10;
            ck->NumLock = flags&0x20;
            ck->CapsLock = flags&0x40;
            ck->Insert = flags&0x80;    
        }    
    }
    return key;    
}
--------------------------------分割线:文件结束------------------------------------------------

上面的代码用法非常简单,但是需要手工包含以下头文件,bios.h,因为用到了里面的bioskey函数。
可以用GetKeyCode()获得扫描码scanCode,低8位字节(scanCode & 0xff)就是ascii码。
例如,我们可以用下面的程序测试按键:按Escapte键退出程序:

#include <stdio.h>
#include <bios.h>
#include <dos.h>
#include <keys.h>

void main()
{
    int key=0,loops=0;
    char c;
    CTLKEYS ck;    /* control key flags struct  */
    printf("\nPress Esc to Escape! Press other key to display Info.\n");
    while(key!=K_ESC && ++loops<100)
    {
        /* wait until a key pressed */
        while(!(key=GetKeyCodeEx(&ck)))
        {
        }
        c=key&0xff;    /* get ASCII code */
        switch(key)
        {
            case K_TAB:
                printf("pressed Tab,");
                break;
            case K_BACK:
                printf("pressed Backspace,");
                break;
            case K_RETURN:
                printf("pressed Enter,");
                break;
            case K_F1:
                printf("pressed F1,");
                break;
            default:
                printf("Pressed %c,",c);    /* printf the char */
                break;
        }
        if(ck.Ctrl)
            printf(" Ctrl,");
        if(ck.lShift)
            printf(" LeftShift,");
        if(ck.rShift)
            printf(" RightShift");
        if(ck.ScrollLock)
            printf(" ScrollLock On,");
        if(ck.NumLock)
            printf(" NumlLock On,");
        if(ck.CapsLock)
            printf(" CapsLock On,");
        printf("\n");
    }
}

[[it] 本帖最后由 hoodlum1980 于 2008-2-25 14:32 编辑 [/it]]
搜索更多相关主题的帖子: 定义 键盘 KEYS 扫描 
2008-02-25 14:25
xianshizhe111
Rank: 1
等 级:新手上路
帖 子:1451
专家分:0
注 册:2007-12-8
收藏
得分:0 
支持一下这都是好东东.顶了.
2008-02-25 14:32
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
LZ得好东西真多啊.你可以把它都做好,发布自己的tc开发包了....
2008-02-25 14:41
快速回复:用于TC下面的键码预定义。(适用于处理键盘的程序)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030182 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved