欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++实现金山打字通助手

程序员文章站 2022-07-16 19:17:47
...

       在练字打字的过程之中,一直很好奇,这个打字速度到底可以有多快,目前我的英文的打字水平,估计也就是240字母/分钟。写出来这个打字程序也用于消遣使用,实现了自动打字的基本框架了。

C++实现金山打字通助手

(图一:金山打字通英文打字速度的测试界面) 

首先我们要思考一个问题,就是如何才能让程序自动打字?

在我们打字的过程之中,是敲击键盘实现打字的,那么就需要让程序自动的模拟按键,是实现打字的过程。还有一点要考虑的过程就是,如何让计算机知道自己应该模拟的是那些文章。遇到每一个字符是如何处理,遇到特殊的符号,怎么办还有大小写问题。

对于模拟按键问题,这一块在几个月我写过一篇关于模拟按键的问题,已经说的很详细了。

C/C++ 模拟键盘操作(一) https://blog.csdn.net/qq_40757240/article/details/105504524

C/C++ 模拟键盘操作(二)GetKeyState函数详细解释 https://blog.csdn.net/qq_40757240/article/details/105524622

C/C++ 模拟键盘操作(三)模拟鼠标输入 https://blog.csdn.net/qq_40757240/article/details/105543565

C/C++取指示灯状态    https://blog.csdn.net/qq_40757240/article/details/106313373

现在我将设计展示给大家:

C++实现金山打字通助手

实现代码如下:

Key.h

#ifndef KEY_H
#define KEY_H

#include <windows.h> 
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

#define A VK_A
#define a VK_A
#define B VK_B
#define b VK_B
#define C VK_C
#define c VK_C
#define D VK_D
#define d VK_D
#define E 69
#define e 69
#define F VK_F
#define f VK_F
#define G VK_G
#define g VK_G
#define H 72
#define h 72
#define I VK_I
#define i VK_I
#define J VK_J
#define j VK_J
#define K VK_K
#define k VK_K
#define L VK_L
#define l VK_L
#define M VK_M
#define m VK_M
#define N VK_N
#define n VK_N
#define O VK_O
#define o VK_O
#define P VK_P
#define p VK_P
#define Q VK_Q
#define q VK_Q
#define R VK_R
#define r VK_R
#define S VK_S
#define s VK_S
#define T VK_T
#define t VK_T
#define U VK_U
#define u VK_U
#define V VK_V
#define v VK_V
#define W VK_W
#define w VK_W
#define X VK_X
#define x VK_X
#define Y VK_Y
#define y VK_Y
#define Z VK_Z
#define z VK_Z
#define _0 VK_NUMPAD0
#define _1 VK_NUMPAD1
#define _2 VK_NUMPAD2
#define _3 VK_NUMPAD3
#define _4 VK_NUMPAD4
#define _5 VK_NUMPAD5
#define _6 VK_NUMPAD6
#define _7 VK_NUMPAD7
#define _8 VK_NUMPAD8
#define _9 VK_NUMPAD9
#define _ VK_SPACE //空格 


//#define ; 186
//#define Caps_Lock 20
class Key
{
	public:
		Key();
		~Key();
		Key& down(int vk_code);
		Key& up(int vk_code);
		Key& press(int vk_code);
		Key& combination(int vk_code);
		Key& combination(int vk_code_1,int vk_code_2);
		Key& combination(int vk_code_1,int vk_code_2,int vk_code_3);
		Key& combination(int vk_code_1,int vk_code_2,int vk_code_3,int vk_code_4);
		Key& sleep(int _time);
		Key& caps(); 
		Key& Caps();
		Key& setSleepTime(int _time);
		Key& bearStr(string str);
		Key& period();
		Key& comma();
		int  getTime();
		 
	private:
		
		int time;
		
};

#endif

 Key.cpp

#include "Key.h"
#include <string>
Key::Key()
{
	this->time=0; //默认没有延迟 
	cout<<this->time;
}

Key::~Key()
{
	
}
void state()
{
	/*
	// 判断键盘CapsLock键是否开启状态,开启状态则为大写,否则为小写
     if (GetKeyState(VK_CAPITAL))
     {
         // 如果当前键盘状态为大写,要求改小写,则模拟按键CapsLock切换状态
         if (!big)
         {
             keybd_event(VK_CAPITAL, NULL, KEYEVENTF_EXTENDEDKEY | 0, NULL);
             keybd_event(VK_CAPITAL, NULL, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, NULL);
        }
     }
     else
     {
         // 如果当前键盘状态为小写,要求改大写,则模拟按键CapsLock切换状态He 
         if (big)
         {
             keybd_event(VK_CAPITAL, NULL, KEYEVENTF_EXTENDEDKEY | 0, NULL);
             keybd_event(VK_CAPITAL, NULL, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, NULL);
         }
     }*/
     //if(!GetKeyStaye(VK_CAPITAL))
    // {
     	//press(VK_CAPITAL); 
     	
	// }
	 //如果是小写改成大写 
}

