python 3检测大写锁定状态 [英] python 3 detect caps lock status

查看:100
本文介绍了python 3检测大写锁定状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来识别 Python 3 中 CAPS LOCK 的状态,我发现唯一适用的是 Abhijit 回答的 Stack Overflow 上的一篇帖子 说明:

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:

您可以使用 ctypes 加载 user32.dll,然后使用 nVirtKey = VK_CAPITAL (0x14) 调用 GetKeyState

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)

我已将此应用于我的脚本,但返回的值不是预期的 1/0,而是一个永不重复的长 9 数字序列.我不确定如何使用此值返回 1/0、T/F 或任何要测试的真值.

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.

任何想法,无论是基于 Abhijit 的评论还是其他适用于 Python 3 的方法?非常感谢您的帮助,因为这让我发疯.

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 从 Win32 GetKeyState 函数中获取返回值此处.

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

Windows 的返回值是一个 Short.函数的返回值是 361693184,如果你转换成二进制是 101011000111100000000000000000.注意尾随的 16 个 0 位.我猜测返回值来自测试时您应该得到 0,并且因为它试图读取完整的 32 位 int,所以前 16 位只是垃圾.

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.

我会先查看您的代码,看看为什么它可能会假设该值是一个 32 位整数.鸭子打字的乐趣:)

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.

这篇关于python 3检测大写锁定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