Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    殺す必要がある唯一のものは殺されるために準備され人 々である。
    Premium Member
    Hitokiri~'s Avatar
    Join Date
    Oct 2012
    Gender
    female
    Location
    Cancer.
    Posts
    1,201
    Reputation
    24
    Thanks
    937
    My Mood
    Bitchy
    Just a correction.

    GetAsyncKeyState returns various key states based on bits set.

    You can determine if a key is being held down ( for a while ) by checking the highest bit and you can determine if it has been pressed once ( KeyUp + KeyDown ) by checking the lowest bit.

    Esentially:

    Code:
    bool KeyDown( int key, bool checkHeldDown = false ){
       return ( bool )GetAsyncKeyState( key ) >> ( checkHeldDown ? 31 : 0 );
    }
    P.S. NEVER just check if( GetAsyncKeyState() ) as the value returned could evaluate to true even though the key wasn't pressed. ( Check MSDN ) The above is guaranteed to work


    - - - Updated - - -

    Just a correction.

    GetAsyncKeyState returns various key states based on bits set.

    You can determine if a key is being held down ( for a while ) by checking the highest bit and you can determine if it has been pressed once ( KeyUp + KeyDown ) by checking the lowest bit.

    Esentially:

    Code:
    bool KeyDown( int key, bool checkHeldDown = false ){
       return ( bool )GetAsyncKeyState( key ) >> ( checkHeldDown ? 31 : 0 );
    }
    P.S. NEVER just check if( GetAsyncKeyState() ) as the value returned could evaluate to true even though the key wasn't pressed. ( Check MSDN ) The above is guaranteed to work

  2. #17
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    @Hitokiri~
    P.S. NEVER just check if( GetAsyncKeyState() ) as the value returned could evaluate to true even though the key wasn't pressed. ( Check MSDN )
    https://msdn.microsof*****m/en-us/lib...=vs.85%29.aspx

    Return value

    Type: SHORT

    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.
    msdn: "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. "

    you: "...as the value returned could evaluate to true even though the key wasn't pressed."

    Which bit(s) and why?

    Also, since the return value is short, you meant to shift-left 15 instead of 31?
    Last edited by abuckau907; 04-12-2015 at 10:51 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  3. #18
    殺す必要がある唯一のものは殺されるために準備され人 々である。
    Premium Member
    Hitokiri~'s Avatar
    Join Date
    Oct 2012
    Gender
    female
    Location
    Cancer.
    Posts
    1,201
    Reputation
    24
    Thanks
    937
    My Mood
    Bitchy
    Quote Originally Posted by abuckau907 View Post
    @Hitokiri~

    https://msdn.microsof*****m/en-us/lib...=vs.85%29.aspx


    msdn: " 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. "

    you: "...as the value returned could evaluate to true even though the key wasn't pressed."

    Which bit(s) and why?

    Also, since the return value is short, you meant to shift-left 15 instead of 31?
    Yep, I made a mistake.
    Forgot it was a word and not a double word.


    About which bit(s), I read it somewhere as well as it being from personal experience.
    Last edited by Hitokiri~; 04-12-2015 at 06:38 PM.

  4. #19
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    @Hitokiri~ Happens to all of us : ) ..most small values are upcast to int32/64 anyway...I do the same thing.

    P.S. NEVER just check if( GetAsyncKeyState() ) as the value returned could evaluate to true even though the key wasn't pressed. ( Check MSDN )
    I think you mean
    P.S. NEVER just check if( GetAsyncKeyState() ) as the value returned could evaluate to true even though the key [isn't currently down]'. ? ( Check MSDN )
    maybe?
    ie. where msdn says "was pressed after the previous call to getasynckeystate. ? If so, that's good advice..because msdn says not to rely on it, but in this case; doing the action on Key_UP, it could only help us (ie. pretend that because of thread timing we don't catch the key in the down state..but the 'pressedSinceLastCall' bit gets set, so we will be aware of the keypress). Again, msdn says not to rely on this, but in the case of doing the action on key_up, it can only help, so I'd leave it. I think.

    -----------------------
    @hiimachicken1

    Code:
      if (GetAsyncKeyState(VK_F1)) 
       { //key is currently down
         wasKeyDown = true;
       }
       else
       {//key isn't currently down, but was it down a  moment ago?! 
         if (wasKeyDown)
           {
             //do the thing
             mem.Write(0x4807B5, (BYTE)117);
             wasKeyDown = false; //have to reset it..
           }
       }
    could be shortened to:
    Code:
        if (GetAsyncKeyState(VK_F1))
        { //key is currently down
          wasKeyDown = true;
        }
        else if (wasKeyDown) //key isn't currently down, but was it down a  moment ago?! 
        { 
          mem.Write(0x4807B5, (BYTE)117);
          wasKeyDown = false; //have to reset it..
        }
    exactly the same thing, but slightly cleaner, imo.
    Last edited by abuckau907; 04-12-2015 at 11:22 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  5. The Following User Says Thank You to abuckau907 For This Useful Post:

    Hitokiri~ (04-13-2015)

  6. #20
    jkfauvel's Avatar
    Join Date
    May 2014
    Gender
    male
    Location
    São Paulo
    Posts
    267
    Reputation
    10
    Thanks
    1,234
    My Mood
    Cool
    Quote Originally Posted by Tonyx97 View Post
    I can share my KeyCode.h for you guys, I made it 2 months ago and it works perfectly for me:
    Code:
    #include <windows.h>
    
    #define LEFT_MOUSE_BUTTON 0x01
    #define RIGHT_MOUSE_BUTTON 0x02
    #define MIDDLE_MOUSE_BUTTON 0x04
    #define A 0x41
    #define B 0x42
    #define C 0x43
    #define D 0x44
    #define E 0x45
    #define F 0x46
    #define G 0x47
    #define H 0x48
    #define I 0x49
    #define J 0x4A
    #define K 0x4B
    #define L 0x4C
    #define M 0x4D
    #define N 0x4E
    #define O 0x4F
    #define P 0x50
    #define Q 0x51
    #define R 0x52
    #define S 0x53
    #define T 0x54
    #define U 0x55
    #define V 0x56
    #define W 0x57
    #define X 0x58
    #define Y 0x59
    #define Z 0x5A
    #define K_0 0x30
    #define K_1 0x31
    #define K_2 0x32
    #define K_3 0x33
    #define K_4 0x34
    #define K_5 0x35
    #define K_6 0x36
    #define K_7 0x37
    #define K_8 0x38
    #define K_9 0x39
    #define BACKSPACE 0x08
    #define TAB 0x09
    #define ENTER 0x0D
    #define CONTROL 0x11
    #define ALT 0x12
    #define CAPSLOCK 0x14
    #define ESCAPE 0x1B
    #define SPACE 0x20
    #define ARROW_LEFT 0x25
    #define ARROW_UP 0x26
    #define ARROW_RIGHT 0x27
    #define ARROW_DOWN 0x28
    #define KDELETE 0x2E
    #define LWIN 0x5B
    #define RWIN 0x5C
    #define NUM_0 0x60
    #define NUM_1 0x61
    #define NUM_2 0x62
    #define NUM_3 0x63
    #define NUM_4 0x64
    #define NUM_5 0x65
    #define NUM_6 0x66
    #define NUM_7 0x67
    #define NUM_8 0x68
    #define NUM_9 0x69
    #define NUM_ADD 0x6B
    #define NUM_SEPARATOR 0x6C
    #define NUM_SUBTRACT 0x6D
    #define NUM_DECIMAL 0x11
    #define NUM_DIVIDE 0x6F
    #define F1 0x70
    #define F2 0x71
    #define F3 0x72
    #define F4 0x73
    #define F5 0x74
    #define F6 0x75
    #define F7 0x76
    #define F8 0x77
    #define F9 0x78
    #define F10 0x79
    #define F11 0x7A
    #define F12 0x7B
    #define LSHIFT 0xA0
    #define RSHIFT 0xA1
    #define LCONTROL 0xA2
    #define RCONTROL 0xA3
    
    bool keyPressed[] =
    {
        false
    };
    
    bool keyUnpressed[][2] =
    {
        {true, false}
    };
    
    bool GetKeyDown (int _key)
    {
        if (GetAsyncKeyState(_key) && !keyPressed[_key])
        {
            keyPressed[_key] = true;
            return keyPressed[_key];
        }
        else if (!GetAsyncKeyState(_key) && keyPressed[_key])
        {
            keyPressed[_key] = false;
            return keyPressed[_key];
        }
        return false;
    }
    
    bool GetKeyUp (int _key)
    {
        if (GetAsyncKeyState(_key) && !keyPressed[_key])
        {
            if (!keyUnpressed[_key][1])
            {
                keyUnpressed[_key][1] = true;
            }
        }
        if (keyUnpressed[_key][1])
        {
            if (GetAsyncKeyState(_key) && keyUnpressed[_key][0])
            {
                keyUnpressed[_key][0] = false;
                return keyUnpressed[_key][0];
            }
            else if (!GetAsyncKeyState(_key) && !keyUnpressed[_key][0])
            {
                keyUnpressed[_key][0] = true;
                return keyUnpressed[_key][0];
            }
        }
        return false;
    }
    
    int GetKey (int _key)
    {
        if (GetAsyncKeyState(_key))
        {
            return GetAsyncKeyState(_key);
        }
        return 0;
    }
    Use GetKeyDown(Key) when you only need to call it 1 time like as want hiimachicken1.
    Use GetKeyUp (Key) when you want to call some function when a key is unpressed.
    Use GetKey (Key) is almost the same as GetAsyncKeyState.
    This library is very useful if you're going to work with a lot of keys
    PS: in the file you can find some useless stuff as the second array because you don't need another dimension for the array. But I don't touch it for now haha.
    Be careful with GetAsyncKeyState within loops, cause they tend to kill your CPU. Just make a keybd hook or something of the kind and if you want the VK code for the pressed key you can get easily.

    - - - Updated - - -

    Quote Originally Posted by Hitokiri~ View Post

    Code:
    bool KeyDown( int key, bool checkHeldDown = false ){
       return ( bool )GetAsyncKeyState( key ) >> ( checkHeldDown ? 31 : 0 );
    }
    P.S. NEVER just check if( GetAsyncKeyState() ) as the value returned could evaluate to true even though the key wasn't pressed. ( Check MSDN ) The above is guaranteed to work[/B][/SIZE][/COLOR][/FONT]
    You can use the bitwise AND operator aswell
    Example:
    Code:
    //Check if user is pressing F9
    if (GetAsyncKeyState(VK_F9) & 0x8000) {     //return > 0 only if highest bit is set. All other cases return = 0
    //code
    }
    As GetAsyncKeyState() is a nonblocking call(thus it will not wait for user input) it may not be the best option for some applications.
    Last edited by jkfauvel; 04-14-2015 at 01:25 PM.
    In the midst of chaos, there is also opportunity.

  7. #21
    殺す必要がある唯一のものは殺されるために準備され人 々である。
    Premium Member
    Hitokiri~'s Avatar
    Join Date
    Oct 2012
    Gender
    female
    Location
    Cancer.
    Posts
    1,201
    Reputation
    24
    Thanks
    937
    My Mood
    Bitchy
    Quote Originally Posted by jkfauvel View Post
    You can use the bitwise AND operator aswell
    I already know the C language.

  8. #22
    jkfauvel's Avatar
    Join Date
    May 2014
    Gender
    male
    Location
    São Paulo
    Posts
    267
    Reputation
    10
    Thanks
    1,234
    My Mood
    Cool
    Quote Originally Posted by Hitokiri~ View Post

    I already know the C language.
    OFC you do, but some don't. In case someone else doesn't understand, just giving the complete information(especially for OP)... LOL Just saying that the response wasnt completely meant for you.
    Last edited by jkfauvel; 04-14-2015 at 02:44 PM.
    In the midst of chaos, there is also opportunity.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. [Help Request] how to find memory offset
    By rotawo2 in forum Crossfire Coding Help & Discussion
    Replies: 8
    Last Post: 02-05-2013, 07:28 AM
  2. Not speed memory solution Help
    By wujia in forum Visual Basic Programming
    Replies: 7
    Last Post: 08-19-2010, 12:10 AM
  3. instruction at ---- reference memory at .......... HELP
    By moldavan55 in forum Combat Arms Help
    Replies: 12
    Last Post: 06-08-2010, 03:19 PM
  4. Changing memory adresses
    By bwoo in forum Call of Duty Modern Warfare 2 Help
    Replies: 10
    Last Post: 01-29-2010, 06:51 AM
  5. Time change I just help?
    By beda000 in forum Call of Duty Modern Warfare 2 Help
    Replies: 4
    Last Post: 11-30-2009, 05:25 PM