Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]Moving mouse with arrow keys.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
407
Master Cheater
Reputation: 0

Joined: 25 Oct 2007
Posts: 357

PostPosted: Mon Oct 12, 2009 12:53 am    Post subject: [C++]Moving mouse with arrow keys. Reply with quote

Hello everyone, I'm back for further assistance..

Most of you probably don't remember me, but I'm some-what new to C++ and etc etc.... anyways...

I'm wondering how to go about making a program that would move the mouse with arrow keys. (like what hotkeys or keywords or w/e you call them would I need to note?)

I still don't know all of the C++ keywords, but I'm sure I could figure out the mathematics of it.... I hope.. Confused


Thank you,


Diamonds
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Mon Oct 12, 2009 12:59 am    Post subject: Reply with quote

GetCursorPos()
SetCursorPos()

basically

GetCursorPos() // now you have ur x and y coords
if(up arrow key is pressed) // i forgot the api to see if a key is pressed
{ SetCursorPos ( x , y-1); // might need y - 10 or something for more obvious movement }



thats just rough psuedo-code.

_________________
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Mon Oct 12, 2009 6:51 am    Post subject: Reply with quote

If you do what manc said you have to do that in a loop.
Code:
while(not_exiting){
   
   const int delta = 5;
   
   POINT cursorpos;
   GetCursorPos(&cursorpos);
   
   if( GetAsyncKeyState(VK_LEFT) & 0x8000 ){
      cursorpos.x -= delta;
   }else if( GetAsyncKeyState(VK_RIGHT) & 0x8000 ){
      cursorpos.x += delta;
   }
   
   if( GetAsyncKeyState(VK_UP) & 0x8000 ){
      cursorpos.y -= delta;
   }else if( GetAsyncKeyState(VK_DOWN) & 0x8000 ){
      cursorpos.y += delta;
   }
   
   SetCursorPos(cursorpos.x, cursorpos.y);
   
   Sleep(20);
   
}

You could also use RegisterHotkey to register the arrow keys as hotkeys, and then you don't need a loop, and instead you only have to do something when a WM_HOTKEY message arrives.
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Mon Oct 12, 2009 8:57 am    Post subject: Reply with quote

it seems like a bad idea to register the arrow keys as hotkeys. GAKS seems like a better idea
_________________
Back to top
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Mon Oct 12, 2009 10:09 am    Post subject: Reply with quote

Kinda offtopic, but does anyone know a similar function like GetAsyncKeySate, but for Linux?
I'm trying to do a little the same, but with Linux using X and Gnome.
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Mon Oct 12, 2009 12:31 pm    Post subject: Reply with quote

HomerSexual wrote:
it seems like a bad idea to register the arrow keys as hotkeys. GAKS seems like a better idea

Why is that? Is registering hotkeys bad? It just seems to me that a loop with GetAsyncKeyState will have a much higher cpu usage...
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Oct 12, 2009 4:58 pm    Post subject: Reply with quote

tombana wrote:
HomerSexual wrote:
it seems like a bad idea to register the arrow keys as hotkeys. GAKS seems like a better idea

Why is that? Is registering hotkeys bad? It just seems to me that a loop with GetAsyncKeyState will have a much higher cpu usage...


The cpu usage would probably be trivial as long as you didn't let the loop run rampant. I doubt you need to check it several million times a second anyway.
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Mon Oct 12, 2009 7:01 pm    Post subject: Reply with quote

tombana wrote:
HomerSexual wrote:
it seems like a bad idea to register the arrow keys as hotkeys. GAKS seems like a better idea

Why is that? Is registering hotkeys bad? It just seems to me that a loop with GetAsyncKeyState will have a much higher cpu usage...


I prefer registerhotkey, but the arrow keys are used so much that something else may already have it registered (like a game?) I always do like ctrl+f11

_________________
Back to top
View user's profile Send private message
samo502
Master Cheater
Reputation: 0

Joined: 14 Mar 2008
Posts: 342
Location: That place.

PostPosted: Thu Oct 22, 2009 12:14 am    Post subject: Reply with quote

Code:
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int showCmd)
{
   MessageBoxA(0, "Use the arrow keys to control the mouse\n [ slows speed\n ] makes it faster\n left Windows key + Z left clicks\n left Windows key + X right clicks\n Left Windows key + A toggles dragging\n And Escape exits.", "Mouse Control v0.5 By Samo502", MB_OK);
   POINT pt;
   int drag = false;
   int speed = 10;
   int not_exiting = true;
   do
   {
      GetCursorPos(&pt);
      if(GetAsyncKeyState(VK_LEFT)) //mouse movement left
      {
         SetCursorPos(pt.x -= speed, pt.y);
      }
      if(GetAsyncKeyState(VK_UP)) //mouse movement up
      {
         SetCursorPos(pt.x, pt.y -= speed);
      }
      if(GetAsyncKeyState(VK_RIGHT)) //mouse movement right
      {
         SetCursorPos(pt.x += speed, pt.y);
      }
      if(GetAsyncKeyState(VK_DOWN)) //mouse movement down
      {
         SetCursorPos(pt.x, pt.y += speed);
      }
      if(GetAsyncKeyState(VK_ESCAPE)) //exit key
      {
         not_exiting = false;
      }
      if(GetAsyncKeyState(VK_LWIN) & GetAsyncKeyState('Z')) //left click
      {
         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
      }
      if(GetAsyncKeyState(VK_LWIN) & GetAsyncKeyState('X')) //right click
      {
         mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
      }
      if(GetAsyncKeyState(VK_LWIN) & GetAsyncKeyState('A')) //drag
      {
         if(drag == true)
         {
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            drag = false;
         }
         else
         {
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            drag = true;
         }
      }
      if(GetAsyncKeyState(VK_OEM_4)) /speed up
      {
         speed--;
      }
      if(GetAsyncKeyState(VK_OEM_6)) //speed down
      {
         speed++;
      }
      Sleep(100); //makes sure the loop doesn't kill your comp.
   }while(not_exiting);
   return 0;
}
Simple one, though has a bad control scheme includes left clicking, right clicking, dragging, movement, changeable movement speed, and esc key to exit. If anyone can recommend a better control scheme in particular i'd like to know.
Back to top
View user's profile Send private message MSN Messenger
iTz SWAT
I post too much
Reputation: 1

