1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include "mainwindow.h"
#include <QApplication>
#include <windows.h>
#include <QDebug>
// register Scanner
void InitRawInput(HWND hWnd){
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x06;
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = hWnd;
if (RegisterRawInputDevices(Rid,1,sizeof(Rid[0])) == false)
{
qDebug() << "Registration failed";
return;
}
qDebug() << "Registration updated";
}
/*
* Function that receives messages from OS
*
* LRESULT --> returns a 32-bit number (64 for 64 OS)
* CALLBACK --> specifies how in assembly parameters will get passed
* and returned from the function
* HWND --> Handle to the window that is receiving messages from the OS
* UINT --> Says what type of message is being sent
* WPARAM/LPARAM --> es. X and Y mouse position
*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
qDebug() << "calback";
LPBYTE lpb;
UINT dwSize;
RAWINPUT *raw;
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_INPUT:
{
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
lpb = new BYTE[dwSize];
if (lpb == NULL)
return 0;
if (GetRawInputData((HRAWINPUT)lParam,
RID_INPUT,
lpb,
&dwSize,
sizeof(RAWINPUTHEADER)) != dwSize)
{
qDebug() << "GetRawInputData doesn't return correct siz";
//OutputDebugStr(TEXT("GetRawInputData doesn't return correct size\n"));
}
raw = (RAWINPUT*) lpb;
qDebug() << "whaa";
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
if (raw->data.keyboard.Message == WM_KEYDOWN ||
raw->data.keyboard.Message == WM_SYSKEYDOWN)
{
USHORT usKey;
usKey = raw->data.keyboard.VKey;
CHAR szTest[4];
_itoa_s((int)usKey,szTest,4,10);
WCHAR str[4];
MultiByteToWideChar( 0,0, szTest, 4, str, 4);
// SetWindowText(hWnd,szTest);
SetWindowText(hWnd,str);
}
}
delete[] lpb;
return 0;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
// TODO: code your application's behavior here.
UINT nDevices;
PRAWINPUTDEVICELIST pRawInputDeviceList;
if (GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST)) != 0)
{
qDebug() << "Errors...";
return 1;
}
if ((pRawInputDeviceList = (PRAWINPUTDEVICELIST)malloc(sizeof(RAWINPUTDEVICELIST) * nDevices)) == NULL)
{
qDebug() << "Initialization failed...";
return 1;
}
int nNoOfDevices = 0;
if ((nNoOfDevices = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST))) == ((UINT) - 1))
{
// Error
return 1;
}
RID_DEVICE_INFO rdi;
rdi.cbSize = sizeof(RID_DEVICE_INFO);
for(int i = 0; i < nNoOfDevices; i++)
{
UINT size = 256;
TCHAR tBuffer[256] = {0};
tBuffer[0] = '\0';
if(GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, tBuffer, &size) < 0)
{
// Error in reading device name
qDebug() << "error reading device name";
}
qDebug() << "Device Name: " << tBuffer;
//_tprintf(L"Device Name: %s\n", tBuffer);
UINT cbSize = rdi.cbSize;
if(GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice, RIDI_DEVICEINFO, &rdi, &cbSize) < 0)
{
// Error in reading information
}
if(rdi.dwType == RIM_TYPEMOUSE)
{
qDebug() << "ID for Mouse:" << rdi.mouse.dwId;
qDebug() << "Number of Buttons:" << rdi.mouse.dwNumberOfButtons;
qDebug() << "Sample rate(Number of data points):" << rdi.mouse.dwSampleRate;
qDebug() << "**************************";
}
if(rdi.dwType == RIM_TYPEKEYBOARD)
{
qDebug() << "Keyboard Mode:" << rdi.keyboard.dwKeyboardMode;
qDebug() << "Number of function keys:" << rdi.keyboard.dwNumberOfFunctionKeys;
qDebug() << "Number of indicators:" << rdi.keyboard.dwNumberOfIndicators;
qDebug() << "Number of keys total: " << rdi.keyboard.dwNumberOfKeysTotal;
qDebug() << "Type of the keyboard: " << rdi.keyboard.dwType;
qDebug() << "Subtype of the keyboard: " << rdi.keyboard.dwSubType;
qDebug() << "***********************";
}
if(rdi.dwType == RIM_TYPEHID)
{
qDebug() << "Vendor Id:" << rdi.hid.dwVendorId;
qDebug() << "Product Id:" << rdi.hid.dwProductId;
qDebug() << "Version No:" << rdi.hid.dwVersionNumber;
qDebug() << "Usage for the device: " << rdi.hid.usUsage;
qDebug() << "Usage Page for the device: " << rdi.hid.usUsagePage;
qDebug() << "***********************";
}
}
free(pRawInputDeviceList);
HWND temper = (HWND)w.effectiveWinId();
InitRawInput(temper);
// //Struct to hold windows event messages
// MSG msg;
// while (GetMessage(&msg, NULL,0,0))
// {
// qDebug() << "testersa";
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// }
// return msg.wParam;
return a.exec();
}
| |