Note that there are some explanatory texts on larger screens.

plurals
  1. POSetCursorPos moving mouse twice?
    primarykey
    data
    text
    <p>I'm making a program that allows users to customize hotkeys and make them perform specific functions.</p> <p>The first function that I have made moves the cursor to a specific set of coordinates within the game window. It does this by finding the client area then adding the set coordinates and then moving the cursor there. However, it appears that each time SetCursorPos is called, it moves twice: first to the correct spot, the to (0,0). I'm not sure what's causing this, and I've spent hours googling and debugging to no avail. Everything works correctly except that it moves twice. I'm using Windows 8 and compiling with Code::Blocks.</p> <p>The script outputs what coordinates it should be moving too before the mouse moves and after. They should be the same. It also outputs "key down" when the hotkey is pressed down and "key up" when it is released.</p> <p>Pressing F1 will cause the mouse to move.</p> <pre><code>#include &lt;iostream&gt; #include &lt;windows.h&gt; 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)&amp;pos); ClientToScreen(hWnd,(LPPOINT)&amp;pos.left); ClientToScreen(hWnd,(LPPOINT)&amp;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 &lt;&lt; "Pre-move: " &lt;&lt; rx+x &lt;&lt; "," &lt;&lt; ry+y &lt;&lt; endl; SetCursorPos(rx + x,ry + y); cout &lt;&lt; "Post-move: " &lt;&lt; rx+x &lt;&lt; "," &lt;&lt; ry+y &lt;&lt; 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 &lt;&lt; "key down" &lt;&lt; endl; Sleep(100); } else{ key_state = false; cout &lt;&lt; "key up" &lt;&lt; endl; Sleep(100); } prev = curr; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload