Thinking about how to use VB's keycode and keyascii to scan code and virtual code

Source: Internet
Author: User

I believe that all the friends who use VB know keycode and keyascii. Keycode parameters are included in keydown and keyup events, and keyascii parameters are included in keypress events. These two parameters are used in many cases.

I didn't know much about the differences between the two parameters. The only clear difference is that the keycode range is wider than that of keyascii, because some buttons do not trigger keypress events. Until a simulated button is created a few days ago.ProgramThe problem was raised only when the target program did not respond to my analog Keyboard Message. After reading the materials, I found that there are still many other statements. In view of the chaotic information on the Internet, I will summarize them here to facilitate learning.

Keycode parameter refers to the keyCode, That isVirtual code. It is used inSystemIdentifies a key. Note that it is only used for identification in the system. We usually do not directly use this code (numbers, but are hard to remember), but define it as a character constant. For example, the Home Key's virtual code is & h24, the constant vbkeyhome is used in VB and vk_home is used in C. Of course, you do not need to declare these constants. You can simply use them. It must be noted that the virtual code of each key is unique, that is, it is case-insensitive and only used to indicate oneLocationDoes not indicate the state of the key.

The keyascii parameter is easy to understand. Of course, it is the key ASCII code, which indicates a character rather than a key. Here we need to explain that the keypress event of VB does not respond to certain keys, such as direction keys, because these keys do not have the traditional ASCII code (these keys only have virtual codes. When I checked the information on the Internet, I found that many people put the virtual code in ASCII, which is very easy to confuse readers ). How can I identify whether a key has an ascii code? Here I will teach you a method that is easier to understand. Keys with ASCII codes areGraphical keys, Can be expressed with symbols, such as a row of number keys on the top of the letter, usually expressed as "1", hold down shift to represent "!", These areSpecific.F1, direction keys, and so on areAbstract(We cannot always express F1 as "f1 !), There is no ASCII matching. From this we can see that,ASCII code and virtual Code complement each other and have no subordination. Based on this consideration, we must be especially careful when using it. A simple example:

 
Private sub form_keydown (keycode as integer, shift as integer) msgbox keycodeend sub

 

This Code identifies virtual code, which emphasizes location, so we press the number key on the keypad and the number key on the letter to display a different virtual code! Because the location is different.

 

 
Private sub form_keypress (keyascii as integer) msgbox keyasciiend sub

This Code recognizes the ASCII code, so no matter which number "1" on the keyboard you press, the result is the same, because these keys represent the number 1. But for the number key on the top of the letter, we can hold down SHIFT first and then press the number key. At this time, the value will be different because the symbol it represents has changed. For the number key "1, after holding down shift, the key indicates "!". ", Therefore, ASCII is different (shift cannot be directly held in the first code, even if you press it, the virtual code of the key will not be changed. If you observe carefully, you will find that there is an additional parameter shift as integer in the first code, this parameter is used to identify whether to hold down the shift and CTRL keys .).

Next, let's take a look at what isScan code(Easy to understand ):

Each key on the keyboard is marked with two unique values. Why should we use two numbers instead of one? This is because a key can be pressed or released. When a key is pressed, they generate a unique value. When a key is released, it also generates a unique value. We store these values in a table, then you can use the table to find out which key was hit and whether it was pressed or released. These values are called Keyboard Scan codes in the system.

Okay, so far, with the above understanding, there will be a process of linking up, and everything will be understood! (In the above discussion, I emphasized that the virtual Code only exists in the system. Why do you understand the working mechanism of the keyboard ):

StartWoven Network, Keyboard working mechanism:

When you press a key,

1. the keyboard detects this action and sends the scan code to the computer through the keyboard controller. The scan code is related to the specific hardware, different vendors may have different scanning codes for the same key.

2. After receiving the scan code, the computer sends it to the keyboard driver;

3. the keyboard driverScan code to keyboard virtual codeThe virtual code has nothing to do with the specific hardware. The virtual code of the same key is always the same for the keyboard of different manufacturers. Then, the keyboard driverScan code and virtual codeAnd other information transmitted to the operating system;

4. The operating system encapsulates the obtained information in a Keyboard Message and sends the Keyboard Message.Insert to message queue.

5. In Windows message system, the keyboard message isSent to a window;

6. After receiving a message, the application where the window is located can learn about keyboard operations and then decide to make a response.

The above is the entire process in which the system receives keyboard messages. Now I suddenly realized it?

Here we will also mention the function used to simulate keyboard messages: keybd_event (the specific usage of Baidu ). This API function is the underlying analog Keyboard Message, so it is so real that the program cannot identify whether the keyboard message is sent or simulated. However, when using this function, you need to note that sometimes the program can still identify us to forge messages. Why? Note this sentence in the keyboard Running Mechanism: "Then, the keyboard driver operatesScan code and virtual codeAnd other information is transmitted to the operating system. Generally, when simulating the keyboard, we only pass the virtual code of the key to be simulated in the first parameter of the keybd_event function. Generally, this is enough, however, for programs with protective measures (such as games), you need to input the corresponding scan code in the second parameter of the keybd_event function. The API function for converting a virtual code to a scan code is mapvirtualkey. A complete example (simulate pressing the Enter key ):

 

Private declare sub keybd_event lib "USER32" (byval bvk as byte, byval scan as byte, byval dwflags as long, byval dwextrainfo as long) private declare function mapvirtualkey lib "USER32" alias "mapvirtualkeya" (byval wcode as long, byval wmaptype as long) as longconst keyeventf_keyup = & H2 'release key constant 'the following code is placed in an appropriate event to call keybd_event (13, mapvirtualkey ("13", 0), 0, 0) 'Press enter to call keybd_event (13, mapvirtualkey ("13", 0), keyeventf_keyup, 0) 'to release the carriage return

Another classic application of keypress events isValidity Verification. When the vbprogram receives a keyboard message, the message first arrives at the keypress event. After processing is completed in this event, the subsequent processing is performed. At the same time, the vbprogram allows us to change the keyascii value in the keypress event, therefore, we can determine whether the input is valid. If the input is invalid, we set the keyascii value to 0. In this way, we discard the input, which means that we cannot enter characters out of the allowed range.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.