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++] Getting keys and sending keys

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Fri Jan 11, 2008 1:30 pm    Post subject: [C++] Getting keys and sending keys Reply with quote

I'm was just wondering how to get and send keystrokes. I was looking at this and I didn't understand the examples.

Code:
SHORT GetAsyncKeyState(     
    int vKey
);


Can someone please explain to me how to get and send keystrokes in C++?
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Fri Jan 11, 2008 2:33 pm    Post subject: Reply with quote

here are 2 examples i made as a SendInput example (Mouse Clicking).

You can change the input to INPUT_KEYBOARD and then change the info on it

DLL Example:

Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

BOOL bExit = FALSE;

DWORD WINAPI Click()
{
   while ( !bExit )
   {
      if ( GetAsyncKeyState( VK_F11 ) )
      {
         for ( ; ; )
         {
            if ( GetAsyncKeyState( VK_F12 ) )
            {
               break;
            }

            INPUT aInput;

            memset(&aInput, 0, sizeof(INPUT));

            aInput.type = INPUT_MOUSE;
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

            SendInput(1, &aInput, sizeof(INPUT));

            aInput.type = INPUT_MOUSE;
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTUP;

            SendInput(1, &aInput, sizeof(INPUT));
            Sleep ( 1000 ); //Change this for click speed.
         }
      }
      Sleep( 100 );
   }
   return 0;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwMsg, LPVOID lpReserved)
{
   switch( dwMsg )
   {
   case DLL_PROCESS_ATTACH:
      DisableThreadLibraryCalls( hModule );
      CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)Click, NULL, 0, 0 );
      return TRUE;
   case DLL_PROCESS_DETACH:
      bExit = TRUE;
      return TRUE;
   }
   return TRUE;
}


Console App Example:

Code:
#include <windows.h>
#include <stdio.h>

bool bAC = false;
unsigned long DelayVal;

void ChangeSpeed()
{
   printf("Please Enter a Clicking Speed. (In Milliseconds)\n");
   scanf("%d", &DelayVal);
   printf("Click Speed Set to %d Second(s)", DelayVal / 1000);
   printf("\n\nPress F11 to start clicking, F12 to stop, and C to change the Click Speed\n\n");
   bAC = true;
}

int main(int argc, char *argv[])
{
   ChangeSpeed();
   while ( bAC )
   {
      if ( GetAsyncKeyState( VK_F11 ) )
      {
         printf("Clicking Starting in 2 Seconds\n\n");
         Sleep( 2000 );

         for ( ; ; )
         {
            if ( GetAsyncKeyState( VK_F12 ) )
            {
               printf("Clicking Stopped\n\n");
               break;
            }

            INPUT aInput;

            memset(&aInput, 0, sizeof(INPUT));

            aInput.type = INPUT_MOUSE;
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

            SendInput(1, &aInput, sizeof(INPUT));

            aInput.type = INPUT_MOUSE; 
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTUP;

            SendInput(1, &aInput, sizeof(INPUT));
            Sleep( DelayVal );
         }
      }
      if ( GetAsyncKeyState( 0x43 /* Key: C */ ) )
      {
         bAC = false;
         ChangeSpeed();
      }
       
      Sleep( 100 );
   }
   return 0;
}

_________________


Last edited by lurc on Fri Jan 11, 2008 9:51 pm; edited 1 time in total
Back to top
View user's profile Send private message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Fri Jan 11, 2008 3:16 pm    Post subject: Reply with quote

Thank you, I understood a lot of the get key part, but this is the only part I have trouble understanding.

Code:
...
INPUT aInput;

            memset(&aInput, 0, sizeof(INPUT));

            aInput.type = INPUT_MOUSE;
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

            SendInput(1, &aInput, sizeof(INPUT));

            aInput.type = INPUT_MOUSE; 
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTUP;

            SendInput(1, &aInput, sizeof(INPUT));
            Sleep( DelayVal );
...


The only part I understood about this part is...

Code:
            Sleep( DelayVal );


