Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ and GetAsyncKeyState() function

Tags:

c++

key

winapi

As it gives only Upper case letters, any idea how to get lower case?? If the user simultaneously pessed SHIFT+K or CAPSLOCK is on,etc, I want to get lower cases.. is it possible in this way or another??

Thanks,

like image 926
snoofkin Avatar asked Jan 25 '26 05:01

snoofkin


2 Answers

Suppose "c" is the variable you put into GetAsyncKeyState().

You may use the following method to detect whether you should print upper case letter or lower case letter.

string out = "";

bool isCapsLock() { // Check if CapsLock is toggled
    if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) // If the low-order bit is 1, the key is toggled
        return true;
    else
        return false;
}

bool isShift() {  // Check if shift is pressed
    if ((GetKeyState(VK_SHIFT) & 0x8000) != 0) // If the high-order bit is 1, the key is down; otherwise, it is up.
        return true;
    else
        return false;
}

if (c >= 65 && c <= 90) { // A-Z
    if (!(isShift() ^ isCapsLock())) { // Check if the letter should be lower case
        c += 32;  // in ascii table A=65, a=97. 97-65 = 32
}
out = c;
like image 95
Brian Avatar answered Jan 27 '26 18:01

Brian


As you rightly point out, it represents a key and not upper or lower-case. Therefore, perhaps another call to ::GetASyncKeyState(VK_SHIFT) can help you to determine if the shift-key is down and then you will be able to modify the result of your subsequent call appropriately.

like image 39
Moo-Juice Avatar answered Jan 27 '26 17:01

Moo-Juice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!