一些好玩的东西
输入字符但不在显示器显示:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
char c;
int main(){
while(1){
char c=getch();
//do domething
}
}获取鼠标位置:
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main(){
POINT p;
while(1){
if(GetCursorPos(&p)) cout<<p.x<<" "<<p.y<<endl;
Sleep(1000);
}
}移动鼠标到指定位置:
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main(){
while(1){
int x=rand()%1600,y=rand()%900;
SetCursorPos(x,y);
Sleep(1000);
}
}限制鼠标在指定矩形移动:
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main(){
POINT p;
while(1){
int xx,yy;
bool ck=0;
if(GetCursorPos(&p)){
if(p.x<500){
xx=500;
ck=1;
}
else if(p.x>1000){
xx=1000;
ck=1;
}
else xx=p.x;
if(p.y<300){
yy=300;
ck=1;
}
else if(p.y>600){
yy=600;
ck=1;
}
else yy=p.y;
if(ck) SetCursorPos(xx,yy);
}
}
}限定鼠标在指定圆形内移动
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main(){
POINT p;
int r=300;//r:1~400
while(1){
if(GetCursorPos(&p)){
if(((p.x-700)*(p.x-700)+(p.y-400)*(p.y-400))>r*r){
if(p.x==700){
if(p.y<400) SetCursorPos(700,400-r);
else SetCursorPos(700,400+r);
}
else if(p.y==400){
if(p.x<700) SetCursorPos(700-r,400);
else SetCursorPos(700+r,400);
}
else{
if(p.x<700&&p.y<400) SetCursorPos(p.x+1,p.y+1);
else if(p.x>700&&p.y<400) SetCursorPos(p.x-1,p.y+1);
else if(p.x<700&&p.y>400) SetCursorPos(p.x+1,p.y-1);
else SetCursorPos(p.x-1,p.y-1);
}
}
}
}
}判断键盘是否被按下键
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int main(){
while(1){
if(_kbhit()){
int c=getch();
cout<<c<<endl;
}
}
system("pause");
}点击鼠标
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main(){
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
}按下键盘按键
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
#define vk(c) (c)-'a'+65
#define Space 32
#define Enter 13
void down(char c){
keybd_event(vk(c),0,0,0);
}
void up(char c){
keybd_event(vk(c),0,KEYEVENTF_KEYUP,0);
}
void press(char c){
down(c);
up(c);
}
int main(){
srand((unsigned)time(NULL));
while(1){
int tmp=rand()%26;
char c='a'+tmp;
press(c);
Sleep(100);
}
}@华法琳血怒 提供的一些意见(主要是里面的类):
#include<bits/stdc++.h>
#include <windows.h>
void apply();
int start_applying(bool looping=true,int interval=50){
do apply(),Sleep(interval);while(looping);
return 0;
}
int main(){
return start_applying();
}
class simulator{
public:
static bool is_pressing(char c){
return GetAsyncKeyState(c);
}
static LONG get_current_x(){
POINT p;
GetCursorPos(&p);
return p.x;
}
static LONG get_current_y(){
POINT p;
GetCursorPos(&p);
return p.y;
}
static void leftdown(){
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
}
static void leftup(){
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
}
static void left_click(int repeat_times=1,int interval=0){
while(repeat_times--){
leftdown();
Sleep(interval);
leftup();
}
}
static void rightdown(){
mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
}
static void rightup(){
mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
}
static void right_click(int repeat_times=1, int interval=0){
while(repeat_times--){
rightdown();
Sleep(interval);
rightup();
}
}
static void keydown(char c){
keybd_event(c,0,0,0);
}
static void keyup(char c){
keybd_event(c,0,KEYEVENTF_KEYUP,0);
}
static void press_key(char c,int repeat_times=1,int interval=0){
while(repeat_times--){
keydown(c);
Sleep(interval);
keyup(c);
}
}
static void set_position(int x,int y){
SetCursorPos(x,y);
}
static void move_mouse(int fromx,int fromy,int tox,int toy,int duration,int delta=10){
int dx=fromx-tox;
dx=-dx;
int dy=fromy-toy;
dy=-dy;
duration/=delta;
int x_movement=dx/duration;
int y_movement=dy/duration;
for(int i=1;i<=duration;i++){
set_position(fromx+x_movement*i,fromy+y_movement*i);
Sleep(delta);
}
set_position(tox,toy);
}
static void drag_mouse(int fromx,int fromy,int tox,int toy,int duration,int delta=10){
leftdown();
move_mouse(fromx,fromy,tox,toy,duration,delta);
leftup();
}
static void move_mouse(int x_movement,int y_movement,int duration,int delta=10){
move_mouse(get_current_x()+x_movement,get_current_y()+y_movement,duration,delta);
}
static void drag_mouse(int x_movement,int y_movement,int duration,int delta = 10){
drag_mouse(get_current_x()+x_movement,get_current_y()+y_movement,duration,delta);
}
};
void apply(){
if(simulator::is_pressing('C')) simulator::left_click(3,50);
}