Embarassed
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Fri Jan 11, 2008 3:25 pm    Post subject: Reply with quote

Memset (Stated Here):

Code:
void * memset ( void * ptr, int value, size_t num );

Fill block of memory

Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

ptr -Pointer to the block of memory to fill.
value - Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num - Number of bytes to be set to the value.


Here, i explained the code using comments:

Code:
INPUT aInput; // state aInput as a new INPUT struct.

            memset(&aInput, 0, sizeof(INPUT)); // set 0's for undeclared / unfilled structs in aInput

            aInput.type = INPUT_MOUSE; // Input type is mouse
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; // Input -> Mouse Input -> dwFlags (Event Flags) = Left Mouse Click Down

            SendInput(1, &aInput, sizeof(INPUT)); // Send That input (Left click Down)

            aInput.type = INPUT_MOUSE; // Input type is mouse
            aInput.mi.dwFlags = MOUSEEVENTF_LEFTUP; // Input -> Mouse Input -> dwFlags (Event Flags) = Left Mouse Click Up

            SendInput(1, &aInput, sizeof(INPUT)); // Send That Input (Left Click Up)
            Sleep( DelayVal );

_________________
Back to top
View user's profile Send private message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Fri Jan 11, 2008 3:41 pm    Post subject: Reply with quote

So if I were to change INPUT_MOUSE to INPUTLEYBOARD, would the code look like this?

Code:
INPUT bInput;

memset(&bInput, 0, sizeof(INPUT));

bInput.type = INPUT_KEYBOARD;
bInput.mi.dwFlags = KEYEVENTF_LCONTROLUP;
SendInput(1, &bInput, sizeof(INPUT));

...
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Fri Jan 11, 2008 9:43 pm    Post subject: Reply with quote

not exactly, but your close. with sending keys it looks like this

Code:
INPUT aInput; // state a new INPUT struct: aInput
   
   memset(&aInput,0,sizeof(INPUT)); // set 0's for unused / undeclared structs.

   aInput.type = INPUT_KEYBOARD; // input type is keyboard
   aInput.ki.wVk = VK_CONTROL; // aInput -> Keyboard Input -> virtual key that your sending (VK_CONTROL is the control key obviously)
   aInput.ki.dwFlags = KEYEVENTF_KEYDOWN; // Event Flag press down key
       
   SendInput(1,&aInput,sizeof(INPUT)); // send it
   
   aInput.type = INPUT_KEYBOARD;
   aInput.ki.wVk = VK_CONTROL;
   aInput.ki.dwFlags = KEYEVENTF_KEYUP; // Event flag, release key.

   SendInput(1,&aInput,sizeof(INPUT));


sorry i forgot to mention that when change the input type your no longer using the struct "mi" (mouse input) your using "ki" (keyboard input) which will give you a choice to define the key you want to send, as well as the event message.

to change the keys you send here is a list of virtual keys: MSDN Virtual-Key Codes

_________________
Back to top
View user's profile Send private message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Sat Jan 19, 2008 2:06 pm    Post subject: Reply with quote

There was a little error but I found out how to fix it.

Code:
#define VK_0     0x30

   INPUT aInput; // state a new INPUT struct: aInput
   
   memset(&aInput,0,sizeof(INPUT));

   aInput.type = INPUT_KEYBOARD;
   aInput.ki.wVk = VK_0;
   aInput.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
   SendInput(1,&aInput,sizeof(INPUT));
   
   aInput.type = INPUT_KEYBOARD;
   aInput.ki.wVk = VK_CONTROL;
   aInput.ki.dwFlags = KEYEVENTF_KEYUP;
   SendInput(1,&aInput,sizeof(INPUT));


The KEYEVENTF_KEYDOWN wasn't working for me. I tried the code above to try to press the 0 key, but it didn't work. Any hints you can give me?

_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
NINTENDO
Grandmaster Cheater Supreme
Reputation: 0

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Sat Jan 19, 2008 3:05 pm    Post subject: Reply with quote

Wow, lurc!
That's some nice codeing Wink
Ur really experienced.

_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
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