前言(不支持中文输入)
这学期英语课用了优课联盟,第一次发现竟然还有不让粘贴的作文???但是身为一个半吊子程序员,能偷懒绝不自己动手,这种事情当然直接让计算机来就好了(ノ◕ω◕)ノ
前提
最主要的两个模块就是文件读取和模拟键盘,这样就可以将一个文件打出来了。
新建一个txt文件,保存要粘贴的内容,运行程序,选择文件,准备好了打个ok,读秒开始后三秒内光标点在要打字的地方,so easy!
1.文件读取
首先是弹出文件选择对话框,使用BROWSEINFO
这个解释多一些
然后就是读取文件,使用ifstream,这个就不放链接了,一搜很多解释的
2.模拟键盘
其实就两行
keybd_event("键码",0,0,0);//按下
keybd_event("键码",0,KEYEVENTF_KEYUP,0);//松开
就是和人按键盘一样
通过修改键码,我们就可以让计算机按不同的键
如果是组合键,就像下面这样:
keybd_event("键码1",0,0,0);//按下
keybd_event("键码2",0,0,0);//按下
keybd_event("键码2",0,KEYEVENTF_KEYUP,0);//松开
keybd_event("键码1",0,KEYEVENTF_KEYUP,0);//松开
上完整代码
#include <bits/stdc++.h>
#include <Windows.h>
#include<Shlobj.h>
#include<CommDlg.h>
#include <string>
#include <fstream>
using namespace std;
#define ll long long
int l=0;//读取文件的s长度
string s[100000];//存储文件,可以更大
void OpebFile()
{
TCHAR szBuffer[MAX_PATH] = {0};
BROWSEINFO bi;
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = NULL;
bi.pszDisplayName = szBuffer;
bi.lpszTitle = ("从下面选择文件或文件夹:");
bi.ulFlags = BIF_BROWSEINCLUDEFILES;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if (NULL == idl)
{
return;
}
SHGetPathFromIDList(idl,szBuffer);//转换文件路径
cout <<"获得文件路径"<<endl;
ifstream myfile(szBuffer);//这个就是文件名
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
}
while(getline(myfile,s[l]))//逐行读取
{
l++;
}
myfile.close();
cout <<"已读取文件"<<endl;
}
void Big(char s)//大写字母
{
if(!GetKeyState(VK_CAPITAL))//这个可以自行实验一下,看大写锁定不同情况的输出
keybd_event(VK_CAPITAL,0,0,0);
keybd_event(VK_CAPITAL,0,KEYEVENTF_KEYUP,0);
int n=s-'A'+65;
keybd_event(n,0,0,0);
keybd_event(n,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_CAPITAL,0,0,0);
keybd_event(VK_CAPITAL,0,KEYEVENTF_KEYUP,0);
}
void Small(char s)//小写字母
{
if(GetKeyState(VK_CAPITAL))
keybd_event(VK_CAPITAL,0,0,0);
keybd_event(VK_CAPITAL,0,KEYEVENTF_KEYUP,0);
int n=s-'a'+65;
keybd_event(n,0,0,0);
keybd_event(n,0,KEYEVENTF_KEYUP,0);
}
void Number(char s)//数字,注意小键盘和主键盘数字代码不一样
{
int n=s-'0'+48;
keybd_event(n,0,0,0);
keybd_event(n,0,KEYEVENTF_KEYUP,0);
}
void Symbols(char s)//符号
{
if(s==' ')
{
keybd_event(32,0,0,0);
keybd_event(32,0,KEYEVENTF_KEYUP,0);
}
if(s==';')
{
keybd_event(186,0,0,0);
keybd_event(186,0,KEYEVENTF_KEYUP,0);
}
else if(s==':')
{
keybd_event(16,0,0,0);
keybd_event(186,0,0,0);
keybd_event(186,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s==',')
{
keybd_event(188,0,0,0);
keybd_event(188,0,KEYEVENTF_KEYUP,0);
}
else if(s=='<')
{
keybd_event(16,0,0,0);
keybd_event(188,0,0,0);
keybd_event(188,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='>')
{
keybd_event(16,0,0,0);
keybd_event(190,0,0,0);
keybd_event(190,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='.')
{
keybd_event(190,0,0,0);
keybd_event(190,0,KEYEVENTF_KEYUP,0);
}
else if(s=='"')
{
keybd_event(16,0,0,0);
keybd_event(222,0,0,0);
keybd_event(222,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='\'')
{
keybd_event(222,0,0,0);
keybd_event(222,0,KEYEVENTF_KEYUP,0);
}
else if(s=='(')
{
keybd_event(16,0,0,0);
keybd_event(57,0,0,0);
keybd_event(57,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s==')')
{
keybd_event(16,0,0,0);
keybd_event(48,0,0,0);
keybd_event(48,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='[')
{
keybd_event(219,0,0,0);
keybd_event(219,0,KEYEVENTF_KEYUP,0);
}
else if(s==']')
{
keybd_event(221,0,0,0);
keybd_event(221,0,KEYEVENTF_KEYUP,0);
}
else if(s=='{')
{
keybd_event(16,0,0,0);
keybd_event(219,0,0,0);
keybd_event(219,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='}')
{
keybd_event(16,0,0,0);
keybd_event(221,0,0,0);
keybd_event(221,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='!')
{
keybd_event(16,0,0,0);
keybd_event(49,0,0,0);
keybd_event(49,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='$')
{
keybd_event(16,0,0,0);
keybd_event(52,0,0,0);
keybd_event(52,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='?')
{
keybd_event(16,0,0,0);
keybd_event(191,0,0,0);
keybd_event(191,0,KEYEVENTF_KEYUP,0);
keybd_event(16,0,KEYEVENTF_KEYUP,0);
}
else if(s=='-')
{
keybd_event(189,0,0,0);
keybd_event(189,0,KEYEVENTF_KEYUP,0);
}
}
void Print()
{
cout << "is ok?"<<endl;
string s2="";
while(s2!="ok")
cin>>s2;
cout<<"光标指向要输入位置"<<endl;
cout <<"3秒后开始"<<endl;
for(int i=3;i>=1;i--)//留一点准备时间,可以自行加长
{
cout <<i<<endl;
Sleep(1000);
}
for(int i=0;i<l;i++)
{
ll l2=s[i].length();
for(int j=0;j<l2;j++)
{
if(('a'<=s[i][j])&&(s[i][j]<='z')) Small(s[i][j]);
else if('A'<=s[i][j]&&s[i][j]<='Z') Big(s[i][j]);
else if('0'<=s[i][j]&&s[i][j]<='9') Number(s[i][j]);
else Symbols(s[i][j]);
}
keybd_event(13,0,0,0);//换行
keybd_event(13,0,KEYEVENTF_KEYUP,0);
}
}
int main()
{
OpebFile();
Print();
return 0;
}
符号不够的话自行搜索键码(keyCode)然后再添加
别问我为什么只有英文,毕竟我只是写英语作文
好吧我承认中文好像很麻烦的样子,应该要加上文字识别,我太菜了不会