#pragma comment(lib, "User32.lib")
RegisitKeyBord(this->m_hWnd);
BOOL RegisitKeyBord(HWND hwnd)
{
if (NULL == hwnd)
return false;
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x01;
rid.usUsage = 0x06;
rid.dwFlags = RIDEV_INPUTSINK;
rid.hwndTarget = hwnd;
return RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
}
BOOL CInterfaceView::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE)
{
return TRUE;
}
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_F5)
{
return TRUE;
}
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
{
return TRUE;
}
if (pMsg->message == WM_INPUT)
{
GetDlgItem(IDC_PROCESS_CARD_NUM)->SetFocus();
PostMessage(NULL, WM_SETCURSOR, (WPARAM)IDC_PROCESS_CARD_NUM);
p_Start = GetTickCount();
np_Start = (DWORD)p_Start;
UINT dwSize;
GetRawInputData((HRAWINPUT)pMsg->lParam, RID_INPUT, NULL, &dwSize,
sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL)
{
return 0;
}
if (GetRawInputData((HRAWINPUT)pMsg->lParam, RID_INPUT, lpb, &dwSize,
sizeof(RAWINPUTHEADER)) != dwSize)
OutputDebugString(TEXT("GetRawInputData doesn't return correct size !\n"));
int type = 0;
RAWINPUT* raw = (RAWINPUT*)lpb;
{
GetRawInputDeviceInfo
(raw->header.hDevice,
RIDI_DEVICENAME,
NULL,
&dwSize);
WCHAR stringBuffer[1024];
GetRawInputDeviceInfo(raw->header.hDevice, RIDI_DEVICENAME, stringBuffer,
&dwSize);
std::string tmp = WChar2Ansi(stringBuffer);
transform(tmp.begin(), tmp.end(), tmp.begin(), ::toupper);
if (tmp.find("VID_060E") != std::string::npos)
{
type = 1;
}
if (type == 0)
{
delete[] lpb;
return 0;
type = 1;
}
}
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
if (raw->data.keyboard.Message == WM_KEYDOWN)
{
char keytext[50] = { 0 };
BYTE state[256] = { 0 };
ToAscii(raw->data.keyboard.VKey, raw->data.keyboard.MakeCode, state, (LPWORD)keytext, 0);
if (g_OpenSign == 1)
{
p_End = GetTickCount();
np_End = (DWORD)p_End;
g_OpenSign = 0;
}
if (np_Start - np_End > 500)
{
g_scan = "";
}
g_scan += keytext;
if (GetTickCount() - g_scaner_data[type].second > 2000)
{
g_scaner_data[type].first = "";
}
std::string org_text = g_scaner_data[type].first;
g_scaner_data[type] = std::make_pair(org_text + keytext, GetTickCount());
}
}
g_ScanSign = 1;
p_End = GetTickCount();
np_End = (DWORD)p_End;
delete[] lpb;
return 0;
}
else if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
{
return TRUE;
}
else
return CDialog::PreTranslateMessage(pMsg);
}
string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen <= 0) return string("");
char* pszDst = new char[nLen];
if (NULL == pszDst) return string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen - 1] = 0;
string strTemp(pszDst);
delete[] pszDst;
return strTemp;
}
std::map<int, std::pair<std::string, DWORD>> g_scaner_data;
CString g_scan = L"";
bool g_ScanSign = 0;