如何 return 来自事件函数的变量?

How to return a variable from the event function?

我是 C/C++ 的新手。目前,我正在从事一个使用触觉设备的项目。我只想return按下设备按钮时的一个值下面是我的代码。

#ifdef  _WIN64
#pragma warning (disable:4996)
#endif

#include <stdio.h>
#include <assert.h>

#if defined(WIN32)
# include <conio.h>
#else
# include "conio.h"
#endif

#include <HD/hd.h>
#include <HL/hl.h>

#include <HDU/hduError.h>

int HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                       HLcache *cache, void *userdata);

/*******************************************************************************
 Main function.
*******************************************************************************/
int btn;
int main(int argc, char *argv[])
{
    HHD hHD;
    HHLRC hHLRC;
    HDErrorInfo error;
    HLerror frameError;

    hHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError())) 
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "\nPress any key to quit.\n");
        getch();
        return -1;
    }
    hdMakeCurrentDevice(hHD);    
    hHLRC = hlCreateContext(hHD);
    hlMakeCurrent(hHLRC);

    /* Add a callback to handle button down in the collision thread. */
    hlAddEventCallback(HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, 0);
    hlAddEventCallback(HL_EVENT_2BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, 0);

    printf("Move around to feel the ambient stick-slip friction.\n\n");
    printf("Press and hold the primary stylus button to feel the spring effect.\n\n");
    printf("Press the second stylus button to trigger an impulse.\n\n");

    /* Run the main loop. */
    while (!_kbhit())
    {
        hlCheckEvents();
        Sleep(3000);

    }
    hlDeleteContext(hHLRC);
    hdDisableDevice(hHD);

    return 0;
}

void HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                         HLcache *cache, void *userdata)
{
    int btn;
    if (event == HL_EVENT_1BUTTONDOWN)
    {
        btn = 1;
        printf("Button 1 pressed %d ", btn);
        return btn;
    }
    else if (event == HL_EVENT_2BUTTONDOWN)
    {
        btn = 2;
        printf("Button 2 pressed %d ", btn);
        return btn;

    }
}

/******************************************************************************/

buttonCB回调函数用作事件。使用此命令 hlCheckEvents(); 在主循环中检查这些按钮事件。在代码中,您可以看到当按下按钮时触发事件的 int HLCALLBACK buttonCB 函数。我希望当按下按钮时,事件应该将一个值存储在一个名为 btn 的变量中,该变量可以从主函数访问。 请帮助我解决这个问题。

我不是很喜欢事件处理。不过我会尽力帮忙的。

您正在从函数中 returning btn,它是 int 类型,但是函数 return 类型是 void,这意味着函数不能 return 任何东西。所以,修复很简单。将 buttonCB 函数的 return 类型从 void 更改为 int:

/*void*/ int HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                         HLcache *cache, void *userdata)
{
    int btn;
    if (event == HL_EVENT_1BUTTONDOWN)
    {
        btn = 1;
        printf("Button 1 pressed %d ", btn);
        return btn;
    }
    else if (event == HL_EVENT_2BUTTONDOWN)
    {
        btn = 2;
        printf("Button 2 pressed %d ", btn);
        return btn;

    }
}

现在函数的 return 类型是 int,也就是您正在 returning 的 btn 变量的类型。

您可以使用 userData 来传输数据,例如:

int button = 0; // Should be valid as long as event might be called.

/* Add a callback to handle button down in the collision thread. */
hlAddEventCallback(HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                   buttonCB, &button);
hlAddEventCallback(HL_EVENT_2BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                   buttonCB, &button);
// ..

/* Run the main loop. */
while (!_kbhit())
{
    // button = 0; // Potentially reinit here
    hlCheckEvents();
    printf("button value: %d", button); // Do work with last value
    Sleep(3000);
}

然后是你的回电

void HLCALLBACK buttonCB(HLenum event, HLuint object, HLenum thread, 
                         HLcache *cache, void *userdata)
{
    int* button = (int*) userdata;
    if (event == HL_EVENT_1BUTTONDOWN)
    {
        *button = 1;
        printf("Button 1 pressed %d ", btn);
    }
    else if (event == HL_EVENT_2BUTTONDOWN)
    {
        *button = 1; = 2;
        printf("Button 2 pressed %d ", btn);
    }
}