Joined: 20 Dec 2007
Posts: 2227
Location: Me.Location;

PostPosted: Thu Oct 22, 2009 6:11 am    Post subject: Reply with quote

I use GetAsncKeyState () in a timer in VB .NET 2008 as a super easy Global hotkey. Is there a better way which uses less CPU Usage, and is more accurate etc?
_________________
Back to top
View user's profile Send private message
samo502
Master Cheater
Reputation: 0

Joined: 14 Mar 2008
Posts: 342
Location: That place.

PostPosted: Thu Oct 22, 2009 5:12 pm    Post subject: Reply with quote

iTz SWAT wrote:
I use GetAsncKeyState () in a timer in VB .NET 2008 as a super easy Global hotkey. Is there a better way which uses less CPU Usage, and is more accurate etc?
I'm going to assume you're doing this same project, if you look at my code above yours shouldn't be too much different really except i think the mouse coords would register as a location type, and not a POINT or LPPOINT type. Someone else is probably going to have to correct me on that since I've never used VB .NET for this sort of thing, or at all for a long time. GetAsyncKeyState() is a pretty accurate command to use, the inaccuracy is in your timer, being that the commands only initialize on tick. If you set the timer time to say.... 100, like my program above uses sleep 100, you should get about the same effect. Memory-wise, VB is almost always going to take up more memory than C++ in this type of project, also depending on code. I would also suggest making a speed variable to move the mouse by.
Back to top
View user's profile Send private message MSN Messenger
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Fri Oct 23, 2009 4:42 am    Post subject: Reply with quote

samo502 wrote:
Code:

      if(GetAsyncKeyState(VK_LWIN) & GetAsyncKeyState('Z')) //left click
      {
         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
      }
      if(GetAsyncKeyState(VK_LWIN) & GetAsyncKeyState('X')) //right click
      {
         mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
      }

I think you need && here instead of &.
&& is the logical AND operator while & is the bitwise AND operator.
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Fri Oct 23, 2009 4:47 am    Post subject: Reply with quote

tombana wrote:
samo502 wrote:
Code:

      if(GetAsyncKeyState(VK_LWIN) & GetAsyncKeyState('Z')) //left click
      {
         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
      }
      if(GetAsyncKeyState(VK_LWIN) & GetAsyncKeyState('X')) //right click
      {
         mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
      }

I think you need && here instead of &.
&& is the logical AND operator while & is the bitwise AND operator.


Think about what the bitwise AND operator does, and what kind of values those functions will return, then tell me again if it will work.
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Fri Oct 23, 2009 6:29 am    Post subject: Reply with quote

smartz993 wrote:
Think about what the bitwise AND operator does, and what kind of values those functions will return, then tell me again if it will work.

Yes it will work in this case, but since the 'important' bits of the return value of GetAsyncKeyState are 0x8000 and 0x0001, then what if one of them returned 0x8000 and the other returned 0x0001: then using & will result in false, and using && will result in true.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites