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;