我正在制作一个程序,允许用户自定义热键并让它们执行特定的功能。
我所做的第一个函数将光标移动到游戏窗口中的一组特定坐标。它通过找到客户区域然后添加设置的坐标然后将光标移动到那里来做到这一点。但是,似乎每次调用 SetCursorPos 时,它都会移动两次:首先到正确的位置,然后到 (0,0)。我不确定是什么原因造成的,我花了几个小时谷歌搜索和调试都无济于事。一切正常,只是它移动了两次。我正在使用 Windows 8 并使用 Code::Blocks 进行编译。
该脚本在鼠标移动之前和之后输出它应该移动的坐标。他们应该是一样的。当热键被按下时它也会输出“key down”,当它被释放时它也会输出“key up”。
按 F1 将导致鼠标移动。
#include <iostream>
#include <windows.h>
using namespace std;
class macro{
public:
int x, y, w, h;
HWND hWnd;
HWND get_hWnd(){
hWnd = FindWindow(NULL,"Untitled - Notepad");
return hWnd;
}
void get_Pos(){
RECT pos;
GetClientRect(hWnd,(LPRECT)&pos);
ClientToScreen(hWnd,(LPPOINT)&pos.left);
ClientToScreen(hWnd,(LPPOINT)&pos.right);
w = pos.right;
h = pos.bottom;
x = pos.left;
y = pos.top;
}
void activate(){
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
}
void initialize(){
get_hWnd();
get_Pos();
}
void mousemove(int rx, int ry){
get_Pos();
cout << "Pre-move: " << rx+x << "," << ry+y << endl;
SetCursorPos(rx + x,ry + y);
cout << "Post-move: " << rx+x << "," << ry+y << endl;
}
}macro;
int main(){
macro.initialize();
bool loop = true;
bool action_complete = true;
int prev = 0;
int curr = 0;
bool key_state; // True if down, false if up.
while(loop){
if(GetAsyncKeyState(0x70)){
curr = 1;
}
else{
curr = 0;
}
if (prev != curr){
if(curr){
key_state = true;
macro.mousemove(100,100);
cout << "key down" << endl;
Sleep(100);
}
else{
key_state = false;
cout << "key up" << endl;
Sleep(100);
}
prev = curr;
}
}
}