WINAPI你自己的键盘onPress和onReleased

时间:2013-04-23 13:48:35

标签: c++ keyboard window

目前,我正在创建自己的onPress和onRelease函数。但是,我的onRelease函数有问题。显然,即使我没有释放键盘,我的onRelease仍继续触发。

我怀疑它与CPU内部的循环次数有关,但我不确定这个理论。不知怎的,也许循环比我的框架慢,那为什么PeerMessage返回false?没有事件被触发。

解决方案: **在WinProc函数下创建一个名为WM_KEYUP的新案例。一旦用户离开按钮,该事件将触发。它有助于解决CPU内部循环次数的问题。 **

*注: 我已经更新了我的代码细节。

描述。窗口编程 我有两个std :: array存储我的键盘数据 1)InputCurr 2)InputPrev

std::array<char, 256> inputPrev;
std::array<char, 256> inputCurr;

While(TRUE) {
    std::copy(InputCurr.begin(), InputCurr.end(), InputPrev.begin());
    inputCurr.fill(0);

    while(PeekMessage (&uMsg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage (&uMsg);
        DispatchMessage (&uMsg);
    }

    if(onReleased(x030))  //Button 0
    //do something
}

char onReleased(char key)
{
    return (inputCurr[key] && !inputPrev[key])? 1 : 0;
}

void VEInputMessage(WPARAM key)
{
    inputCurr[key]= 1;  //Set to true of the keyboard key
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{
    HDC dc;           /* device context */
    PAINTSTRUCT ps;   /* the paint struct */
    RECT rect;

    UNREFERENCED_PARAMETER(rect);

    switch (msg)
    {
    /* when the window is created */
    case WM_CREATE:
        break;

    /* when the rectangle is drawn */
    case WM_LBUTTONDOWN:
            break;

    case WM_MOUSEMOVE:
        break;

    case WM_PAINT:
        dc = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;

    /* When it's time for the window to be closed and removed */
    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    case WM_KEYDOWN:
        VEINPUT::VEInputMessage(wParam);  //Updated the input key 
        if(wParam == AEVK_ESCAPE)
            PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}

0 个答案:

没有答案