//About raw input see MSDN Https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536%28v=vs.85%29.aspx
Efficiency is much higher than keyboard hooks, but rawinputdevice structure and raw related functions are not recognized in BCB
//rawinput4.cpp: Defines the entry point for the application. //#include"stdafx.h"#include"rawinput4.h"#include<Set>#include<string>#include<algorithm>using namespacestd;#defineMax_loadstring 100//Global Variables:HINSTANCE HInst;//Current InstanceTCHAR sztitle[max_loadstring];//title bar TextTCHAR szwindowclass[max_loadstring];//main window class nameSet<DWORD>keys;//avoid pressing and holding the response KeyDownBOOLIsinkeys (DWORD key) {Set<dword>::iterator Itor =Find (Keys.begin (), Keys.end (), key); BOOLresult =false; if(Itor = =Keys.end ()) {Keys.insert (key); Result=false; } Else{result=true; } returnresult;}//when released, remove key from setvoidErasekey (DWORD key) {keys.erase (key);}//The forward declaration of the function contained in this code module:ATOM MyRegisterClass (hinstance hinstance); BOOL InitInstance (HINSTANCE,int); LRESULT CALLBACK WndProc (hwnd, UINT, WPARAM, LPARAM); Int_ptr CALLBACK About (hwnd, uint, WPARAM, LPARAM);voidRegkeyboardrawinput (HWND hwnd) {Rawinputdevice rid[1]; rid[0].ususagepage =0x01; rid[0].ususage =0x06; rid[0].dwflags = Ridev_inputsink;//ridev_nolegacy; //adds HID keyboard and also ignores legacy keyboard messagesrid[0].hwndtarget =hwnd; if(Registerrawinputdevices (Rid,1,sizeof(rid[0])) ==FALSE) {MessageBox (null,l"registering raw input failed! ", L"Register Raw Input", mb_ok+mb_iconinformation); }}intapientry _tWinMain (hinstance hinstance, HInstance hprevinstance, LPTSTR LP CmdLine,intncmdshow) {Unreferenced_parameter (hprevinstance); Unreferenced_parameter (lpCmdLine); MSG msg; Haccel hacceltable; //Initializing global StringsLoadString (hinstance, Ids_app_title, SzTitle, max_loadstring); LoadString (HInstance, Idc_rawinput4, Szwindowclass, max_loadstring); MyRegisterClass (HINSTANCE); //To perform an application initialization: if(!InitInstance (HINSTANCE, nCmdShow)) { returnFALSE; } hacceltable=loadaccelerators (hinstance, Makeintresource (IDC_RAWINPUT4)); //main message loop: while(GetMessage (&msg, NULL,0,0)) { if(! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg); DispatchMessage (&msg); } } return(int) Msg.wparam;}////function: MyRegisterClass ()////Purpose: Registers the window class. ////notes:////only if you want to//This code is associated with "RegisterClassEx" added to Windows 95//this function and its usage are only required when the WIN32 system is compatible with the previous function. It is important to call this function,//This allows the application to obtain an associated//"Well-formed" small icon. //ATOM MyRegisterClass (hinstance hinstance) {wndclassex Wcex; Wcex.cbsize=sizeof(Wndclassex); Wcex.style= Cs_hredraw |Cs_vredraw; Wcex.lpfnwndproc=WndProc; Wcex.cbclsextra=0; Wcex.cbwndextra=0; Wcex.hinstance=hinstance; Wcex.hicon=LoadIcon (hinstance, Makeintresource (IDI_RAWINPUT4)); Wcex.hcursor=loadcursor (NULL, Idc_arrow); Wcex.hbrbackground= (Hbrush) (color_window+1); Wcex.lpszmenuname=Makeintresource (IDC_RAWINPUT4); Wcex.lpszclassname=Szwindowclass; WCEX.HICONSM=LoadIcon (Wcex.hinstance, Makeintresource (Idi_small)); returnRegisterClassEx (&Wcex);}////function: InitInstance (hinstance, int)////Purpose: Save the instance handle and create the main window////notes:////in this function, we save the instance handle in the global variable and//Create and display the main program window. //BOOL InitInstance (HInstance hinstance,intncmdshow) {HWND hwnd; HInst= HInstance;//storing an instance handle in a global variablehWnd=CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault,0, Cw_usedefault,0, NULL, NULL, HINSTANCE, NULL); if(!hWnd) { returnFALSE; } ShowWindow (HWnd, ncmdshow); UpdateWindow (HWND); Regkeyboardrawinput (HWND); returnTRUE;}////functions: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Processes the message of the main window. ////wm_command-Processing Application Menu//WM_PAINT-Draw the main window//Wm_destroy-Sends an exit message and returns////LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {intWmid, wmevent; Paintstruct PS; HDC hdc; Switch(message) { CaseWm_command:wmid=LoWord (WParam); Wmevent=HiWord (WParam); //Analysis Menu Selection: Switch(wmid) { CaseIdm_about:dialogbox (HInst, Makeintresource (Idd_aboutbox), hWnd, about); Break; CaseIdm_exit:destroywindow (HWND); Break; default: returnDefWindowProc (hWnd, message, WParam, LParam); } Break; CaseWM_PAINT:HDC= BeginPaint (HWnd, &PS); //TODO: Add any drawing code here ...EndPaint (HWnd, &PS); Break; CaseWm_destroy:postquitmessage (0); Break; Casewm_input: {UINT dwsize; Getrawinputdata (hrawinput) LParam, Rid_input, NULL,&dwsize,sizeof(Rawinputheader)); Lpbyte LPB=NewByte[dwsize]; if(LPB = =NULL) { return 0; } if(Getrawinputdata (Hrawinput) LParam, Rid_input, LPB, &dwsize,sizeof(Rawinputheader)) !=dwsize) OutputDebugString (TEXT ("Getrawinputdata doesn ' t return correct size!\n")); Rawinput* Raw = (rawinput*) LPB; if(Raw->header.dwtype = =Rim_typekeyboard) { if(Raw->data.keyboard.message = =wm_keydown) { if(! Isinkeys (raw->Data.keyboard.VKey)) {Charkeytext[Ten] = {0}; BYTE state[ the] = {0}; //get a name by using a virtual keyboard codeToascii (Raw->data.keyboard.vkey,raw->data.keyboard.makecode,state, (Lpword) Keytext,0); Charbuf[Ten] = {0}; strings ="the keyboard presses the virtual key code:"+string(Keytext); MessageBoxA (Null,s.c_str (),"keyboard", MB_OK); } } if(Raw->data.keyboard.message = =wm_keyup) {Erasekey (Raw-Data.keyboard.VKey); } } Delete[] LPB; return 0; } default: returnDefWindowProc (hWnd, message, WParam, LParam); } return 0;}//The message handler for the About box. int_ptr CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); Switch(message) { CaseWm_initdialog:return(INT_PTR) TRUE; CaseWm_command:if(LoWord (wParam) = = IDOK | | LoWord (wParam) = =IDCANCEL) {EndDialog (hdlg, LoWord (WParam)); return(INT_PTR) TRUE; } Break; } return(INT_PTR) FALSE;}
Use raw input instead of global keyboard hooks