Identificar caracteres do teclado em C/C++

main.html
#include <windows.h>
#include "resource.h"

#define IDM_SAIR   1
#define IDM_E12    2
#define IDM_E13    3
#define IDM_E14    4
#define IDM_E15    5
#define IDM_E16    6
#define IDM_E17    7
#define IDM_E18    8
#define IDM_E19    9

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HBRUSH hBrush[6];
HCURSOR hCursor;

char *caractere[] = {
    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a",
    "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
    "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
    "x", "y", "z", "Home", "PgUp", "PgDown", "End", "Sup",
    "Sdown", "Sesq", "Sdir", "Ret"
};

char szNA[] = "Teclado";
char buf[30];
char buf1[100] = "\0";
char buf2[500] = "\0";
char fcr[] = "Deseja fechar?";

int cont = 0;

int contador[46];

int barra = 0;
int hbar  = 0; //height bar
BOOL cores = TRUE;
int cont1  = 0;

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR szCL, int iCS) {
    WNDCLASS wc;
    HWND     hwnd;
    MSG      msg;
    int      i;
    HACCEL   hAccel;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hI;
    wc.hIcon         = LoadIcon(hI, IDI_ICON);
    wc.hCursor       = LoadCursor(hI, szNA);
    wc.hbrBackground = GetSysColorBrush(WHITE_BRUSH);
    wc.lpszMenuName  = szNA;
    wc.lpszClassName = szNA;

    if(!RegisterClass(&wc)) {
        return 0;
    }

    hwnd = CreateWindow(
        szNA, "Teclado:", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        HWND_DESKTOP, NULL, hI, NULL
    );

    hAccel = LoadAccelerators(hI, szNA);

    ShowWindow(hwnd, iCS);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0)) {
        if(!TranslateAccelerator(hwnd, hAccel, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    for(i=0; i<6; i++) {
        DeleteObject((HGDIOBJ) hBrush[i]);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wP, LPARAM lP) {
    HDC         hdc;
    PAINTSTRUCT ps;
    RECT        rect;
    POINT       pt;
    float       x, y;
    int         i, j;
    switch(msg) {
        case WM_CREATE:
            hBrush[0] = CreateSolidBrush(RGB(255, 0, 0));
            hBrush[1] = CreateSolidBrush(RGB(127, 127, 127));
            hBrush[2] = CreateSolidBrush(RGB(255, 255, 0));
            hBrush[3] = CreateSolidBrush(RGB(0, 0, 255));
            hBrush[4] = CreateSolidBrush(RGB(0, 255, 0));
            hBrush[5] = CreateSolidBrush(RGB(255, 175, 0));
            for(i=0; i<100; i++) contador[i] = 0;
            return 0;
        case WM_COMMAND:
            switch(LOWORD(wP)) {
                case IDM_SAIR:
                    if(IDOK==MessageBox(hwnd,fcr,szNA,MB_OK|0x30)) {
                        SendMessage(hwnd, WM_CLOSE, 0, 0L);
                    }
                    break;
                case IDM_E12:
                    SendMessage(hwnd, WM_KEYDOWN, VK_ESCAPE, 1L);
                    break;
                case IDM_E13:
                    SendMessage(hwnd, WM_KEYDOWN, VK_F12, 1L);
                    break;
                case IDM_E14:
                    SendMessage(hwnd, WM_KEYDOWN, VK_RIGHT, 1L);
                    break;
                case IDM_E15:
                    SendMessage(hwnd, WM_KEYDOWN, VK_LEFT, 1L);
                    break;
                case IDM_E16:
                    SendMessage(hwnd, WM_KEYDOWN, VK_UP, 1L);
                    break;
                case IDM_E17:
                    SendMessage(hwnd, WM_KEYDOWN, VK_DOWN, 1L);
                    break;
                case IDM_E18:
                    cores = TRUE;
                    break;
                case IDM_E19:
                    cores = FALSE;
                    break;
            }
            return 0;
        case WM_KEYDOWN:
            hdc = GetDC(hwnd);
            GetClientRect(hwnd, &rect);
            switch(wP) {
                case VK_SPACE:
                    SetRect(
                        &rect,
                        rect.right*0.05,
                        rect.bottom*0.68,
                        rect.right*0.95,
                        rect.bottom*0.8
                    );
                    contador[36]++;
                    break;
                case VK_HOME:
                    SetRect(
                        &rect,
                        rect.right*(0.05+6.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+7.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[37]++;
                    break;
                case VK_PRIOR:
                    SetRect(
                        &rect,
                        rect.right*(0.05+7.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+8.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[38]++;
                    break;
                case VK_NEXT:
                    SetRect(
                        &rect,
                        rect.right*(0.05+8.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+9.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[39]++;
                    break;
                case VK_END:
                    SetRect(
                        &rect,
                        rect.right*(0.05+9.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+11.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[40]++;
                    break;
                case VK_UP:
                    SetRect(
                        &rect,
                        rect.right*(0.05+10.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+11.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[41]++;
                    GetClientRect(hwnd, &rect);
                    if(hbar < rect.bottom)
                        hbar++;
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
                case VK_DOWN:
                    SetRect(
                        &rect,
                        rect.right*(0.05+11.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+12.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[42]++;
                    if(hbar > 0)
                        hbar--;
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
                case VK_LEFT:
                    SetRect(
                        &rect,
                        rect.right*(0.05+12.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+13.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[43]++;
                    if(barra > 0) {
                        barra--;
                    }
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
                case VK_RIGHT:
                    SetRect(
                        &rect,
                        rect.right*(0.05+13.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*(0.05+14.0*0.9/15.0),
                        rect.bottom*0.68
                    );
                    contador[44]++;
                    GetClientRect(hwnd, &rect);
                    if(barra<rect.right)
                        barra++;
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
                case VK_RETURN:
                    SetRect(
                        &rect,
                        rect.right*(0.05+14.0*0.9/15.0),
                        rect.bottom*0.52,
                        rect.right*0.95,
                        rect.bottom*0.68
                    );
                    contador[45]++;
                    break;
                case VK_ESCAPE:
                    MessageBox(
                        hwnd,
                        buf1,
                        "Sequência caracteres Pressionados!",
                        MB_OK
                    );
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
                case VK_F12:
                    sprintf(
                        buf2,
                        "Espaço - %d, Home - %d, PageUp - %d, PageDown \
                        - %d, End - %d, Seta Acima - %d, Seta Abaixo - %d, \
                        Seta Esquerda - %d, Seta a Direita - %d, Return - %d",
                        contador[36],
                        contador[37],
                        contador[38],
                        contador[39],
                        contador[40],
                        contador[41],
                        contador[42],
                        contador[43],
                        contador[44],
                        contador[45]
                    );
                    for(i=0; i<36; i++) {
                        sprintf(buf, "%c", caractere[i][0]);
                        strcat(buf2, buf);
                        sprintf(buf, "%d", caractere[i]);
                        strcat(buf2, buf);
                    }
                    MessageBox(
                        hwnd,
                        buf2,
                        "Número de vezes que os caracteres foram pressionados!",
                        MB_OK
                    );
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
            }
            FillRect(hdc, &rect, hBrush[2+3*cores]);
            ReleaseDC(hwnd, hdc);
            cont1++;
            return 0;
        case WM_KEYUP:
            sprintf(buf, "%d", cont1);
            MessageBox(
                hwnd,
                buf,
                "Tecla Pressionada",
                MB_OK
            );
            cont1 = 0;
            InvalidateRect(hwnd, NULL, 1);
            return 0;
        case WM_CHAR:
            switch(wP) {
                case '\r':
                case '\ ':
                    break;
                default:
                    sprintf(
                        buf,
                        "A tecla pressionada foi %c",
                        (char) wP
                    );
                    InvalidateRect(hwnd, NULL, 0);
                    MessageBox(
                        hwnd,
                        buf,
                        "Tecla Pressionada",
                        MB_OK
                    );
                    if(isdigit((char) wP)) {
                        contador[wP-48]++;
                    }
                    else {
                        contador[wP-87]++;
                    }
                    sprintf(buf, "%c", (char)wP);
                    strcat(buf1, buf);
                    cont++;
                    if(cont==100) {
                        cont=0;
                        strcpy(buf1, " ");
                    }
                    InvalidateRect(hwnd, NULL, 1);
                    break;
            }
            return 0;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
            FillRect(hdc, &rect, hBrush[3*cores]);
            for(i=0; i<barra; i++) {
                MoveToEx(hdc, i, rect.bottom, &pt);
                LineTo(hdc, i, rect.bottom - hbar);
            }
            SetRect(
                &rect,
                rect.right*0.05,
                rect.bottom*0.2,
                rect.right*0.95,
                rect.bottom*0.8
            );
            FillRect(hdc, &rect, hBrush[1+3*cores]);
            if(cores) {
                SetBkColor(hdc, RGB(0, 255, 0));
            }
            else {
                SetBkColor(hdc, RGB(127, 127,127));
            }
            for(j=0; j<3; j++) {
                for(i=0; i<15; i++) {
                    x = i*0.95/15;
                    y = 0.2*j;
                    TextOut(
                        hdc,
                        rect.right*(0.05+x)+3,
                        rect.bottom*(0.25+y)+15,
                        caractere[i+15*j],
                        strlen(caractere[i+15*j])
                    );
                }
            }
            for(i=0; i<4; i++) {
                y = 0.2*i;
                MoveToEx(hdc, rect.left, rect.bottom*(0.25+y), &pt);
                LineTo(hdc, rect.right, rect.bottom*(0.25+y));
            }
            for(i=0; i<16; i++) {
                x = i*0.95/15;
                MoveToEx(
                    hdc,
                    rect.right*(0.05+x),
                    rect.bottom*0.25,
                    &pt
                );
                LineTo(hdc, rect.right*(0.05+x), rect.bottom*0.85);
            }
            EndPaint(hwnd, &ps);
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            return 0;
    }
    return DefWindowProc(hwnd, msg, wP, lP);
}


resource.html




/********** resource.h **********/

#define IDI_ICON 20

resourcerc.html



/********** resource.rc **********/

#include "resource.h"

IDI_ICON ICON "icone.ico"

Comentários