C#中窗体屏蔽键盘按键

重写Wndproc示例:

static bool flag = false;
        protected override void WndProc(ref Message m)
        {
           switch (m.Msg) {
                 case 0x0200: //WM_MOUSEMOVE
                         if (flag)
                         {
                             PostMessage(this.Handle, 0x00A1,new IntPtr(2),m.LParam);//这里需要导入 user32.dll 
                         }
                         break;
                     case 0x201://WM_LBUTTONDOWN
                         flag = true;
                         break;
                     case 0x202://WM_LBUTTONUP
                        flag = false;

                               break;

                default:
                    base.WndProc(ref m);
                    break;
            }

      [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);





虚拟键盘代码                        16进制值                          对应键   
VK_LBUTTON                     01                                    鼠标左键   
VK_RBUTTON                    02                                   鼠标右键   
VK_CANCEL                       03                                  用于执行Ctrl+C或Ctrl+Break   
VK_MBUTTON                     04                                   鼠标中键   
VK_BACK                             08                                   Backspace键   
VK_TAB                             09                                    Tab键   
VK_CLEAR                         0C                                    Clear键   
VK_RETURN                     0D                                    Enter键   
VK_SHIFT                           10                                    Shift键   
VK_CONTROL                   11                                   Ctrl键   
VK_MENU                         12                                     Arl键   
VK_PAUSE                       13                                    Pause键   
VK_CAPITAL                     14                                   Capslock键   
VK_ESCAPE                     1B                                   Ese键   
VK_SPACE                        20                                    SpaceBar键   
VK_PRIOR                       21                                  PgUp键   
VK_NEXT                          22                                    PgDn键   
VK_END                           23                                      End键   
VK_HOME                        24                                     Home键   
VK_LEFT                          25                                     Left   Arrow键   
VK_UP                             26                                      Up   Arrow键   
VK_RIGHT                       27                                     Right   Arrow键   
VK_DOWN                     28                                      Down   Arrow键   
VK_SELECT                   29                                       Select键   
VK_EXECUTE                  2B                                      Execute键   
VK_SNAPSHOT             2C                                      PrintScreen键   
VK_INSERT                   2D               Ins键   
VK_DELECT                   2E Del键   
VK_HELP 2F Help键   
VK_0      30 0键   
… … …   
VK_9           39 9键   
VK_A         41 A键   
… … …   
VK_Z           5A Z键   
VK_NUMAPD0       60 数字板0键   
… … …   
VK_NUMAPD9       69 数字板9键   
VK_MULTIPY        6A 乘号键   
VK_ADD                6B 加号键   
VK_SEPARATOR 6C Separator键   
VK_SUBSTRACT 6D 减号键   
VK_DECIMAL        6E 小数点键   
VK_DIVIDE                   6F 除号键   
VK_F1                70 F1键   
… … …   
VK_F24            87 F24键   
VK_NUMLOCK 90 NumLock键   
VK_SCROLL                   91 ScrollLock键

VK_CAPITAL       Caps   Lock   
VK_ESCAPE         Esc   
VK_SPACE           Space   
VK_PRIOR           Page   Up   
VK_NEXT             Page   Down   
VK_END               End   
VK_HOME             Home   
VK_LEFT             向左方向键   
VK_UP   
VK_RIGHT   
VK_DOWN   
VK_DELETE         Delete   
VK_INSERT         Inser   
VK_NUMPAD0~VK_NUMPAD9           小键盘上的0-9键   
VK_F1-VK_f12                             F1-F12键

检测Caps Lock 键:

var
KeyStates:TKeyboardState;

begin

   GetKeyboardState(KeyStates);
if Odd(KeyStates[VK_CAPITAL]) then
    spBtnA.Caption := 'a'
else
    spBtnA.Caption := 'A';

end;


你可能感兴趣的:(C#中窗体屏蔽键盘按键)