简体   繁体   中英

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 stating:

You can use ctypes to load user32.dll and then call GetKeyState with nVirtKey = VK_CAPITAL (0x14)

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

I've applied this to my script, but the value returned is not the anticipated 1/0, but a long 9 number sequence which never repeats. I'm not certain how to use this value to return the 1/0, T/F, or any true value to test against.

Any ideas, either based on Abhijit's comment or another method that works in Python 3? Your help is greatly appreciated, as this is driving me nuts.

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.

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.

Here is the minimal working answer, which I built upon the answer of danjmwalker

import ctypes

def is_capslock_on():
    return True if ctypes.WinDLL("User32.dll").GetKeyState(0x14) else False

print(is_capslock_on())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM