Unity player embedded into Qt application, the video game dosen't response to keyboard and mouse wheel events any more.
-
The game itself can response to keyboad, mouse wheel, mouse move and mouse click events, but when I embed unity player into Qt application, the game dosen't response to keyboad and mouse wheel events any more. how to fix it ?
typedef struct MyData { HINSTANCE hInstance; HINSTANCE hPrevInstance; LPWSTR lpCmdLine; int nShowCmd; } MYDATA, * PMYDATA; DWORD WINAPI MyThreadFunction(LPVOID lpParam); int main(int argc, char *argv[]) { QApplication a(argc, argv); HINSTANCE hInstance = GetModuleHandle(NULL); LPWSTR lpCmdLine = GetCommandLineW(); QWidget w; w.show(); QString cmd = " -screen-fullscreen 0 -popupwindow -screen-width 1920 -screen-height 1280 -popupwindow -parentHWND " + QString::number(w.winId()) + " delayed "; qDebug() << "lpCmdLine " << cmd; wchar_t ss[1024]; cmd.toWCharArray(ss); PMYDATA pData = (PMYDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA)); pData->hInstance = hInstance; pData->hPrevInstance = NULL; pData->lpCmdLine = ss; pData->nShowCmd = 10; DWORD dwThreadId; CreateThread( NULL, // default security attributes 0, // use default stack size MyThreadFunction, // thread function name pData, // argument to thread function 0, // use default creation flags &dwThreadId); // returns the thread identifier //UnityMain(hInstance, NULL, ss, 10); return a.exec(); } DWORD WINAPI MyThreadFunction(LPVOID lpParam) { PMYDATA pData = (PMYDATA)lpParam; UnityMain(pData->hInstance, pData->hPrevInstance, pData->lpCmdLine, pData->nShowCmd); return 0; }
-
The game itself can response to keyboad, mouse wheel, mouse move and mouse click events, but when I embed unity player into Qt application, the game dosen't response to keyboad and mouse wheel events any more. how to fix it ?
typedef struct MyData { HINSTANCE hInstance; HINSTANCE hPrevInstance; LPWSTR lpCmdLine; int nShowCmd; } MYDATA, * PMYDATA; DWORD WINAPI MyThreadFunction(LPVOID lpParam); int main(int argc, char *argv[]) { QApplication a(argc, argv); HINSTANCE hInstance = GetModuleHandle(NULL); LPWSTR lpCmdLine = GetCommandLineW(); QWidget w; w.show(); QString cmd = " -screen-fullscreen 0 -popupwindow -screen-width 1920 -screen-height 1280 -popupwindow -parentHWND " + QString::number(w.winId()) + " delayed "; qDebug() << "lpCmdLine " << cmd; wchar_t ss[1024]; cmd.toWCharArray(ss); PMYDATA pData = (PMYDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA)); pData->hInstance = hInstance; pData->hPrevInstance = NULL; pData->lpCmdLine = ss; pData->nShowCmd = 10; DWORD dwThreadId; CreateThread( NULL, // default security attributes 0, // use default stack size MyThreadFunction, // thread function name pData, // argument to thread function 0, // use default creation flags &dwThreadId); // returns the thread identifier //UnityMain(hInstance, NULL, ss, 10); return a.exec(); } DWORD WINAPI MyThreadFunction(LPVOID lpParam) { PMYDATA pData = (PMYDATA)lpParam; UnityMain(pData->hInstance, pData->hPrevInstance, pData->lpCmdLine, pData->nShowCmd); return 0; }
@gaojinhsu said in Unity player embedded into Qt application, the video game dosen't response to keyboard and mouse wheel events any more.:
The game itself can response to keyboad, mouse wheel, mouse move and mouse click events, but when I embed unity player into Qt application, the game dosen't response to keyboad and mouse wheel events any more. how to fix it ?
typedef struct MyData {
HINSTANCE hInstance;
HINSTANCE hPrevInstance;
LPWSTR lpCmdLine;
int nShowCmd;
} MYDATA, * PMYDATA;DWORD WINAPI MyThreadFunction(LPVOID lpParam);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);HINSTANCE hInstance = GetModuleHandle(NULL); LPWSTR lpCmdLine = GetCommandLineW();
QWidget w;
w.show();QString cmd = " -screen-fullscreen 0 -popupwindow -screen-width 1920 -screen-height 1280 -popupwindow -parentHWND " + QString::number(w.winId()) + " delayed "; qDebug() << "lpCmdLine " << cmd; wchar_t ss[1024]; cmd.toWCharArray(ss); PMYDATA pData = (PMYDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA)); pData->hInstance = hInstance; pData->hPrevInstance = NULL; pData->lpCmdLine = ss; pData->nShowCmd = 10; DWORD dwThreadId; CreateThread( NULL, // default security attributes 0, // use default stack size MyThreadFunction, // thread function name pData, // argument to thread function 0, // use default creation flags &dwThreadId); // returns the thread identifier //UnityMain(hInstance, NULL, ss, 10); return a.exec();
}
DWORD WINAPI MyThreadFunction(LPVOID lpParam)
{
PMYDATA pData = (PMYDATA)lpParam;
UnityMain(pData->hInstance, pData->hPrevInstance, pData->lpCmdLine, pData->nShowCmd);
return 0;
}This can be a bit tricky, as you're dealing with integration between two different frameworks. Here are a few steps you can consider to troubleshoot and fix the issue:
Check Unity Input Settings: Make sure that the Unity game's input settings are properly configured to respond to keyboard and mouse events. Check the Input Manager in Unity to ensure that the desired keys and mouse buttons are correctly set up.
Focus Issues: Sometimes, focus issues can prevent keyboard and mouse events from reaching the Unity game. Ensure that the embedded Unity player has the correct focus when running within the Qt application. You might need to handle focus changes and forwarding events appropriately between Qt and Unity.
Event Handling: Unity and Qt have different event handling systems. Make sure that Unity is receiving and processing the events correctly. You might need to capture the Qt events and translate them into Unity events.
Cross-thread Communication: It seems you're using a separate thread for Unity (MyThreadFunction) and the main thread for Qt. Ensure that you're correctly synchronizing and communicating between these threads, as UI events might need proper synchronization.
Qt Event Filters: You can use Qt's event filtering mechanism to intercept and forward events to the embedded Unity player. This involves subclassing QObject and implementing eventFilter() to capture and forward events.
Unity Build Settings: Double-check the build settings of your Unity project. Make sure that the input modules are correctly configured to handle the input events you expect.
Debugging: Use debugging tools in both Unity and Qt to diagnose the issue. Log messages, breakpoints, and debugging tools can help you identify where the events are getting lost.
Unity Player Logs: Check the Unity player logs for any error messages or warnings related to input events. This might give you insights into what's going wrong.