I've been trying to read data (x/y axis, pressed buttons, etc) from a usb joystick, and I've heard of DirectInput. Been searching for some long hours to find a decent example or walkthrough with little success. If anyone can share a simple tutorial, or guide me to the correct place, I'll be in your debt. Thank you
edit: Trying to achieve this goal on Windows platform
There are a few DirectInput samples in DirectX SDK, source code and compiled binaries. Included is Joystick
sample. Older versions of SDK (look for "DirectX 9.0 SDK", years around 2004-2005) had even more samples, up to a dozen!
Joystick Sample
Description The Joystick sample program obtains and displays joystick data.
Path Source: (SDK root )\Samples\C++\DirectInput\Joystick
Executable: (SDK root )\Samples\C++\DirectInput\Bin\Joystick.exe
User's Guide Observe how the displayed data changes when you move and twist the stick, rotate the throttle wheel, and press buttons in various combinations.
Programming Notes The application polls the joystick for immediate data in response to a timer set inside the dialog procedure.
Probably these might help you:
actually Coding4Fun has a lot of interesting things. It began around the time Win7 were released.
I used walbourn/directx-sdk-samples
on github and it works perfectly.
I had to configure .pro
file as follows to run it on Qt Creator:
TEMPLATE = app
CONFIG += c++14
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
HEADERS += resource.h
DISTFILES += directx.ico joystick.rc
RC_FILE = joystick.rc
DEFINES -= UNICODE
#DEFINES += _MBCS
win32: QMAKE_CXXFLAGS_RELEASE -= -Zc:strictStrings
win32: QMAKE_CFLAGS_RELEASE -= -Zc:strictStrings
win32: QMAKE_CXXFLAGS_DEBUG -= -Zc:strictStrings
win32: QMAKE_CFLAGS_DEBUG -= -Zc:strictStrings
win32: QMAKE_CFLAGS -= -Zc:strictStrings
win32: QMAKE_CXXFLAGS -= -Zc:strictStrings
LIBS += -L"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.10240.0/um/x64"
LIBS += -lshell32 -luser32 -ldxguid -lcomctl32 -lole32 -loleaut32 -ldinput8
来源:https://stackoverflow.com/questions/9742611/usb-joystick-with-c-and-directinput