C # control the keyboard buttons (case-sensitive buttons, etc)

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;

Namespace sn set keyboard case sensitivity
{Www.2cto.com
Public partial class Form1: Form
{
Const uint KEYEVENTF_EXTENDEDKEY = 0x1;
Const uint KEYEVENTF_KEYUP = 0x2;

[DllImport ("user32.dll")]
Static extern short GetKeyState (int foreign rtkey );
[DllImport ("user32.dll")]
Static extern void keybd_event (byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo );

Public enum VirtualKeys: byte
{
VK_NUMLOCK = 0x90, // number lock key
VK_SCROLL = 0x91, // scroll to lock
VK_CAPITAL = 0x14, // case-sensitive lock
VK_A = 62
}

Public Form1 ()
{
InitializeComponent ();
}

Public static bool GetState (VirtualKeys Key)
{
Return (GetKeyState (int) Key) = 1 );
}
Public static void SetState (VirtualKeys Key, bool State)
{
If (State! = GetState (Key ))
{
Keybd_event (byte) Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );
Keybd_event (byte) Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
}
}

// Enable uppercase keyboard
Private void btnOpenCAPITAL_Click (object sender, EventArgs e)
{
SetState (VirtualKeys. VK_CAPITAL, true );
}

// Close the upper case of the keyboard
Private void btnCloseCAPITAL_Click (object sender, EventArgs e)
{
SetState (VirtualKeys. VK_CAPITAL, false );
}

// Enable keyboard scroll lock
Private void btnOpenScroll_Click (object sender, EventArgs e)
{
SetState (VirtualKeys. VK_SCROLL, true );
}

// Disable keyboard scroll lock
Private void btnCloseScroll_Click (object sender, EventArgs e)
{
SetState (VirtualKeys. VK_SCROLL, false );
}

// Enable the keyboard number lock key
Private void btnOpenNum_Click (object sender, EventArgs e)
{
SetState (VirtualKeys. VK_NUMLOCK, true );
}

// Disable the keyboard number lock key
Private void btnCloseNum_Click (object sender, EventArgs e)
{
SetState (VirtualKeys. VK_NUMLOCK, false );
}

}
}

Related Article

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.