Home > Mobile >  How do you write an IsPressed (a function that should only return true on first press)?
How do you write an IsPressed (a function that should only return true on first press)?

Time:02-14

I need a function in a game loop that should only return true on first press.

I don't know which game I can give an example from which game is currently on the market, but I will explain this request in detail (I haven't played/can't play any of the modern games because I'm blind).

For example, there is an event you are listening to in the loop. When the user presses the S key, a text showing the last status of the character appears on the screen: Stamina, energy etc. This loop can run thousands of times per second, as the loops depend on the speed of the hardware and the code you write. We only need to detect the first press of the user. It should return false when the key is hold pressed. Otherwise, the last status of the user is shown thousands of times on the screen.

I am trying to provide this functionality using win32api. GetAsyncKeyState seems like the right option for this scenario but I don't understand how this can be done.

bool IsPressed(int key) {
return GetAsyncKeyState(key) & 1 != 0;
}

The function I wrote acts like a char event.

How can I write this?

I tried using other answers on StackOwerflow. However, I could not find an answer to the question I asked. Still, these answers helped me with some issues: What is the fastest way to determine a key press and key holding in Win32?

CodePudding user response:

While the GetAsyncKeyState can indicate via least significant bit whether the key has been pressed since the last call to this function, this behavior cannot be relied on.

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...

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the preemptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application.

Therefore, you can't rely on the Windows API to do the bookkeeping for you. I suggest wrapping any key queries in a class that you'll use for any button presses.


class KeyInput {
public:
    using key_type = int;

    enum class Transition {
        none,
        pressed,
        released,
    };

    struct PressResult {
        bool pressed;
        Transition transition;
    };

    PressResult state(key_type key) {
        PressResult result;
        result.pressed = (GetAsyncKeyState(key) < 0);
        if (auto it = last_states_.find(key); it != last_states_.end()) {
            if (last_states_[key] == result.pressed) {
                result.transition = Transition::none;
            }
            else if (result.pressed) {
                result.transition = Transition::pressed;
            }
            else {
                result.transition = Transition::released;
            }
        }
        else {
            result.transition = Transition::none;
        }

        last_states_[key] = result.pressed;

        return result;
    };

private:
    std::unordered_map<key_type, bool> last_states_{}; 
};

See it in action

Note

This will not capture transitions since the last iteration of your program. In order to handle that, you'll need your class to check every button press (that you care about) on every iteration.

CodePudding user response:

I do not know about C , but it certainly follows the same process in all languages, so you can set that pressure method to "if" and define a number that, by pressing its value, becomes one, And write. In "If" if the value of the number is equal to 1, do not do this again

In java, for example, it says:

int x = 0;
Button back = findViewById(R.id.button);
if(x == 0){
    back.setOnClickListener(v -> {
        //your code to do;
        x = 1;
    });
}else{}

that work in C too,

  • Related