1. 程式人生 > >C# 判斷大小寫是否按下

C# 判斷大小寫是否按下

原文:https://blog.csdn.net/z397164725/article/details/7377815 

測試確實可以!

1.//滑鼠在UI移動才能獲取,不建議使用
        [DllImport("user32.dll", EntryPoint = "GetKeyboardState")]

        public static extern int GetKeyboardState(byte[] pbKeyState);

//大小寫狀態

         byte[] bs = new byte[256];
         GetKeyboardState(bs);
         if(bs[0x14] == 1);
 {
                MessageBox.Show("按下"); 
 }

2.
        [DllImport("USER32", SetLastError = true)]
        static extern short GetKeyState(int nVirtKey);

      //大小寫狀態
        if (GetKeyState(20) == 1)
            {
                MessageBox.Show("按下");
            }

3.
      //大小寫狀態
        if (IsKeyLocked(Keys.CapsLock))
            {
                MessageBox.Show("按下");

            }