Win32에서는 키보드 입력 메시지또한 처리 할 수 있다.


 C언어에서는 kbhit()라는 함수등을 사용한다.


 키보드는 키를 눌렀을때, 떼었을때, 문자일때로 나뉘어 져있다.


 키보드의 정보는 Virtual-Key라는 것을 이용해서 구분을 짓는다.[각주:1]


 예제

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
 
#include <tchar.h>
#include <windows.h>
 
#include <string.h>
 
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
 
WNDCLASS WndClass;
 
int numbers = 0;
 
int WINAPI WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            LPSTR lpszCmdParam,
            int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    g_hInst = hInstance;
 
    WndClass.cbClsExtra=0;
    WndClass.cbWndExtra=0;
    WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc=(WNDPROC)WndProc;
    WndClass.lpszClassName="ApiBase";
    WndClass.lpszMenuName=NULL;
    WndClass.style=CS_HREDRAW|CS_VREDRAW;
 
    RegisterClass(&WndClass);
    
    hWnd = CreateWindow("ApiBase",
                        "Test",
                        WS_OVERLAPPEDWINDOW,
                        10,// X
                        100,// Y
                        400,// Width
                        400,// Height
                        NULL,
                        (HMENU)NULL,
                        hInstance,
                        NULL);
 
                        
    ShowWindow(hWnd,nCmdShow);
    
    while(GetMessage(&Message,0,0,0))
    {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    
    return Message.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{    
    switch(iMessage)
    {
        case WM_CREATE:
            strcpy(strTemp, "");
            break;
        case WM_KEYDOWN:
            switch(wParam)
            {
                case VK_HOME:
                    printf("Home\n");
                    break;
                case VK_END:
                    printf("End\n");
                    break;
                case VK_UP:
                    printf("Up Key\n");
                    break;
                case VK_F1:
                    printf("F1\n");
                    break;
                case 0x41:
                    printf("A\n");
                    break;
            }
            break;
        case WM_LBUTTONUP:
            printf("%d\n",ReleaseCapture());
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    }
    return DefWindowProc(hWnd,iMessage,wParam, lParam);
}
 
cs


 하지만, 키보드 메시지를 사용한다면, 연속적으로 눌은 키에 대한 정보는 처리가 되지 않는다.


 쉽게 말해서 노트패드에 정보를 입력해보자.


 동시다발적으로 여러 키를 눌을 경우에, 해당 키의 정보를 다 읽지 못하고, 하나의 키만 처리하는걸 알 수 있을 것이다.(노트패드에서 이렇게 처리할 경우가 있을리도 만무하다.)


 이유는 메시지가 하나의 값만 계속 받아서 처리하기 떄문일것이다.


 그럴때는 어떻게 하는가?


 이것은 다음 네이버 카페에서 도움을 받았었다.[각주:2]


 여기서, GetKeyState 라는 함수를 사용하면 된다.[각주:3]


 해당 함수는 키의 눌음을 인식하는 함수이다.

  1. 참조 : https://msdn.microsoft.com/ko-kr/library/windows/desktop/dd375731(v=vs.85).aspx (2016-07-05) [본문으로]
  2. http://cafe.naver.com/cafec [본문으로]
  3. https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms646301(v=vs.85).aspx [본문으로]
Posted by JunkMam
,