复制 保存
private void Form1_Load(object sender, EventArgs e){ const int KEYEVENTF_EXTENDEDKEY = 0x1; const int KEYEVENTF_KEYUP = 0x2; keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0); keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr) 0);}[DllImport("user32.dll")]static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
复制 保存
// An umanaged function that retrieves the states of each key[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]public static extern short GetKeyState(int keyCode); bool CapsLock = (((ushort) GetKeyState(0x14)) & 0xffff) != 0;bool NumLock = (((ushort) GetKeyState(0x90)) & 0xffff) != 0;bool ScrollLock = (((ushort) GetKeyState(0x91)) & 0xffff) != 0;// Show the statusMessageBox.Show("Caps Lock is on: " + CapsLock.ToString());MessageBox.Show("Num Lock is on: " + NumLock.ToString());MessageBox.Show("Scroll Lock is on: " + ScrollLock.ToString());本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/09/07/1561801.html,如需转载请自行联系原作者