Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty

    Proper use of GetAsyncKeyState()

    I know GetAsyncKeyState() is a function used often in hacks, for navigating menus, detecting key presses in the case of nonmenu hacks and so on, but the fact is a lot of people do not fully understand how it works. So today I will be briefly explaining exactly what GetAsyncKeyState() does and how to use it properly.

    Definition: GetAsyncKeyState is a function that returns a SHORT value (on 32bit processors that is a 2 value, half the size of a normal int). The SHORT value will be non-zero (positive) if the key specified is pressed down at the instant the functions is called.

    Example:
    Code:
    GetAsyncKeyState(VK_INSERT);
    if the key is pressed down at the exact instant that this function is called the function will return a positive value. Due to automatic type conversion any positive value of any data type when put into an if() statement will return true.

    Use in Loops:
    Now the function seems simple enough, but this is where a lot of people fail. When used in loops you have to be careful about your timing. The CPU loops through you code 1000's of times per second! Now when a human presses a key on his/her keyboard the program can test that key hundreds of even thousands of times before the human can remove his finger from the button o_O.

    Example of bad use:
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
        int i = 0;
        
        while(1) //infinite loop!
    {
                 if(GetAsyncKeyState(VK_DOWN))
                 {
                  i--;
                  cout<<"Selected: " << i <<endl;
                 }
                 
                 if(GetAsyncKeyState(VK_UP))
                 {
                  i++;
                  cout<<"Selected: " << i <<endl;
                 }
        }
        return 0;
    }
    I have this code in a infinite loop, but the loop has no controls to slow down the speed of the loop so it could repeat thousands of times before your even humanly able to remove ur finger from the keyboard.

    Example of Good Use:
    The important thing that makes the difference between a good loop and a bad loop is timing.
    This function: Sleep(int timeInMilliseconds); makes a huge difference.
    if will slow the speed ur loop repeats to a reasonable human speed, so ur user is actually able to remove their finger before subsequent iterations.

    Now a second = 1000 milliseconds so a good amount of time for your program to sleep might be 100 milliseconds or Sleep(100);

    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
        int i = 0;
        
        while(1){ //infinite loop!
                 if(GetAsyncKeyState(VK_DOWN))
                 {
                  i--;
                  cout<<"Selected: " << i <<endl;
                 }
                 
                 if(GetAsyncKeyState(VK_UP))
                 {
                  i++;
                  cout<<"Selected: " << i <<endl;
                 }
                 Sleep(100); // loop will only start again after 1/10 of a second
        }
        return 0;
    }
    See and just like that problem solved. You can compile both of these codes and test to see the difference.

    Using GetAsyncKeyState to set values
    Usually this the value being set are boolean values. quite a few people think. "Oh well because its just on or off I do not neeed to time the loop. Well this is wrong. Boolean expressions like this are especially troublesome in trainers made by novice or even experience gamehackers:

    Example of bad technique:
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    void runHack()
        {
                 cout << "Running hack." <<endl;
                 return;
        }
        
    int main()
    {
        int i = 0;
        bool hack = false;
        
        
      
        
        while(1){
                 if(GetAsyncKeyState(VK_DELETE))
                 {
                  if(hack)hack = false; // if true set to false
                  else hack = true;     //if false set to true
                  cout<<"Hack set to: " << hack <<endl;
                 }
                  
                  if(hack) runHack(); //will run if hack is turned on!
                  else cout<<"Hack is turned off!" <<endl;
        }
        return 0;
    }
    Notice there is no sleep time in the above hack! This means that the program could iterate a 100 times before you lift ur finger off the keyboard. Your just as likely to turn off as you are to turn it back on. When you create a hack you don't want to leave ur user with a 50:50 chance like this, plus ur loop is eating up much more CPU time then it needs!

    The Right way to set bools
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    void runHack()
        {
                 cout << "Running hack." <<endl;
                 return;
        }
        
    int main()
    {
        int i = 0;
        bool hack = false;
        
        
      
        
        while(1){
                 if(GetAsyncKeyState(VK_DELETE))
                 {
                  if(hack)hack = false; // if true set to false
                  else hack = true;     //if false set to true
                  cout<<"Hack set to: " << hack <<endl;
                 }
                  
                  if(hack) runHack(); //will run if hack is turned on!
                  else cout<<"Hack is turned off!" <<endl;
      
                 Sleep(200);
        }
        return 0;
    }
    A lot of people for some reason thing that booleans should iterate quicker or that the best hacks have faster clock cycles. This simply not true. The best hacks actually have lower clock cycles, eat up less CPU time and therefore run smoother! Notice I even set the Sleep to 100 here, because it is a boolean expression it needs less monitoring. It only needs to be set to on or off. You could even set it to Sleep(500) with little difference to the user.

    Now before I continue on I want to touch on bools a little bit more. A common error I see is poor logic control.

    Example of bad logic:
    Code:
    if(GetAsyncKeyState(VK_DELETE))
                 {
                  hack = true;     //just sets to true!
                  cout<<"Hack set to: " << hack <<endl;
                 }
    THis is terrible logic! How is the user supposed to turn the hack off hmmm? o_O

    More bad logic:
    Code:
    if(GetAsyncKeyState(VK_DELETE))
                 {
                  if(hack)hack = true;     //just sets to true!
                  cout<<"Hack set to: " << hack <<endl;
                 }
    this says if hack is true then set hack to true... it was a nice attempt, but again fails

    Good logic:
    Code:
    if(GetAsyncKeyState(VK_DELETE))
                 {
                  if(hack)hack = false; // if true set to false
                  else hack = true;     //if false set to true
                  cout<<"Hack set to: " << hack <<endl;
                 }
    finally that's how you do it. This way the bool's value is changed everytime the Del key is pressed!

    Pro logic:

    Code:
    bool bMyVal = false;
    
    if(GetAsyncKeyState(VK_KEYHERE)) 
    bMyVal = !bMyVal; // toggle :)
    2 lines of code and does everything you need. Thx HD...



    OK. Final chapter
    This is the part that many people get confused on. Im sure all of you have seen this at one time or another:
    Code:
    if(GetAsyncKeyState(VK_DELETE)&0x8000) //tests the most significant bit (msb)
    Let me just start by saying for the most part people do this because they don't understand how GetAsyncKeyState works. Let's check MSDN:
    Quote Originally Posted by MSDN
    Return Value

    If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.
    Okay a lot of newbies read this and are like "WTF this mean o_O?". And I don't blame you, without a good understanding of programming logic and binary operations this doesn't make a whole lot of sense. So lets walk through this slowly.

    GetAsyncKeyState() returns a SHORT. I already touched on this in the beginning of this tutorial, but just to let it really sync in let me explain further. A SHORT value is processor specific. in other words a SHORT on a 16bit processor would be 1 byte long or a char sized value, while a SHORT on our modern 32bit computers are 16 bits or 2 bytes wide. And on the newer 64 bit computers it will be 32 bits wide and so on. The important thing to remember is that it is half the size of an integer always. Now I will use the a 32bit example in this tutorial because at the time of writing this most computers use 32bit processors.

    The return value:

    The short will have its Most significant bit set if the key is down at the instant the function is called. Now just remember that, because that is all you need to know. if you don't know anything about bits don't worry! All of this is unnecessary. All Im doing is explaining why it is unnecessary
    (in fact feel free to end the tutorial here if ur satisfied)

    The least significant bit:
    "and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks."

    Pay special attention to that last sentence because here is why. In the days of DOS, programs ran one at a time all in order. In other words windows gave all programs full access to its CPU and if one program wanted to take a particularly long time running and not give up control of the CPU it could. However with modern operating systems Windows only gives programs a set amount of time to run, then it interrupts their process and share the CPU with other programs so that one can't hog all the fun.
    With this also came the process of multithreading, now you don't need to know what this is though by all means look it up if you want, but just understand that multithreading creates parallelism which speeds up the execution of code in multiprocessor systems.

    What this means is code that was written like this:
    Code:
    if(GetAsynKeyState(VK_INSERT));  //first
    if(GetAsynKeyState(VK_DELETE)&1); //second
    can execute like this
    Code:
    if(GetAsynKeyState(VK_INSERT));  //first
    if(GetAsynKeyState(VK_DELETE)&1); //same time
    So while this testing the least significant bit thing was okay for 16 bit DOS. It is not okay for modern multithreaded system. Now given it isn't exactly like that. This is more psuedo code thin anything, when ur accessing API functions this could very well be the case, and it is only still there for backwards compatibility.

    Removing the least significant bit from the equation means that only the most significant bit is left. And since this:

    Code:
    if(GetAsynKeyState(VK_INSERT)&0x8000)
    Does the same thing as this:
    Code:
    if(GetAsynKeyState(VK_INSERT))
    ... There is no longer any need to even mess with MSB or LSB at alll and you misewell save time as well as peace of mind and do it the easy way.

    Thanks for reading, why06
    Last edited by why06; 02-27-2010 at 04:06 PM.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  2. The Following 27 Users Say Thank You to why06 For This Useful Post:

    0rbit (08-05-2010),ac1d_buRn (02-28-2010),Alroundeath (06-27-2010),dban0001 (12-10-2013),desertflame (07-07-2014),doofbla (08-21-2010),falzarex (02-27-2010),Frosttall (11-03-2016),KABLE (02-27-2010),Kalisnoir (12-10-2012),lalakijilp (02-27-2010),Lyoto Machida (05-24-2011),Matrix_NEO006 (02-27-2010),MegaProphet (05-31-2016),NextGen1 (02-27-2010),noremy016 (01-31-2017),Obama (02-27-2010),ovenran (09-04-2010),Pixipixel_ (02-27-2010),Retoxified (02-27-2010),rscaerzx (05-18-2012),Unbelivable (05-17-2010),Void (02-27-2010),crex (02-03-2013),[Banned]mark0108 (09-04-2010),_corn_ (12-28-2011),|-|3|_][({}PT3R12 (03-02-2010)

  3. #2
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    Thanks saves a lot of people from reinventing the wheel

    are you saying in the last part of the tutorial that the &0X8000 is unnecessary or am I understanding you wrong??

  4. #3
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Quote Originally Posted by lalakijilp View Post
    Thanks saves a lot of people from reinventing the wheel

    are you saying in the last part of the tutorial that the &0X8000 is unnecessary or am I understanding you wrong??
    Exactly, I'm saying its completely unnecessary and in no case would you ever need to do it.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  5. #4
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    Quote Originally Posted by why06 View Post
    Exactly, I'm saying its completely unnecessary and in no case would you ever need to do it.
    then why do people use it....

  6. #5
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    I learned something.

  7. #6
    NextGen1's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Not sure really.
    Posts
    6,312
    Reputation
    382
    Thanks
    3,019
    My Mood
    Amazed
    Very Nice , I Like it


     


     


     



    The Most complete application MPGH will ever offer - 68%




  8. #7
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Quote Originally Posted by lalakijilp View Post
    then why do people use it....
    guess one person did it and everyone else follows, but I imagine if you want to be specific there can be a use for the &1 but MSDN states it can't truly be relied upon in OS that use multithreading, but if you know the specifics of it don't let me tell you otherwise.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  9. #8
    Obama's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    The Black house
    Posts
    22,195
    Reputation
    870
    Thanks
    6,076
    My Mood
    Cool
    Didn't understand a bit but its well formatted
    /thanks

  10. #9
    Retoxified's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    148
    Reputation
    8
    Thanks
    171
    Nice tut, nice format
    +rep

  11. #10
    Pixipixel_'s Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    VCExpress.exe || France :D
    Posts
    2,087
    Reputation
    27
    Thanks
    742
    My Mood
    Cool
    Very nice tut.

  12. #11
    Retoxified's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    148
    Reputation
    8
    Thanks
    171
    oh yeah
    Setting bools:

    Code:
    bool bMyVal = false;
    
    if(GetAsyncKeyState(VK_KEYHERE)&1)
    {
        bMyVal = !bMyVal; // toggle :)
    }

  13. The Following 3 Users Say Thank You to Retoxified For This Useful Post:

    falzarex (02-27-2010),lalakijilp (02-27-2010),why06 (02-27-2010)

  14. #12
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Oh yeh, pro toggle, that's how its done!


    But no &1 ;l...

    EDIT: added ur example
    Last edited by why06; 02-27-2010 at 04:08 PM.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  15. #13
    Matrix_NEO006's Avatar
    Join Date
    Feb 2008
    Gender
    male
    Posts
    240
    Reputation
    12
    Thanks
    33
    My Mood
    Lonely
    remember that this will work on anything but MFC.

  16. The Following User Says Thank You to Matrix_NEO006 For This Useful Post:

    why06 (02-28-2010)

  17. #14
    falzarex's Avatar
    Join Date
    Apr 2008
    Gender
    male
    Location
    here
    Posts
    417
    Reputation
    14
    Thanks
    145
    but I r computerz I spam keyboardz 1000keys/min lol jk
    nice tut
    oh yeah btw is it true that sleep will help the older computers cope better with load?
    I never bothered using sleep when coding coz it still works Fine on my quadcore
    Quote Originally Posted by falzarex aka myself
    GTFO FUCKER U DONT BELONG IN THE INTERNETZ WORLD COZ ITS MINE


    This is an epic fail resume
    Hello VBfags.
    A 'member' of the almighty C++ section will soon join you, he is 13 year old, has the IQ and typing skills of a VBfag, so I thought he would fit in here nicely.

    A few reasons why he should be in this section instead of the C++ section:
    1) He has the IQ of a VBfag.
    2) He has no sense of grammer/spelling at all.
    3) He thinks he is pro(like most of the people in here)
    4) He thinks copy pasting is fun(exactly what you guys do)
    5) He loves it up the ass(he will keep you VBfags nice and warm)

  18. #15
    KABLE's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    California
    Posts
    2,863
    Reputation
    192
    Thanks
    282
    My Mood
    Pensive
    Quote Originally Posted by falzarex View Post
    but I r computerz I spam keyboardz 1000keys/min lol jk
    nice tut
    oh yeah btw is it true that sleep will help the older computers cope better with load?
    I never bothered using sleep when coding coz it still works Fine on my quadcore
    Showoff

    Quote Originally Posted by TOXIN
    Shit, now I have to enter this chapacha shit.
    my tumblr
    How To: Not Get Banned Botting

    "Had a dream I was king. I woke up, still king."
    .................................................-Eminem

Page 1 of 2 12 LastLast

Similar Threads

  1. Guide On Using Olly Debugger
    By Dave84311 in forum Game Hacking Tutorials
    Replies: 1
    Last Post: 12-14-2013, 11:12 PM
  2. [Help] What is wrong with my use of GetAsyncKeyState?
    By yodaliketaco in forum C++/C Programming
    Replies: 18
    Last Post: 06-25-2011, 06:45 PM
  3. [Help] How to use Friendly Fire properly?
    By roylytammy in forum Vindictus Farming Discussions / Farming Help
    Replies: 5
    Last Post: 06-11-2011, 03:40 PM
  4. GetAsyncKeyState how to properly use it?
    By Mr.Magicman in forum C++/C Programming
    Replies: 19
    Last Post: 07-30-2010, 03:42 AM
  5. How to Use Tsearch
    By wardo1926 in forum Hack Requests
    Replies: 5
    Last Post: 12-18-2007, 09:24 PM