Use C # programming to check the status of the Scroll Lock key

Source: Internet
Author: User

Use C # programming to check the status of the Scroll Lock key

Flower pure spring @ http://blog.csdn.net/metababy

The function of the Scroll Lock key is similar to that of Num Lock and Caps Lock. You can try it on your computer. It mainly means window scrolling.

 

GetKeyStateFunction

The Scroll Lock key is rarely used on a computer. But in Excel, sometimes you will find a strange problem. The result 1 will find the Scroll Lock key problem. For example, when the scroll lock key is pressed, you can use the up and down keys to move the cursor to the front or back grid. You will find that the cursor is not moving, but the window is moving.

TheGetKeyStateWindows API function. This requires the use of the System. Runtime. InteropServices namespace.

Using System. Runtime. InteropServices;

Use the Windows API function-GetKeyState. This requires the System. Runtime. InteropServices namespace.

Using System. Runtime. InteropServices;

The GetKeyState function is from user32.dll. It only needs one parameter, that is, the key you want to check the status.

Use the following code snippet to import the dll and define the key value.

[DllImport ("user32.dll")]

Public static extern short GetKeyState (int keyCode );

Int VK_SCROLL = 0x91;

GetKeyState returns a 16-bit integer, which contains two important bits.

If the high sequence is 1, the key is DOWN; otherwise, the key is UP.

If the low sequence is 1, the key is triggered. For example, the caps lock key is triggered when it is opened. If the low-order position is 0, the key is disabled and not triggered. The indicator light of the trigger key on the keyboard. when the key is triggered, it is on and off when it is not triggered.

For details, seeHttp://baike.baidu.com/view/1080073.html

With the knowledge of its principle, we use the following code to convert its 0 and 1 states to the bool type.

Bool scrollLock = Convert. ToBoolean (GetKeyState (VK_SCROLL) & 1 );

Finally, we use this code to test:

If (scrolllLock)

MessageBox. Show ("Scroll Lock is enabled !");

Check the status of other keys

The GetKeyState function can be used to check the status of any key. For example, the following definitions are used to check Caps Lock and Num Lock:

Int VK_CAPITAL = 0x14;

Int VK_NUMLOCK = 0x90;

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.