如何使用低级别的8位标志,如条件? [英] How do I use low-level 8 bit flags as conditionals?
问题描述
在我的键盘钩子,每个按键得到指出,如果它被注入与否的标志。
http://msdn.microsoft.com/en-us/library /ms644967(VS.85).aspx
In my keyboard hook, each keypress gets a flag that states if it was injected or not. http://msdn.microsoft.com/en-us/library/ms644967(VS.85).aspx
我蒸馏脱除lParam的一个KBDLLHOOKSTRUCT。我可以访问kbd.flags.XXX。我只是不知道如何将这种8bit的标志转换成如果(注射){...
键入条件,我知道如何使用。
I've distilled a KBDLLHOOKSTRUCT from the lParam. I can access kbd.flags.XXX. I just don't know how to convert this 8bit flag into an if (injected) {...
type conditional that I know how to use.
如果你聪明的计算机科学类型的人会帮助我我会很感激。
If one of you smart computer-science types would help me out I'd really appreciate it.
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
KBDLLHOOKSTRUCT kbd = new KBDLLHOOKSTRUCT();
Marshal.PtrToStructure(lParam, kbd);
//if (injected) {...
干杯!
Cheers!
推荐答案
您需要按位与它带着面具。例如,注入的位位4这是二进制00010000,十六进制为0x10。所以,你按位与它为0x10,并查看是否有左:
You need to bitwise-and it with a mask. For example, the injected bit is bit 4. That's binary 00010000, hex 0x10. So you bitwise-and it with 0x10, and see if anything's left:
bool isInjected = ((kbd.flags & 0x10) != 0);
(当然,按照安德鲁的答案,这将是一个好主意,定义一个LLKHF_INJECTED常数这个而不是直接包括代码的十六进制值!)
(Of course, as per Andrew's answer, it would be a good idea to define a LLKHF_INJECTED constant for this rather than including the hex value directly in your code!)
这篇关于如何使用低级别的8位标志,如条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!