Skip to content Skip to sidebar Skip to footer

Python 3 Detect Caps Lock Status

I've been searching for a way to identify the status of the CAPS LOCK in Python 3 and the only thing I've found applicable was a post here in Stack Overflow answered by Abhijit sta

Solution 1:

From the looks of it, your value is being treated as a full-sized integer.

hllDll.GetKeyState gets its return value from the Win32 GetKeyState function seen here.

The return value from Windows is a Short. Your return value from the function was 361693184, which if you translate into binary is 10101100011110000000000000000. Notice the trailing 16 0-bits. I'm guessing that return value came from a test when you should have gotten a 0, and because it's trying to read a full 32-bit int, the top 16 bits are just garbage.

I would start by looking at your code to see why it might be assuming the value is a 32-bit integer. The joys of duck typing :)

I hope this helps! If this doesn't seem to be the problem, post some code where you call the function, so we can get a better look.


Solution 2:

Thanks, Gimson, that did help. I was able to resolve this by calling the value as below:

def CAPSLOCK_STATE():
    import ctypes
    hllDll = ctypes.WinDLL ("User32.dll")
    VK_CAPITAL = 0x14
    return hllDll.GetKeyState(VK_CAPITAL)

CAPSLOCK = CAPSLOCK_STATE()
if ((CAPSLOCK) & 0xffff) != 0:
    print("\nWARNING:  CAPS LOCK IS ENABLED!\n")

This does the trick.


Post a Comment for "Python 3 Detect Caps Lock Status"