Key& Key::down(int vk_code)
{
	keybd_event(vk_code,0,0,0);
	return *this;

}
Key& Key::up(int vk_code)
{
	keybd_event(vk_code,0,KEYEVENTF_KEYUP,0);
	return *this;

}
Key& Key::press(int vk_code)
{
	if(this->time)
	{
		this->sleep(this->time);
	}
	
	if(islower(vk_code))//小写字母 
	{
		vk_code-=32; 
	}
	down(vk_code);
	up(vk_code);
	return *this; 
}
Key& Key::combination(int vk_code)
{
	press(vk_code);
	return *this;
}
Key& Key::combination(int vk_code_1,int vk_code_2)
{
	down(vk_code_1);
	press(vk_code_2);
	up(vk_code_1);
	return *this;
}
Key& Key::combination(int vk_code_1,int vk_code_2,int vk_code_3)
{
	down(vk_code_1);
	down(vk_code_2);
	press(vk_code_3);
	up(vk_code_2);
	up(vk_code_1);
	return *this;
}
Key& Key::combination(int vk_code_1,int vk_code_2,int vk_code_3,int vk_code_4)
{
	down(vk_code_1);
	down(vk_code_2);
	down(vk_code_3);
	press(vk_code_4);
	up(vk_code_3);
	up(vk_code_2);
	up(vk_code_1);
	return *this;
}
Key& Key::sleep(int _time)
{
	Sleep(_time);
	return *this;
}
Key& Key::caps()
{
	if (GetKeyState(VK_CAPITAL))
    {
         // 如果当前键盘状态为大写,要求改小写,则模拟按键CapsLock切换状态

             keybd_event(VK_CAPITAL, 0, 0, 0);
             keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
    }
    return *this;
}
Key& Key::Caps()
{
	if (!GetKeyState(VK_CAPITAL))
    {
         // 如果当前键盘状态为小写,要求改大写,则模拟按键CapsLock切换状态

             keybd_event(VK_CAPITAL, 0, 0, 0);
             keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
    }
    return *this;
}
Key& Key::setSleepTime(int _time)
{
	this->time=_time;
	return *this;
}
Key& Key::period()
{
	keybd_event(VK_DECIMAL, 0, 0, 0);
    keybd_event(VK_DECIMAL, 0, KEYEVENTF_KEYUP, 0);
	return *this;
}
Key& Key::comma()
{
	keybd_event(188, 0, 0, 0);
    keybd_event(188, 0, KEYEVENTF_KEYUP, 0);
	return *this;
}
int Key::getTime()
{
	return this->time;
}
Key& Key::bearStr(string str)
{
    for(int cout=0;cout<str.length();cout++)
    {
    	if(isupper(str[cout]))//如果是大写字母
		{
			this->Caps();
			press(str[cout]);
			this->caps(); 
		}
		else if(str[cout]=='.')
		{
			period();
		}
		else if(str[cout]==',')
		{
			comma();
		}
		else
		{
			press(str[cout]);
		}
	}
	return *this;
}

main.cpp

#include "Key.h"
#include <string>
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
 

void fileTest()
{
	string str;
	ifstream txt("test.txt");
	getline(txt,str); 
	//cout<<str;
	Key *nkey= new Key;
	nkey->sleep(3000);
	//nkey->setSleepTime(100);
	nkey->bearStr(str); 
}


int main(int argc,char *argv[])
{

	fileTest();
	
	return 0;
}

观看打字效果:

编译运行之后直接将光标放置在打字的软件上输入框就可以了。

C++实现金山打字通助手

备注:

这个软件我已经放置了在了gittee上面 https://gitee.com/shixiongyan/KingsoftTypingTools

测试文件的内容是:

test.txt

 He became interested in two theories that possibly explained how cholera killed people. The first suggested that cholera multiplied in the air. A cloud of dangerous gas floated around until it found its victims. The second suggested that people absorbed this disease into their bodies with their meals. From the stomach the disease quickly attacked the body and soon the affected person died. John Snow suspected that the second theory was correct but he needed evidence. So when another outbreak hit London in 1854, he was ready to begin his enquiry. As the disease spread quickly through poor neighbourhoods, he began to gather information. In two particular streets, the cholera outbreak was so severe that more than 500 people died in ten days. He was determined to find out why.