C++ emulates an instance of a keyboard key

  • 2020-06-07 04:55:50
  • OfStack

This is similar to the mouse click function, directly on the function


keybd_event(VK_RETURN,0,0,0);
keybd_event(VK_RETURN,0,KEYEVENTF_KEYUP,0);

This is simulated pressing and lifting the enter key

I'm going to go straight to the first refresh program that I used to write with some functions

I've tried it on my own small size, and if they're using a cell phone, the results are phenomenal


#include<iostream>
#include<windows.h>
 
using namespace std;
 
int b[11000],top=0;
char a[10];
bool f=true; 
 
int main(){
	int n,num;
	// Initialize the  
	a[0]='0';
	a[1]='1';
	a[2]='2';
	a[3]='3';
	a[4]='4';
	a[5]='5';
	a[6]='6';
	a[7]='7';
	a[8]='8';
	a[9]='9';
	
	while(1){
		cout<<" Please enter refresh times: ";
		cin>>n;
		cout<<" Please enter the interval ( unit : ms  1000 ms =1 seconds ) : ";
		cin>>num;
		if_return:
		cout<<" Do I need a lead return?  1.yes 2.no"<<endl;
		int k;
		cin>>k;
		if(k==1){
			f=true;
		}
		else if(k==2){
			f=false;
		}
		else{
			cout<<" Typo! "<<endl;
			goto if_return;// A messy but convenient loop  
		}
		cout<<" Move the cursor to the input field "<<endl;
		Sleep(2000);
		mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
		mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);// Simulate clicking the left mouse button  
		cout<<" Please wait for 3 Seconds... "<<endl;
		Sleep(1000);
		cout<<"3"<<endl;
		Sleep(1000);
		cout<<"2"<<endl;
		Sleep(1000);
		cout<<"1"<<endl;
		for(int i=1; i<=n; i++){
			if(f==true){
				keybd_event(VK_RETURN,0,0,0);
				keybd_event(VK_RETURN,0,KEYEVENTF_KEYUP,0);
			} 
			int x=i;
			while(x>0){
				b[top++]=x%10;
				x/=10;
			}
			top--;
			for(int j=top; j>=0; j--){
				keybd_event(a[b[j]],0,0,0);
				keybd_event(a[b[j]],0,KEYEVENTF_KEYUP,0);// A numeric key is pressed in simulation  
			}
			top=0;
			keybd_event(VK_RETURN,0,0,0);
			keybd_event(VK_RETURN,0,KEYEVENTF_KEYUP,0);// Press enter in simulation  
			Sleep(num);
		}
		putchar(7);
		system("cls");// Clear the screen  
	}
	
	
	return 0;
}

Related articles: