The difference between getkeystate, getasynckeystate, and getkeyboardstate functions:
1, BOOL getkeyboardstate (pbyte lpkeystate); Gets all 256 keys (keyboard keys, mouse buttons, and so on) of the state, Lpkeystate is a pointer to a 256bit array, storing all the state of the keys.
2, short getasynckeystate (int vkey), determine whether the key virtual-keycodes to Vkey is down or up. The highest bit of the return value represents the state of the key (up or down), with the lowest bit of the return value indicating whether the Vkey key was pressed before this function call. Getasynckeystate directly detects hardware interrupts on the keyboard.
3, short getkeystate (int nvirtkey), used to judge the state of Nvirtkey. The highest bit of the return value indicates that the highest bit is 1, indicating that the current key is down, and that the highest bit is 0 the current key is in the up state. This function reads messages from the message queue for processing.
Here is the content reproduced: http://bingtears.iteye.com/blog/663149
0x8000 & Getkeystate (Vk_shift); This sentence is to determine if there is a shift key pressed
Why Getasynckeystate () &
First of all, there are a lot of programs or books are 0x8000f, this f is not hexadecimal f but represents floating point number. In fact & 8000 is the essence. Small fish I have been finishing my own writing something, summed up
First introduce a few concepts:
Bitwise AND Operator "&": is a binocular operator whose function is to participate in the corresponding binary phase of the two number of operations. Only the corresponding two binaries are 1 o'clock, the result bit is 1, otherwise 0. The number of participating operations appears in a complementary fashion. For example: The result of 0x11 & 0x12 (i.e. 0001 0001 & 0001 0010) is 0x10 (0001 0000); (vs. reverse reference attached)
Virtual key: Refers to a key that can be explicitly represented by a non-letter. (such as ESC BS TAB NumLock, etc., the virtual key list is attached);
Physical key state: In the Control Panel of the operating system to set the right mouse button mapping (the actual left mouse button can be mapped to a right-click event), or through the program can also be set, so that the (actual) physical key state;
Logic key state: Use Getkeystate,getkeyboardstate, etc function to get the logic key state, simulate press the button;
getasynckeystate function Function: Read is the physical key state, is also no matter how you mouse keyboard mapping, it only read the actual key state. MSDN gives an example that is appropriate for example, the calling Getasynckeystate (Vk_lbutton) always returns the state of the left physical mouse button , regardless of whether it is mapped to the left or right logical mouse button. That is, if you reset the mapping, Getasynckeystate still read only the physical state;
Getasynckeystate return value: represents two content, one is the highest bit value, which indicates whether the key is pressed, 1 is pressed, 0 is lifted, one is the lowest bit value, and is ignored under WINDOWSCE (refer to the most from msdnif Significant bit is set, and the key is down. The least significant bit is not valid in Windows CE, and should be ignored.)
Asynchronous: English meaning is asynchronous
What is the return value of getasynckeystate in practice? Little Fish I wrote a program to get the return value:
#include <Windows.h>
#include <stdio.h>
void Main ()
{
while (1)
{
Short a =:: Getasynckeystate (Vk_lshift)
printf ("0x%x", a);
Sleep (10);
}
}
Of course, this can be written in the MessageBox:
if (Short a =:: Getasynckeystate (Vk_lshift))
{
Char buffer[30];
sprintf (buffer, "0x%x", a);
MessageBox (0, buffer, "A's value", MB_OK);
}
Getasynckeystate The return value of the button not pressed or lifted when not pressed 0x0 is 0000 0000 0000 0000 0000 0000 0000 0000
Getasynckeystate The return value of the pressed key is returned 0xffff8001 that is 1111 1111 1111 1111 1000 0000 0000 0001 (This is not to return 4 bytes, but%x prints 32 bits, the first 16 bits of f)
0x8000 0000 0000 0000 0000 1000 0000 0000 0000
Getasynckeystate (vk_lshift) & 0x8000 Return to 0x1 that is 0000 0000 0000 0000 1000 0000 0000 0000
So why should getasynckeystate ' and ' 0x8000 this constant?
The answer is: Get the key state, block out the other possible states, according to MSDN on the low should ignore.
Someone on the internet wrote this, meaning it's clear:
#define KEYDOWN (Vk_code) ((Getasynckeystate (vk_code) & 0x8000)? 1:0)
#define KEYUP (Vk_code) ((Getasynckeystate (vk_code) & 0x8000)? 0:1)
The program should be:
if (Getasynckeystate (vk_lshift) &&0x8000)
It is not right to write the following logic for a virtual key, although the result is the same:
if (Getasynckeystate (Vk_lshift))
So let the keyboard "up and down" start events can be written like this:
if (:: Getasynckeystate (vk_left) & 0x8000)
Code ...
if (:: Getasynckeystate (vk_right) & 0x8000)
Code ...
if (:: Getasynckeystate (VK_UP) & 0x8000)
Code ...
if (:: Getasynckeystate (Vk_down) & 0x8000)
Code ...
About Getasynckeystate and getkeystate differences:
Getasynckeystate above already said almost, about Getasynckeystate and getkeystate the most difference: getasynckeystate when the key is not pressed for the case of 0, The getkeystate starts at 0 when the key is not pressed, and then turns to 1 after a ' press lift ' and loops in turn.
Short getkeystate (int nvirtkey//Virtual-key code);
Function: Returns the state of a key, pressed, released, or locked (down, up, or toggled)
Parameters: Virtual key code (VK_). If the letter is a-Z, A-Z, or a number 0-9, the corresponding ASCII code (such as the ASCII code of the letter O is 16 0x4f)
Return value: The high position of the return code shows whether a key is currently pressed, low (0-bit) shows the status of NumLock, CapsLock, ScrollLock (on or off, the keyboard light on when on). That is, the high level is 1, the return value is less than 0, indicating that a key is pressed, the lowest bit is 1 means in the locked (on) state (refer to msdn:if the High-order bit is 1, the key was down; otherwise, it's up.
If the Low-order bit is 1, the key is toggled. A key, such as the Caps LOCK key, is toggled if it's turned on. The key is off and untoggled if the Low-order bit is 0. A Toggle key ' s indicator light (if any) on the keyboard would be in when the key was toggled, and off when the key is Untogg Led. )
Note: This function should not be used outside of the keyboard message handler because the information it returns is valid only after the keyboard message has been retrieved from the message queue. If you really need to, please use getasynckeystate
----------------------------------------
Some information has also been found online:
About the difference from several other functions:
Short getkeystate (int nvirtkey);
Short getasynckeystate (int vkey);
BOOL getkeyboardstate (pbyte lpkeystate);
The maximum difference between the three key status functions is:
The first one is to get the keyboard message from the Windows message queue and return to the key status.
The second one is to directly detect the hardware interrupt of the keyboard and return to key status.
Third: The key status is returned when a keyboard message is removed from Windows Message Queuing.
keybd_event function, is the simulation keyboard keystroke, a complete keystroke simulation event, is "press" and "bounce" two messages, so keybd_event (vk_f12,0,0,0); keybd_event (vk_f12,0,keyeventf_ keyup,0); Completed a full click of the F12 event.
The Getasynckeystate () function is a hardware interrupt that detects the keyboard directly. (Some people say that it is a "real-time" detection, which feels wrong, such as you call Sleep (), even if it is interrupted for a year, as long as the program is still running, it can be the state of the key to detect it). Since the last call to the Getasynckeystate () function (in some loops, n calls Getasynckeystate (), it is checked every time since the last call, the state of the key), if the key has been pressed, return 1, otherwise, 0 Some data shows that if the input focus is from another thread that is different from the input thread that called the function, 0 is returned (for example, 0 should be returned when another program has input focus). Experiments have shown that this is not entirely true, the function is actually working in most areas, and only a few are otherwise.
---------------
Report:
VC + + compiler, calculate the ~, the result is-11. Why not 5?
The binary representation of 10 is 1010, the bitwise inverse should be 0101, that is, 5 of the decimal, why do you draw-11?
VC is a 32-bit compiler, so
10 = 00000000 00000000 00000000 00001010
~ = 11111111 11111111 11111111 11110101 = 11
Can be passed through a mask (bit and) with 15 bits and
15 = 00000000 00000000 00000000 00001111
~ = 00000000 00000000 00000000 00000101 = 11
Report:
Vk_lbutton left mouse button 0x01
Vk_rbutton right mouse button 0x02
Vk_cancel Ctrl + Break 0x03
Vk_mbutton Mouse Middle Key 0x04
Vk_back Backspace Key 0x08
Vk_tab TAB Key 0x09
Vk_return Enter 0x0D
Vk_shift SHIFT Key 0x10
Vk_control Ctrl Key 0x11
Vk_menu Alt Key 0x12
Vk_pause PAUSE Key 0x13
Vk_capital caps Lock Key 0x14
Vk_escape ESC Key 0x1B
Vk_space Space bar 0x20
Vk_prior Page up key 0x21
Vk_next Page down key 0x22
Vk_end END Key 0x23
Vk_home HOME Key 0x24
Vk_left left ARROW key 0x25
Vk_up Up ARROW key 0x26
Vk_right RIGHT ARROW key 0x27
Vk_down down ARROW key 0x28
Vk_snapshot Print Screen Key 0x2C
Vk_insert Insert Key 0x2D
Vk_delete Delete Key 0x2e
' 0 ' – ' 9 ' digital 0-9 0x30-0x39
' A ' – ' Z ' letter A-Z 0x41-0x5a
Vk_lwin left Winkey (104 keyboards only) 0x5b
Vk_rwin Right Winkey (104 keyboards only) 0x5C
Vk_apps Appskey (104 keyboards only) 0x5d
VK_NUMPAD0 Keypad 0 Key 0x60
Vk_numpad1 Keypad 1 Key 0x61
Vk_numpad2 Keypad 2 Key 0x62
VK_NUMPAD3 Keypad 3 Key 0x63
VK_NUMPAD4 Keypad 4 Key 0x64
VK_NUMPAD5 Keypad 5 Key 0x65
VK_NUMPAD6 Keypad 6 Key 0x66
VK_NUMPAD7 Keypad 7 Key 0x67
Vk_numpad8 Keypad 8 Key 0x68
VK_NUMPAD9 Keypad 9 Key 0x69
Vk_f1-vk_f24 function Key F1–f24 0x70-0x87
Vk_numlock num Lock Key 0x90
Vk_scroll SCROLL Lock Key 0x91
The usage and difference of getkeystate and getasynckeystate and Getkeyboardstate functions