1

So for one of my projects I need to be able to hold a key down on my keyboard and for aslong as i hold that down it slowly moves my cursor down. I have no clue on how to perform this action. I know that SetCursorPos instantly moves the cursor but im looking for a smooth drag. Thanks

xhienne
  • 5,738
  • 1
  • 15
  • 34

1 Answers1

1

You do not tell us what kind of library you are using. But here is an idea: If you know that the SetCursorPos instantly moves your cursor somewhere else. You could try incrementing the position by calling SetCursorPos(current_cursor_pos + 1) everytime a button is pressed, whether or not the button is pressed, you will check in a while loop. You would have to find out how to get the current cursor position and only increment the Y.

Why increment? Because most graphical libraries place the x and y zero position on the top left of your screen. So going down along the Y axis means incrementing the Y position of your cursor.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Oscar K
  • 185
  • 1
  • 11
  • Thanks for the contribution but im not too sure if this would work for what im trying to do. Ill give it a try tho. – jwjwjwjwjwjwyes Feb 10 '21 at 20:37
  • Be so kind to upvote the answer on the top left. Gives me credits and makes it easier to help more people :) – Oscar K Feb 10 '21 at 20:39
  • Nah this wont work. What im looking for is something that smoothly drags my cursor but u can change how quickly you can do it. – jwjwjwjwjwjwyes Feb 10 '21 at 20:41
  • 1
    I dont have enough rep yet sorry : / – jwjwjwjwjwjwyes Feb 10 '21 at 20:41
  • By the way, if you're not sure if this would work on what you're trying to do. Be so kind to explain the problem a bit more, what libraries/engine/whatever you're using etc. Maybe we can help you more/better – Oscar K Feb 10 '21 at 20:42
  • Gave your question an upvote ;) earned the first points now – Oscar K Feb 10 '21 at 20:43
  • So the libraries that i have on my code atm are iostream and Windows.h I can add more if needed. Im trying to make something known as a mouse macro. Just need something that drags down while a key is held down and need to make it so i can change the speed it drags at. Ive got the keydown stuff sorted tho – jwjwjwjwjwjwyes Feb 10 '21 at 20:44
  • I'm not really sure, as it's late in the evening here. But a quick Google told me that if you #include winuser.h. You can call the function `getcursorpos()` if you give a `POINT` type variable in the parameter. The x and y are than placed in the variable which you can read out. See this question: https://stackoverflow.com/questions/6423729/get-current-cursor-position/6423739 and see this from microsoft: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos – Oscar K Feb 10 '21 at 20:48
  • As for the "drag" try using a variable by which you increase the current mouse position. That variable is 0 at first. So when the button is pressed. You will actuall do: `set_mouse_pos(current_mouse_pos+0). But as long as the button is pressed, you also add 0.1 (for instance) every 10 millisecond to that variable. So at first it moves with +0.1, than 0.2, 0.3 etc. The speed of the drag is then determined by the amount of milliseconds you wait for each increase, and the number by which you increase the initial variable. – Oscar K Feb 10 '21 at 20:53