Skip to content

Instantly share code, notes, and snippets.

@qwe321qwe321qwe321
Created May 1, 2020 05:50
Show Gist options
  • Save qwe321qwe321qwe321/64bd0b672aefaab821891fd14b0a77ee to your computer and use it in GitHub Desktop.
Save qwe321qwe321qwe321/64bd0b672aefaab821891fd14b0a77ee to your computer and use it in GitHub Desktop.
Simple Cursor Locker is a simple program to lock the cursor with the specific axis(X/Y axis). It's helpful in some design programs which do not have this feature.
#include<cstdio>
#include<windows.h>
using namespace std;
bool IsKey(int key);
int main() {
const int enableKeyX = VK_F6;
const int enableKeyY = VK_F7;
printf("//////////////////////////////\n");
printf("==== Simple Cursor Locker ====\n");
printf("//////////////////////////////\n");
printf("Press F6 to lock mouse X, F7 to lock mouse Y\n");
printf("The key detection is global, it works on any program.");
POINT p;
bool lockX = false, lockY = false;
int lockPosX = 0, lockPosY = 0;
while(1) {
if (!GetCursorPos(&p)) {
printf("For some unknown reason, I cannot get the cursor position by winapi\n");
continue;
}
if (IsKey(enableKeyX)) {
lockX = !lockX;
if (lockX) {
printf("Lock mouse X in %d\n", p.x);
lockPosX = p.x;
} else {
printf("Unlock mouse X\n");
}
} else if (IsKey(enableKeyY)) {
lockY = !lockY;
if (lockY) {
printf("Lock mouse Y in %d\n", p.y);
lockPosY = p.y;
} else {
printf("Unlock mouse Y\n");
}
}
if (lockX || lockY) {
SetCursorPos(lockX? lockPosX : p.x, lockY ? lockPosY : p.y);
}
}
}
bool IsKey(int key) {
if(GetKeyState(key) < 0)
{
while(GetKeyState(key)<0);
return true;
}
else
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment