我是 C/C 新手。目前,我正在做一個使用觸覺設備的專案。我只想在按下設備按鈕時回傳一個值,下面是我的代碼。
#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();。在代碼中,您可以看到HLCALLBACK buttonCB按下按鈕時觸發事件的 int 函式。我希望當按下按鈕時,事件應該將一個值存盤在一個btn可以從主函式訪問的變數中。請幫我解決這個問題。
uj5u.com熱心網友回復:
您可以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);
}
}
uj5u.com熱心網友回復:
我不是很喜歡事件處理。但我會盡力提供幫助。
您正在從函式回傳 btn,該函式的型別為 int,但函式回傳型別為 void,這意味著該函式不能回傳任何內容。所以,修復很簡單。將 buttonCB 函式的回傳型別從 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;
}
}
現在函式的回傳型別是 int,這是您要回傳的 btn 變數的型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/428315.html
下一篇:從RVA中查找檔案的函式偏移量
