Using DirectInput to receive a signal after connecting a joystick

I have a C ++ program that lists all input devices (using direct input) at the beginning of the program. If the program is running, and then I connect another controller, this controller will not be recognized until the program is restarted. Does anyone know of an event that I can use that will cause my program to list all devices after a new one is connected?

+6
source share
1 answer

This article discusses ways to detect changes to the playing field. First of all, you can process the WM_DEVICECHANGE message and check wParam for DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE . It seems that in order to get them as wParam s, you must first call RegisterDeviceNotification .

An example article on how to do this is as follows:

 DEV_BROADCAST_DEVICEINTERFACE notificationFilter; ZeroMemory(&notificationFilter, sizeof(notificationFilter)); notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; notificationFilter.dbcc_size = sizeof(notificationFilter); HDEVNOTIFY hDevNotify; hDevNotify = RegisterDeviceNotification(m_hWnd, &notificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); if(hDevNotify == NULL) { // do some error handling } 

The only thing you need to pay attention to is the minimum supported OS for this - XP, so you need to add the appropriate #define for this before including the Windows headers.

Depending on what you want to do, you might not even have to call this function first. Instead, you can simply check DBT_DEVNODES_CHANGED to not distinguish between a connected or disconnected device. This might save some code if you don't care.

+2
source

Source: https://habr.com/ru/post/944904/


All Articles