如何检查返回的位掩码值?
我从 C# 调用这个函数: GetKeyboardStatus()
查看文档,它说它返回一个位掩码价值。我的代码的目标是确定设备是否具有带有字母数字字符的物理键盘。我已经成功调用了这个函数,返回值是15。但是,由于我不理解位掩码,我不知道如何将它与0x0008值进行比较,根据文档“指示键盘硬件是否有字母数字键。”。我不会将此标记为 Windows Mobile 或 Compact Framework 问题,因为我认为回答我的问题您需要了解的只是位掩码和 C#,我希望答案将扩展我对如何使用位掩码的理解(但不是必需的)。这是我的代码。我认为唯一错误的部分是返回语句:
public static bool HasAlphaNumericKeys {
get {
const uint KBDI_KEYBOARD_ALPHA_NUM = 0x0008;
uint returnValue = GetKeyboardStatus();
return returnValue == KBDI_KEYBOARD_ALPHA_NUM;
}
}
[DllImport("coredll")]
private static extern uint GetKeyboardStatus();
感谢您的帮助,但我发现这不是确定是否存在带字母数字键的物理键盘的可靠方法。我尝试了 2 种设备,一种有键盘,一种没有,GetKeyboardStatus 函数为这两种设备返回了 15,所以我什至无法测试答案中位掩码的解释。
I am calling this function from C#:
GetKeyboardStatus()
Looking at the documentation it says that it returns a bit mask value. The goal of my code is to determine if the device has a physical keyboard with alphanumeric characters. I have successfully called this function and the return value is 15. However since I don't understand bit masks, I don't know how to compare it to the 0x0008 value, which according to the documentation "Indicates whether or not the keyboard hardware has alphanumeric keys.". I am not tagging this as a Windows Mobile or Compact Framework question because I think all you will need to understand to answer my question is bit masks and C# and I am hoping the answer will expand my understanding of how to work with a bit mask (not required though). Here is my code. I think the only part that is wrong is the return statement:
public static bool HasAlphaNumericKeys {
get {
const uint KBDI_KEYBOARD_ALPHA_NUM = 0x0008;
uint returnValue = GetKeyboardStatus();
return returnValue == KBDI_KEYBOARD_ALPHA_NUM;
}
}
[DllImport("coredll")]
private static extern uint GetKeyboardStatus();
Thanks for trying to help but I have discovered that this is not a reliable way to determine if there is a physical keyboard with alphanumeric keys. I tried 2 devices, one with a keyboard and one without and the GetKeyboardStatus function returned 15 for both of them, so I can't even test the explanation of bit masks in the answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您实际上将按位运算作为二进制值写在一张纸上,那么按位运算会更容易理解
您 15(十进制)是二进制 1111 (2^3 + 2^2 + 2^1 + 2^0) = ( 8+4+2+1)
8(十进制)的二进制形式为 1000 (2^3 + 0 + 0 + 0) = (8+0+0+0)
逻辑与表示对于每一位,如果两个值都是1 则结果为 1,否则为 0
因此,郑重声明:逻辑 AND 运算的结果是一个数字,而不是真/假。
由于您希望结果设置 KBDI_KEYBOARD_ALPHA_NUM 中的所有位,因此我更愿意像这样进行检查,
仅当我想要 时,我才会将结果与 != 0 进行比较要设置 KBDI_KEYBOARD_ALPHA_NUM 中的任意位。由于在这种情况下只涉及 1 位,因此两者的工作原理相同。但为了说明差异:
当您希望设置所有位时,与 != 0 相比并不会使您的代码不言自明
The bitwise operations get a lot easier to understand if you actually write them on a piece of paper as binary values
You 15 (decimal) is in binary 1111 (2^3 + 2^2 + 2^1 + 2^0) = (8+4+2+1)
The 8 (decimal) is in binary 1000 (2^3 + 0 + 0 + 0) = (8+0+0+0)
A logical and means that for each bit if both value are a 1 then the result is a 1 otherwise a 0
So for the record: The result of the logical AND operation is a number and not true/false.
Since you want the result to have all the bits from KBDI_KEYBOARD_ALPHA_NUM set I would prefer to check like this
I would compare the result with != 0 only if I want any of the bits in KBDI_KEYBOARD_ALPHA_NUM to be set. Since in this case there is only 1 bit involved both will work the same. But to illustrate the difference:
Comparing to != 0 when you want all bits to be set doesn't make your code self-explanatory
尝试
return (returnValue & KBDI_KEYBOARD_ALPHA_NUM) != 0;
如果设置了 returnValue 的第 3 位,则返回 true,无论 returnValue 中任何其他位的值如何。
Try
return (returnValue & KBDI_KEYBOARD_ALPHA_NUM) != 0;
This returns true if bit 3 of returnValue is set, regardless of the values of any of the other bits in returnValue.
我相信按位运算符就是您想要的,特别是按位与(&)。按位与查看两个操作数的每一位,如果两位均为“1”,则返回“1”,否则返回“0”。因此,如果您将位掩码与特定标志值进行“与”操作,并得到非零结果,则您知道该位掩码包含该标志。
I believe bitwise operators are what you want, specifically bitwise-AND (&). Bitwise AND looks at each bit of the two operands and returns '1' if both bits are '1', and '0' otherwise. So if you AND a bitmask with a specific flag value, and get a non-zero result, you know that the bitmask contains the flag.
基本上你需要检查第四位是否被设置,所以只需使用按位与运算:
Basically you need to check whether fourth bit is set, so just use bitwise AND operation: