循环计数不必要的输入win32 API

问题描述 投票:0回答:1

我有一个编码逻辑问题,但我无法理解错误的原因。

我用C语言为输入系统使用win32 API做了一个简单的输入函数,

该函数有两个参数,一为键盘的十六进制代码,二为按键模式。

关键模式是,

SINGLE
:仅激活该命令一次,并且仅在再次按下时再次激活。

KEEP
:在按键无限循环中触发突发命令。

以及有问题的部分,

KEEP_TIME
:以毫秒为单位计算按下按钮的时间。

#define PRESS_STATE 0x8000

typedef enum 
{
    SINGLE,
    KEEP,
    KEEP_TIME
} InputMode;


static DWORD keyInput(int keyCode, InputMode mode) 
{
    static bool_ keyState[256] = { FALSE };  /* Array to control the state of the keys */
    static bool_ prevKeyState[256] = { FALSE }; /* Array to store the previous state of keys */
    static DWORD keyPressTimes[256] = { 0 }; /* Array to record key press times */
    DWORD releaseTime = 0;
    DWORD pressTime = 0;
    DWORD pressDuration = 0;

    /* Get current key state */
    bool_ currentKeyState = GetAsyncKeyState(keyCode) & PRESS_STATE;

    /* Check if the key is pressed at the current time */
    if (currentKeyState) 
    {
        /* Check if the key was released in the previous frame */
        if (!prevKeyState[keyCode]) 
        {
            /* Mark the key as pressed in the current frame */
            keyState[keyCode] = true;
            keyPressTimes[keyCode] = GetTickCount(); /* Records key press time */
            prevKeyState[keyCode] = true;

            if (mode == SINGLE) 
            {
               return true;
            }
        }
    } 
    else 
    {
        if (keyState[keyCode]) 
        {
            prevKeyState[keyCode] = FALSE; /* Mark the key as released in the current frame */

            if (mode == SINGLE) 
            {
                keyState[keyCode] = FALSE;
            }

            releaseTime = GetTickCount();
            pressTime = keyPressTimes[keyCode];
            pressDuration = releaseTime - pressTime;

           if (mode == KEEP_TIME) 
            {
                return pressDuration;
                keyState[keyCode] = false;
            }            

        }
    }

    /* Return 0 if KEEP mode is used and the key is pressed */
    return (mode == KEEP) && currentKeyState ? 1 : 0;
}

代码符合 ANSI C89 标准。

KEEP_TIME
模式的问题是它以毫秒为单位返回正确的值,但在输入数字后它开始无限计数。

KEY : 'W' pressed by 422 ms
KEY : 'W' pressed by 437 ms
KEY : 'W' pressed by 453 ms
KEY : 'W' pressed by 469 ms
KEY : 'W' pressed by 484 ms
KEY : 'W' pressed by 500 ms
KEY : 'W' pressed by 516 ms
KEY : 'W' pressed by 531 ms
KEY : 'W' pressed by 547 ms
KEY : 'W' pressed by 562 ms
KEY : 'W' pressed by 578 ms
KEY : 'W' pressed by 594 ms
KEY : 'W' pressed by 609 ms
KEY : 'W' pressed by 625 ms
KEY : 'W' pressed by 641 ms
KEY : 'W' pressed by 656 ms

等等,但是不知道计数的原因,不知道是因为循环还是因为

printf()
在循环里面

这是循环内的代码。

void GLRender()
{/* GAME LOOP */
    
    DWORD pressDuration = keyInput(KEY_W, KEEP_TIME);

    if (pressDuration > 1) 
    {
            /* Print key press time */
            printf("KEY : '%c' pressed by %d ms\n", KEY_W, pressDuration);
    }
}

我想让第三种模式 KEEP_TIME 以正确的毫秒返回值,这样我就可以做一个时间按钮系统

c winapi
1个回答
-2
投票

使用

%s

void GLRender()
{/* GAME LOOP */
    
    DWORD pressDuration = keyInput(KEY_W, KEEP_TIME);

    if (pressDuration > 1) 
    {
            /* Print key press time */
            printf("KEY : '%s' pressed by %s ms\n", KEY_W, pressDuration);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.