Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Low-Level Keyboard Input Detection

Low-Level Keyboard Input Detection

Scheduled Pinned Locked Moved C / C++ / MFC
androidioslinuxhardwarequestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Don Guy
    wrote on last edited by
    #1

    Hey there, I am writing an software that should be able to detect all keyboard inputs by the user. i.e., both hardware and software/on-screen keyboard. This software is going to be written in a platform independent way and supposed to run on Windows, Linux, Android & iOS. The idea is to capture the keyboard inputs from a low level, there by making sure that it doesn't miss any inputs even if it's a on-screen keyboard like in a mobile device. I am looking at possible open source libraries that can be used. Anyone got any good ideas on what to use? Thanks in advance!

    L R S 3 Replies Last reply
    0
    • D Don Guy

      Hey there, I am writing an software that should be able to detect all keyboard inputs by the user. i.e., both hardware and software/on-screen keyboard. This software is going to be written in a platform independent way and supposed to run on Windows, Linux, Android & iOS. The idea is to capture the keyboard inputs from a low level, there by making sure that it doesn't miss any inputs even if it's a on-screen keyboard like in a mobile device. I am looking at possible open source libraries that can be used. Anyone got any good ideas on what to use? Thanks in advance!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Given that those platforms have very different operating systems and hardware it is unlikely that you will find a library that can handle each type, especially at the hardware level. If you want open source code then Google should be your search tool.

      1 Reply Last reply
      0
      • D Don Guy

        Hey there, I am writing an software that should be able to detect all keyboard inputs by the user. i.e., both hardware and software/on-screen keyboard. This software is going to be written in a platform independent way and supposed to run on Windows, Linux, Android & iOS. The idea is to capture the keyboard inputs from a low level, there by making sure that it doesn't miss any inputs even if it's a on-screen keyboard like in a mobile device. I am looking at possible open source libraries that can be used. Anyone got any good ideas on what to use? Thanks in advance!

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        A cross-plattform key-logger :~ ? This sounds like the ultimate trojan, so finding resources will not be easy...

        ~RaGE();

        I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Entropy isn't what it used to.

        1 Reply Last reply
        0
        • D Don Guy

          Hey there, I am writing an software that should be able to detect all keyboard inputs by the user. i.e., both hardware and software/on-screen keyboard. This software is going to be written in a platform independent way and supposed to run on Windows, Linux, Android & iOS. The idea is to capture the keyboard inputs from a low level, there by making sure that it doesn't miss any inputs even if it's a on-screen keyboard like in a mobile device. I am looking at possible open source libraries that can be used. Anyone got any good ideas on what to use? Thanks in advance!

          S Offline
          S Offline
          Software_Developer
          wrote on last edited by
          #4

          For Windows, here is a global key hook example, prints pressed keycodes to the console. It even works when it's in the background. And this is a Windows discussion board, also ask in Linux, Android and IOS sections for answers..

          #include
          #include

          HHOOK hKeyboardHook;

          __declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
          {
          DWORD SHIFT_key=0;
          DWORD CTRL_key=0;
          DWORD ALT_key=0;

          if  ((nCode == HC\_ACTION) &&   ((wParam == WM\_SYSKEYDOWN) ||  (wParam == WM\_KEYDOWN)))      
          {
          	KBDLLHOOKSTRUCT hooked\_key = 	\*((KBDLLHOOKSTRUCT\*)lParam);
          	DWORD dwMsg = 1;
          	dwMsg += hooked\_key.scanCode << 16;
          	dwMsg += hooked\_key.flags << 24;
          	char lpszKeyName\[1024\] = {0};
          	lpszKeyName\[0\] = '\[';
          
          	int i = GetKeyNameText(dwMsg,	(lpszKeyName+1),0xFF) + 1;
          	lpszKeyName\[i\] = '\]';
          
          	int key = hooked\_key.vkCode;
          
          	SHIFT\_key = GetAsyncKeyState(VK\_SHIFT);
          	CTRL\_key = GetAsyncKeyState(VK\_CONTROL);
              ALT\_key = GetAsyncKeyState(VK\_MENU);
          
          	if (key >= 'A' && key <= 'Z')   mmmmmmcjmcfkgkgcgkj.j.g     
          	{
          		 
          		if  (GetAsyncKeyState(VK\_SHIFT)>= 0) key +=32;
          
          		if (CTRL\_key !=0 && key == 'y' )
          		{
                     MessageBox(NULL, "CTRL-y was pressed\\nLaunch your app here", "H O T K E Y", MB\_OK); 
                     CTRL\_key=0;
          		}
          
          		if (CTRL\_key !=0 && key == 'q' )
          		{
          			MessageBox(NULL, "Shutting down", "H O T K E Y", MB\_OK); 
                     PostQuitMessage(0);
          		}
          
          
          
          
          		printf("key = %c\\n", key);
          
          		SHIFT\_key = 0;
          		CTRL\_key = 0;
          		ALT\_key = 0;
          
          	}
          
          	printf("lpszKeyName = %s\\n",  lpszKeyName );
          }
          return CallNextHookEx(hKeyboardHook,	nCode,wParam,lParam);
          

          }

          void MessageLoop()
          {
          MSG message;
          while (GetMessage(&message,NULL,0,0))
          {
          TranslateMessage( &message );
          DispatchMessage( &message );
          }
          }

          DWORD WINAPI my_HotKey(LPVOID lpParm)
          {
          HINSTANCE hInstance = GetModuleHandle(NULL);
          if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
          if (!hInstance) return 1;

          hKeyboardHook = SetWindowsHookEx ( 	WH\_KEYBOARD\_LL, (HOOKPROC) KeyboardEvent,  	hInstance,  NULL    );
          MessageLoop();
          UnhookWindowsHookEx(hKeyboardHook);
          return 0;
          

          }

          int main(int argc, char** argv)
          {
          HANDLE hThread;
          DWORD dwThread;

          hThread = CreateThread(NULL,NULL,(LPTHREAD\_START\_ROUTINE) 	my\_HotKey, (LPVOID) argv\[0\], NULL, &dwThread);
          
          //ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
          
          if (hThread) return WaitForSingleObject(hThread,INFINI
          
          D 1 Reply Last reply
          0
          • S Software_Developer

            For Windows, here is a global key hook example, prints pressed keycodes to the console. It even works when it's in the background. And this is a Windows discussion board, also ask in Linux, Android and IOS sections for answers..

            #include
            #include

            HHOOK hKeyboardHook;

            __declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
            {
            DWORD SHIFT_key=0;
            DWORD CTRL_key=0;
            DWORD ALT_key=0;

            if  ((nCode == HC\_ACTION) &&   ((wParam == WM\_SYSKEYDOWN) ||  (wParam == WM\_KEYDOWN)))      
            {
            	KBDLLHOOKSTRUCT hooked\_key = 	\*((KBDLLHOOKSTRUCT\*)lParam);
            	DWORD dwMsg = 1;
            	dwMsg += hooked\_key.scanCode << 16;
            	dwMsg += hooked\_key.flags << 24;
            	char lpszKeyName\[1024\] = {0};
            	lpszKeyName\[0\] = '\[';
            
            	int i = GetKeyNameText(dwMsg,	(lpszKeyName+1),0xFF) + 1;
            	lpszKeyName\[i\] = '\]';
            
            	int key = hooked\_key.vkCode;
            
            	SHIFT\_key = GetAsyncKeyState(VK\_SHIFT);
            	CTRL\_key = GetAsyncKeyState(VK\_CONTROL);
                ALT\_key = GetAsyncKeyState(VK\_MENU);
            
            	if (key >= 'A' && key <= 'Z')   mmmmmmcjmcfkgkgcgkj.j.g     
            	{
            		 
            		if  (GetAsyncKeyState(VK\_SHIFT)>= 0) key +=32;
            
            		if (CTRL\_key !=0 && key == 'y' )
            		{
                       MessageBox(NULL, "CTRL-y was pressed\\nLaunch your app here", "H O T K E Y", MB\_OK); 
                       CTRL\_key=0;
            		}
            
            		if (CTRL\_key !=0 && key == 'q' )
            		{
            			MessageBox(NULL, "Shutting down", "H O T K E Y", MB\_OK); 
                       PostQuitMessage(0);
            		}
            
            
            
            
            		printf("key = %c\\n", key);
            
            		SHIFT\_key = 0;
            		CTRL\_key = 0;
            		ALT\_key = 0;
            
            	}
            
            	printf("lpszKeyName = %s\\n",  lpszKeyName );
            }
            return CallNextHookEx(hKeyboardHook,	nCode,wParam,lParam);
            

            }

            void MessageLoop()
            {
            MSG message;
            while (GetMessage(&message,NULL,0,0))
            {
            TranslateMessage( &message );
            DispatchMessage( &message );
            }
            }

            DWORD WINAPI my_HotKey(LPVOID lpParm)
            {
            HINSTANCE hInstance = GetModuleHandle(NULL);
            if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
            if (!hInstance) return 1;

            hKeyboardHook = SetWindowsHookEx ( 	WH\_KEYBOARD\_LL, (HOOKPROC) KeyboardEvent,  	hInstance,  NULL    );
            MessageLoop();
            UnhookWindowsHookEx(hKeyboardHook);
            return 0;
            

            }

            int main(int argc, char** argv)
            {
            HANDLE hThread;
            DWORD dwThread;

            hThread = CreateThread(NULL,NULL,(LPTHREAD\_START\_ROUTINE) 	my\_HotKey, (LPVOID) argv\[0\], NULL, &dwThread);
            
            //ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
            
            if (hThread) return WaitForSingleObject(hThread,INFINI
            
            D Offline
            D Offline
            Don Guy
            wrote on last edited by
            #5

            Thanks for the code! Does this work if it's a on-screen/software key board?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups