Joystick reading in Form Application | Member | | Join Date: Oct 2008
Posts: 50
| | |
Hi,
I have a Windows Form Application with one button and a textbox. How should I approach if I want the program to display the X value of a joystick plugged in the USB port of the PC when the button is pressed? I heard from a friend it should only take around 20 lines to do so.
At first I tried using an open source library called libusb-win32; but even though I could get the program to find the device according to its IDs I wasn't able to read values off it. Then I tried to use DirectInput in DirectX SDK and followed the tutorial on msdn, but the callback functions in it didn't work because that tutorial seems to be for MFC applications or Windows API.
The problem is basically "Joystick reading in Form Application"; sounds simple but I've stucked on this for at least 30 hours :(. If you know ways of doing this or sample code on the net, please give some advices.
Thanks in advance
ps. new thread 'cause the problem's no longer limited to DirectInput...
| | Member | | Join Date: Oct 2008
Posts: 50
| | | re: Joystick reading in Form Application
I think I also have it now. This is just a form with a textbox and a button which when pressed updates the X-axis value of joystick to the textbox.
Althought it doesn't seem to do anythinghh most of the time, I did encounter for once or twice that the value was updated correctly when the button is pressed. It seems completely random. - #define DIRECTINPUT_VERSION 0x0800
-
#include <windows.h>
-
#include <commctrl.h>
-
#include <basetsd.h>
-
#include <dinput.h>
-
#include <dinputd.h>
-
#include <assert.h>
-
#include <oleauto.h>
-
#include <shellapi.h>
-
#pragma warning( disable : 4996 ) // disable deprecated warning
-
#pragma managed(push, off)
-
#include <strsafe.h>
-
#pragma managed(pop)
-
#pragma warning( default : 4996 )
-
#include "resource.h"
-
-
// global variables
-
HINSTANCE g_hinst = GetModuleHandle(NULL);
-
HRESULT hr = CoInitialize(0);
-
LPDIRECTINPUT8 g_pDI = NULL;
-
LPDIRECTINPUTDEVICE8 joystick = NULL;
-
HWND hDlg = NULL;
-
-
int valueX = 0;
-
-
-
HRESULT CreateDevice(
-
REFGUID rguid,
-
LPDIRECTINPUTDEVICE * lplpDirectInputDevice,
-
LPUNKNOWN pUnkOuter
-
);
-
-
HRESULT EnumDevices(
-
DWORD dwDevType,
-
LPDIENUMDEVICESCALLBACK lpCallback,
-
LPVOID pvRef,
-
DWORD dwFlags
-
);
-
-
void initialise_global_vars_of_joystick()
-
{
-
g_hinst = GetModuleHandle(NULL);
-
hr = CoInitialize(0);
-
g_pDI = NULL;
-
joystick = NULL;
-
HWND hDlg = NULL;
-
}
-
-
BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext)
-
{
-
// Obtain an interface to the enumerated joystick.
-
hr = g_pDI->CreateDevice(pdidInstance->guidInstance, &joystick, NULL);
-
if(FAILED(hr))
-
return DIENUM_CONTINUE;
-
-
return DIENUM_STOP;
-
}
-
-
BOOL CALLBACK EnumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
-
{
-
hDlg = (HWND)context;
-
-
DIPROPRANGE propRange;
-
propRange.diph.dwSize = sizeof(DIPROPRANGE);
-
propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
-
propRange.diph.dwHow = DIPH_BYID;
-
propRange.diph.dwObj = instance->dwType;
-
propRange.lMin = 0;
-
propRange.lMax = +255;
-
-
// Set the range for the axis
-
if (FAILED(joystick->SetProperty(DIPROP_RANGE, &propRange.diph))) {
-
return DIENUM_STOP;
-
}
-
-
return DIENUM_CONTINUE;
-
}
-
-
HRESULT UpdateInputState(HWND hDlg)
-
{
-
CHAR strText[128]; // Device state text
-
DIJOYSTATE2 js; // Direct Input joystick state
-
CHAR* str;
-
-
if (NULL == joystick)
-
return S_OK;
-
-
// Poll the device to read the current state
-
hr = joystick->Poll();
-
if (FAILED(hr))
-
{
-
// Attempt to reacquire joystick
-
}
-
hr = joystick->GetDeviceState(sizeof(DIJOYSTATE2), &js);
-
if (FAILED(hr))
-
return hr;
-
-
valueX = js.lX;
-
js.lY;
-
js.rglSlider;
-
-
return hr;
-
}
-
-
-
#pragma once
-
-
-
namespace test {
-
-
using namespace System;
-
using namespace System::ComponentModel;
-
using namespace System::Collections;
-
using namespace System::Windows::Forms;
-
using namespace System::Data;
-
using namespace System::Drawing;
-
-
/// <summary>
-
/// Summary for Form1
-
///
-
/// WARNING: If you change the name of this class, you will need to change the
-
/// 'Resource File Name' property for the managed resource compiler tool
-
/// associated with all .resx files this class depends on. Otherwise,
-
/// the designers will not be able to interact properly with localized
-
/// resources associated with this form.
-
/// </summary>
-
public ref class Form1 : public System::Windows::Forms::Form
-
{
-
public:
-
Form1(void)
-
{
-
InitializeComponent();
-
//
-
//TODO: Add the constructor code here
-
//
-
-
}
-
-
/*virtual void WndProc(Message% m) override {
-
if(m.Msg == WM_DEVICECHANGE) {
-
//do stuff
-
}
-
}*/
-
-
void detect(void)
-
{
-
hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI, NULL);
-
if FAILED(hr)
-
{
-
// DirectInput not available; take appropriate action
-
}
-
-
//g_pDI is an initialized pointer to IDirectInput8
-
g_pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY);
-
-
if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick2)))
-
{
-
// DirectInput not available; take appropriate action
-
}
-
-
if (FAILED(hr = joystick->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE | DISCL_FOREGROUND)))
-
{
-
-
}
-
-
DIDEVCAPS g_diDevCaps;
-
g_diDevCaps.dwSize = sizeof(DIDEVCAPS);
-
if (FAILED(hr = joystick->GetCapabilities(&g_diDevCaps)))
-
{
-
-
}
-
-
if (FAILED(hr = joystick->EnumObjects(EnumAxesCallback, NULL, DIDFT_AXIS)))
-
{
-
-
}
-
-
hr = joystick->Poll();
-
if (FAILED(hr))
-
{
-
hr = joystick->Acquire();
-
while(hr == DIERR_INPUTLOST)
-
hr = joystick->Acquire();
-
// return S_OK;
-
}
-
-
hr = UpdateInputState(hDlg);
-
}
-
-
-
protected:
-
/// <summary>
-
/// Clean up any resources being used.
-
/// </summary>
-
~Form1()
-
{
-
if (components)
-
{
-
delete components;
-
}
-
}
-
private: System::Windows::Forms::TextBox^ textBox1;
-
protected:
-
private: System::Windows::Forms::Button^ button1;
-
-
private:
-
/// <summary>
-
/// Required designer variable.
-
/// </summary>
-
System::ComponentModel::Container ^components;
-
-
-
-
#pragma region Windows Form Designer generated code
-
/// <summary>
-
/// Required method for Designer support - do not modify
-
/// the contents of this method with the code editor.
-
/// </summary>
-
void InitializeComponent(void)
-
{
-
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
-
this->button1 = (gcnew System::Windows::Forms::Button());
-
this->SuspendLayout();
-
//
-
// textBox1
-
//
-
this->textBox1->Location = System::Drawing::Point(112, 101);
-
this->textBox1->Name = L"textBox1";
-
this->textBox1->Size = System::Drawing::Size(100, 22);
-
this->textBox1->TabIndex = 0;
-
this->textBox1->Text = L"default";
-
//
-
// button1
-
//
-
this->button1->Location = System::Drawing::Point(82, 147);
-
this->button1->Name = L"button1";
-
this->button1->Size = System::Drawing::Size(75, 23);
-
this->button1->TabIndex = 1;
-
this->button1->Text = L"button1";
-
this->button1->UseVisualStyleBackColor = true;
-
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
-
//
-
// Form1
-
//
-
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
-
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
-
this->ClientSize = System::Drawing::Size(292, 266);
-
this->Controls->Add(this->button1);
-
this->Controls->Add(this->textBox1);
-
this->Name = L"Form1";
-
this->Text = L"Form1";
-
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
-
this->ResumeLayout(false);
-
this->PerformLayout();
-
-
}
-
#pragma endregion
-
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
-
}
-
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
-
try {
-
initialise_global_vars_of_joystick();
-
detect();
-
} catch (Exception ^ex) {
-
MessageBox:: Show("No device detected!", "Error");
-
}
-
textBox1->Text = valueX + "";
-
}
-
};
-
}
I also heard that overriding WndProc(Message% m) is a way to go, but I don't know how to do that...
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|