GetKeyState() Verständnisproblem



  • struct key_type {
    	int key;
    	key_state state{ key_state::none };
    
    	void get() {
    		SHORT k = GetKeyState(key);
    
    		if (key < 0)
    			state = key_state::down;
    
    		else if (key&1 != 0)
    			state = key_state::toggled;
    
    		else
    			state = key_state::up;
    	}
    };
    
    #include <iostream>
    
    int main() {
    	key_type k;
    	k.key = 0x14;
    
    	while (true) {
    		k.get();
    
    		if (k.state == key_state::down)
    			std::cout << "down\n";
    
    		else if (k.state == key_state::toggled)
    			std::cout << "toggled\n";
    	}
    }
    

    Warum ist egal wie oft und wie lang ich CAPS drücke der Key immer auf key_state::up?

    Return value

    Type: SHORT

    The return value specifies the status of the specified virtual key, as follows:

    If the high-order bit is 1, the key is down; otherwise, it is up.
    If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

    Wenn das high-order bit 1 ist, ist der Key down. Short ist signed, deshalb if(key < 0).

    Ansonsten, wenn gesetzt, dann key&1 (damit prüfe ich den lowest bit).

    Ansonsten up.



  • wuehfbkjucvf schrieb:

    Warum ist egal wie oft und wie lang ich CAPS drücke der Key immer auf key_state::up?

    Die Antwort steht in den Remarks: "The key status returned from this function changes as a thread reads key messages from its message Queue."

    Die liest gar keine Messages (GetMessage), und deswegen ändert sich nichts. Versuch's mit GetAsyncKeyState.


Anmelden zum Antworten