Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualAntiMight

Posted 04 October 2012 - 10:59 PM

Now I modified the callback function,and print out the type string of all devices. I got two different guidinstance code from single usb keyboard, but none of them work. I can only get output from my PS/2 keyboard. So Did I make mistakes creating usb keyboard? Sorry for my bad English, I'm not a English speaker. and here is the modified code}
#include <iostream>
#include <dinput.h>
#include <string>
#include <vector>
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")
#define DIRECTINPUT_VERSION 0x0800
struct Keyboard
{
IDirectInputDevice8* device;
GUID guid;
char key[256];
};
#define input Input::Instance()
#define MAXDEVICES 10
class Input
{
private:
IDirectInput8* dinput;
public:
std::vector<Keyboard> keyboards;
private:
Input()
{
  DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,
   IID_IDirectInput8,(LPVOID*)&dinput, NULL);
}
Input(const Input&);
Input& operator = (const Input&);
public:
static Input* Instance()
{
  static Input instance;
  return &instance;
}
bool CreateKeyboards()
{
  if(dinput==NULL) return false;
  HWND hwnd = GetConsoleWindow();
  if(FAILED(dinput->EnumDevices(DI8DEVCLASS_ALL,
   EnumKeyboardsCallBack,
   NULL,
   DIEDFL_ALLDEVICES)))
  {
   MessageBox(hwnd,"EnumDevices() - FAILED", "Error",0);
   return false;
  }
  int numdevice = keyboards.size();
  for(int i = 0; i < numdevice; i++)
  {
   if(FAILED(keyboards[i].device->SetDataFormat(&c_dfDIKeyboard)))
   {
	MessageBox(hwnd, "SetDataFormat() - FAILED","Error",0);
	return false;
   }
   if(FAILED(keyboards[i].device->SetCooperativeLevel(hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
   {
	MessageBox(hwnd, "SetCooperativeLevel() - FAILED", "Error",0);
	return false;
   }
   if(FAILED(keyboards[i].device->Acquire()))
   {
	MessageBox(hwnd, "Acquire() - FAILED", "Error",0);
	return false;
   }
   return true;
  }
}
static BOOL CALLBACK  EnumKeyboardsCallBack(LPCDIDEVICEINSTANCE lpddi,
			LPVOID pvRef)
{
  std::cout << lpddi->tszInstanceName <<":	"<<lpddi->guidInstance.Data1 << std::endl;
  std::string typeString = lpddi->tszInstanceName;
  static int numdevices = 0;
  if(numdevices < MAXDEVICES )
  {
   if(typeString == "Keyboard"|| typeString=="USB Keyboard"||typeString == "USB Combo Keyboard" )
   {
	IDirectInputDevice8* device;
	input->dinput->CreateDevice(lpddi->guidInstance, &device, NULL);
	Keyboard keyboard = { device, lpddi->guidInstance };
	input->keyboards.push_back(keyboard);
	numdevices++;
   }
  return DIENUM_CONTINUE;
  }
  return DIENUM_STOP;
}
void printKeyboardsInfo()
{
  int size = keyboards.size();
  std::cout <<std::endl <<"Totle "<<size <<" keyboards attached"<<"\n\n";
  for(int i = 0; i < size; i++)
  {
   std::cout <<"Keyboard"<<i<<":"<< keyboards[i].guid.Data1 << "\n\n";
  }
}
int KeyboardNumber() { return keyboards.size(); }
};
int main()
{
if(!input->CreateKeyboards()) return 0;
input->printKeyboardsInfo();
for(int i = 0;i < input->keyboards.size(); i++)
{
  std::cout << "keyboard Index:"<<i<<std::endl;
  while(!(input->keyboards[i].key[DIK_N] & 0x80))
  {
   ZeroMemory(input->keyboards[i].key,256*sizeof(char));
   input->keyboards[i].device->GetDeviceState(256*sizeof(char),(LPVOID)&input->keyboards[i].key);
   if(input->keyboards[i].key[DIK_1] & 0x80) {
	std::cout << "new input,device guid:"<<input->keyboards[i].guid.Data1<<std::endl;
	Sleep(500);
   }
  }
}
system("pause");
return 0;
}
here is the output,usb keyboard doesn't response.[attachment=11627:kt.png]

#3AntiMight

Posted 04 October 2012 - 10:42 PM

Now I modified the callback function,and print out the type string of all devices. I got two different guidinstance code from single usb keyboard, but none of them work. I can only get output from my PS/2 keyboard. So Did I make mistakes creating usb keyboard? Sorry for my bad English, I'm not a English speaker. and here is the modified code}
#include <iostream>
#include <dinput.h>
#include <string>
#include <vector>
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")
#define DIRECTINPUT_VERSION 0x0800
struct Keyboard
{
IDirectInputDevice8* device;
GUID guid;
char key[255];
};
#define input Input::Instance()
#define MAXDEVICES 10
class Input
{
private:
IDirectInput8* dinput;
public:
std::vector<Keyboard> keyboards;
private:
Input()
{
  DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,
   IID_IDirectInput8,(LPVOID*)&dinput, NULL);
}
Input(const Input&);
Input& operator = (const Input&);
public:
static Input* Instance()
{
  static Input instance;
  return &instance;
}
bool CreateKeyboards()
{
  if(dinput==NULL) return false;
  HWND hwnd = GetConsoleWindow();
  if(FAILED(dinput->EnumDevices(DI8DEVCLASS_ALL,
   EnumKeyboardsCallBack,
   NULL,
   DIEDFL_ALLDEVICES)))
  {
   MessageBox(hwnd,"EnumDevices() - FAILED", "Error",0);
   return false;
  }
  int numdevice = keyboards.size();
  for(int i = 0; i < numdevice; i++)
  {
   if(FAILED(keyboards[i].device->SetDataFormat(&c_dfDIKeyboard)))
   {
	MessageBox(hwnd, "SetDataFormat() - FAILED","Error",0);
	return false;
   }
   if(FAILED(keyboards[i].device->SetCooperativeLevel(hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
   {
	MessageBox(hwnd, "SetCooperativeLevel() - FAILED", "Error",0);
	return false;
   }
   if(FAILED(keyboards[i].device->Acquire()))
   {
	MessageBox(hwnd, "Acquire() - FAILED", "Error",0);
	return false;
   }
   return true;
  }
}
static BOOL CALLBACK  EnumKeyboardsCallBack(LPCDIDEVICEINSTANCE lpddi,
			LPVOID pvRef)
{
  std::cout << lpddi->tszInstanceName <<":	"<<lpddi->guidInstance.Data1 << std::endl;
  std::string typeString = lpddi->tszInstanceName;
  static int numdevices = 0;
  if(numdevices < MAXDEVICES )
  {
   if(typeString == "Keyboard"|| typeString=="USB Keyboard"||typeString == "USB Combo Keyboard" )
   {
	IDirectInputDevice8* device;
	input->dinput->CreateDevice(lpddi->guidInstance, &device, NULL);
	Keyboard keyboard = { device, lpddi->guidInstance };
	input->keyboards.push_back(keyboard);
	numdevices++;
   }
  return DIENUM_CONTINUE;
  }
  return DIENUM_STOP;
}
void printKeyboardsInfo()
{
  int size = keyboards.size();
  std::cout <<std::endl <<"Totle "<<size <<" keyboards attached"<<"\n\n";
  for(int i = 0; i < size; i++)
  {
   std::cout <<"Keyboard"<<i<<":"<< keyboards[i].guid.Data1 << "\n\n";
  }
}
int KeyboardNumber() { return keyboards.size(); }
};
int main()
{
if(!input->CreateKeyboards()) return 0;
input->printKeyboardsInfo();
for(int i = 0;i < input->keyboards.size(); i++)
{
  std::cout << "keyboard Index:"<<i<<std::endl;
  while(!(input->keyboards[i].key[DIK_N] & 0x80))
  {
   ZeroMemory(input->keyboards[i].key,256*sizeof(char));
   input->keyboards[i].device->GetDeviceState(256*sizeof(char),(LPVOID)&input->keyboards[i].key);
   if(input->keyboards[i].key[DIK_1] & 0x80) {
	std::cout << "new input,device guid:"<<input->keyboards[i].guid.Data1<<std::endl;
	Sleep(500);
   }
  }
}
system("pause");
return 0;
}
here is the output,usb keyboard doesn't response.[attachment=11627:kt.png]

#2AntiMight

Posted 04 October 2012 - 10:38 PM

Now I modified the callback function,and print out the type string of all devices. I got two different guidinstance code from single usb keyboard, but none of them work. I can only get output from my PS/2 keyboard. So Did I make mistakes creating usb keyboard? Sorry for my bad English, I'm not a English speaker. and here is the modified code}
#include <iostream>
#include <dinput.h>
#include <string>
#include <vector>
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")
#define DIRECTINPUT_VERSION 0x0800
struct Keyboard
{
IDirectInputDevice8* device;
GUID guid;
char key[255];
};
#define input Input::Instance()
#define MAXDEVICES 10
class Input
{
private:
IDirectInput8* dinput;
public:
std::vector<Keyboard> keyboards;
private:
Input()
{
  DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,
   IID_IDirectInput8,(LPVOID*)&dinput, NULL);
}
Input(const Input&);
Input& operator = (const Input&);
public:
static Input* Instance()
{
  static Input instance;
  return &instance;
}
bool CreateKeyboards()
{
  if(dinput==NULL) return false;
  HWND hwnd = GetConsoleWindow();
  if(FAILED(dinput->EnumDevices(DI8DEVCLASS_ALL,
   EnumKeyboardsCallBack,
   NULL,
   DIEDFL_ALLDEVICES)))
  {
   MessageBox(hwnd,"EnumDevices() - FAILED", "Error",0);
   return false;
  }
  int numdevice = keyboards.size();
  for(int i = 0; i < numdevice; i++)
  {
   if(FAILED(keyboards[i].device->SetDataFormat(&c_dfDIKeyboard)))
   {
	MessageBox(hwnd, "SetDataFormat() - FAILED","Error",0);
	return false;
   }
   if(FAILED(keyboards[i].device->SetCooperativeLevel(hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
   {
	MessageBox(hwnd, "SetCooperativeLevel() - FAILED", "Error",0);
	return false;
   }
   if(FAILED(keyboards[i].device->Acquire()))
   {
	MessageBox(hwnd, "Acquire() - FAILED", "Error",0);
	return false;
   }
   return true;
  }
}
static BOOL CALLBACK  EnumKeyboardsCallBack(LPCDIDEVICEINSTANCE lpddi,
			LPVOID pvRef)
{
  std::cout << lpddi->tszInstanceName <<":	"<<lpddi->guidInstance.Data1 << std::endl;
  std::string typeString = lpddi->tszInstanceName;
  static int numdevices = 0;
  if(numdevices < MAXDEVICES )
  {
   if(typeString == "Keyboard"|| typeString=="USB Keyboard"||typeString == "USB Combo Keyboard" )
   {
	IDirectInputDevice8* device;
	input->dinput->CreateDevice(lpddi->guidInstance, &device, NULL);
	Keyboard keyboard = { device, lpddi->guidInstance };
	input->keyboards.push_back(keyboard);
	numdevices++;
   }
  return DIENUM_CONTINUE;
  }
  return DIENUM_STOP;
}
void printKeyboardsInfo()
{
  int size = keyboards.size();
  std::cout <<std::endl <<"Totle "<<size <<" keyboards attached"<<"\n\n";
  for(int i = 0; i < size; i++)
  {
   std::cout <<"Keyboard"<<i<<":"<< keyboards[i].guid.Data1 << "\n\n";
  }
}
int KeyboardNumber() { return keyboards.size(); }
};
int main()
{
if(!input->CreateKeyboards()) return 0;
input->printKeyboardsInfo();
for(int i = 0;i < input->keyboards.size(); i++)
{
  std::cout << "keyboard Index:"<<i<<std::endl;
  while(!(input->keyboards[i].key[DIK_N] & 0x80))
  {
   ZeroMemory(input->keyboards[i].key,256*sizeof(char));
   input->keyboards[i].device->GetDeviceState(256*sizeof(char),(LPVOID)&input->keyboards[i].key);
   if(input->keyboards[i].key[DIK_1] & 0x80) {
	std::cout << "new input,device guid:"<<input->keyboards[i].guid.Data1<<std::endl;
	Sleep(500);
   }
  }
}
system("pause");
return 0;
}[code]
}here is the output,usb keyboard doesn't response.
[attachment=11627:kt.png]

#1AntiMight

Posted 04 October 2012 - 10:31 PM

Now I modified the callback function,and print out the type string of all devices. I got two different guidinstance code from single usb keyboard, but none of them work. I can only get output from my PS/2 keyboard. So Did I make mistakes creating usb keyboard? Sorry for my bad English, I'm not a English speaker. and here is the modified code}
#include <iostream>
#include <dinput.h>
#include <string>
#include <vector>
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")
#define DIRECTINPUT_VERSION 0x0800
struct Keyboard
{
IDirectInputDevice8* device;
GUID guid;
char key[255];
};
#define input Input::Instance()
#define MAXDEVICES 10
class Input
{
private:
IDirectInput8* dinput;
public:
std::vector<Keyboard> keyboards;
private:
Input()
{
  DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,
   IID_IDirectInput8,(LPVOID*)&dinput, NULL);
}
Input(const Input&);
Input& operator = (const Input&);
public:
static Input* Instance()
{
  static Input instance;
  return &instance;
}
bool CreateKeyboards()
{
  if(dinput==NULL) return false;
  HWND hwnd = GetConsoleWindow();
  if(FAILED(dinput->EnumDevices(DI8DEVCLASS_ALL,
   EnumKeyboardsCallBack,
   NULL,
   DIEDFL_ALLDEVICES)))
  {
   MessageBox(hwnd,"EnumDevices() - FAILED", "Error",0);
   return false;
  }
  int numdevice = keyboards.size();
  for(int i = 0; i < numdevice; i++)
  {
   if(FAILED(keyboards[i].device->SetDataFormat(&c_dfDIKeyboard)))
   {
    MessageBox(hwnd, "SetDataFormat() - FAILED","Error",0);
    return false;
   }
   if(FAILED(keyboards[i].device->SetCooperativeLevel(hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
   {
    MessageBox(hwnd, "SetCooperativeLevel() - FAILED", "Error",0);
    return false;
   }
   if(FAILED(keyboards[i].device->Acquire()))
   {
    MessageBox(hwnd, "Acquire() - FAILED", "Error",0);
    return false;
   }
   return true;
  }
}
static BOOL CALLBACK  EnumKeyboardsCallBack(LPCDIDEVICEINSTANCE lpddi,
		    LPVOID pvRef)
{
  std::cout << lpddi->tszInstanceName <<":	"<<lpddi->guidInstance.Data1 << std::endl;
  std::string typeString = lpddi->tszInstanceName;
  static int numdevices = 0;
  if(numdevices < MAXDEVICES )
  {
   if(typeString == "Keyboard"|| typeString=="USB Keyboard"||typeString == "USB Combo Keyboard" )
   {
    IDirectInputDevice8* device;
    input->dinput->CreateDevice(lpddi->guidInstance, &device, NULL);
    Keyboard keyboard = { device, lpddi->guidInstance };
    input->keyboards.push_back(keyboard);
    numdevices++;
   }
  return DIENUM_CONTINUE;
  }
  return DIENUM_STOP;
}
void printKeyboardsInfo()
{
  int size = keyboards.size();
  std::cout <<std::endl <<"Totle "<<size <<" keyboards attached"<<"\n\n";
  for(int i = 0; i < size; i++)
  {
   std::cout <<"Keyboard"<<i<<":"<< keyboards[i].guid.Data1 << "\n\n";
  }
}
int KeyboardNumber() { return keyboards.size(); }
};
int main()
{
if(!input->CreateKeyboards()) return 0;
input->printKeyboardsInfo();
for(int i = 0;i < input->keyboards.size(); i++)
{
  std::cout << "keyboard Index:"<<i<<std::endl;
  while(!(input->keyboards[i].key[DIK_N] & 0x80))
  {
   ZeroMemory(input->keyboards[i].key,256*sizeof(char));
   input->keyboards[i].device->GetDeviceState(256*sizeof(char),(LPVOID)&input->keyboards[i].key);
   if(input->keyboards[i].key[DIK_1] & 0x80) {
    std::cout << "new input,device guid:"<<input->keyboards[i].guid.Data1<<std::endl;
    Sleep(500);
   }
  }
}
system("pause");
return 0;
}

PARTNERS