c++ changing memory offset help

Posts 16–22 of 22 · Page 2 of 2
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
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.
@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.microsoft.com/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?
Quote Originally Posted by abuckau907 View Post
@Hitokiri~

https://msdn.microsoft.com/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.
@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.
Quote Originally Posted by jkfauvel View Post
You can use the bitwise AND operator aswell
I already know the C language.
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.
Posts 16–22 of 22 · Page 2 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us