C# 后台按键 视频播放器 全屏后无法 触发

我是研究僧i 提交于 2021-02-14 14:11:01
第一种  (全屏不可触发)    

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) { int WM_KEYDOWN = 256; int WM_SYSKEYDOWN = 260; if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN) { switch (keyData) { case Keys.Escape: DialogResult dr = MessageBox.Show("确定要退出吗?", "退出系统", MessageBoxButtons.OKCancel); if (dr == DialogResult.OK)//如果点击“确定”按钮 { FormPassword form = new FormPassword(); form.StartPosition = FormStartPosition.CenterScreen; form.ShowDialog(); // this.Close();//esc关闭窗体 //System.Environment.Exit(0); } break; } } return false; }

 //第二种方法   (全屏可触发)

private void timer2_Tick(object sender, EventArgs e)
        {
            //因为回车的ASCII码是13 所以我们这里就用把回车的ascii传递过去
            //需要监控什么按键就写某个按键的ASCII码
            if (GetAsyncKeyState(27) != 0)
            {
                ////MessageBox.Show("你按下了ESC");
                DialogResult dr = MessageBox.Show("确定要退出吗?", "退出系统", MessageBoxButtons.OKCancel);
                //if (dr == DialogResult.OK)//如果点击“确定”按钮
                //{
                //    //Form2 form = new Form2();
                //    //form.StartPosition = FormStartPosition.CenterScreen;
                //    //form.ShowDialog();
                //    // this.Close();//esc关闭窗体

                //    System.Environment.Exit(0);
                //}

                //this.Close();
            }
        }
        [DllImport("user32")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        private readonly int MOUSEEVENTF_MOVE = 0x0001;//模拟鼠标左键按下
        private readonly int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标左键抬起
        [DllImport("user32.dll")]
        //函数声明
        public static extern int GetAsyncKeyState(int vKey);

//第三种方法 视频播放器按键事件 (全屏不可触发)

private void axWindowsMediaPlayer1_KeyDownEvent_1(object sender, _WMPOCXEvents_KeyDownEvent e)
        {
            if (e.nKeyCode == (int)Keys.Escape) //32
            {
                //MessageBox.Show("test");
                this.Close();
            }
        }

 //第四种方法

HOOK,    只能触发几下 就失效了 不知道为啥 ,........

 

附:

VK_LBUTTON             鼠标左键                      0x01

VK_RBUTTON             鼠标右键                      0x02

VK_CANCEL              Ctrl + Break                  0x03

VK_MBUTTON             鼠标中键                      0x04

VK_BACK                Backspace 键       0x08

VK_TAB                 Tab 键                        0x09

VK_RETURN              回车键                        0x0D

VK_SHIFT               Shift 键                      0x10

VK_CONTROL             Ctrl 键                       0x11

VK_MENU                Alt 键                 0x12

VK_pause               Pause 键                      0x13

VK_CAPITAL             Caps Lock 键                  0x14

VK_ESCAPE              Esc 键                        0x1B

VK_SPACE               空格键         0x20

VK_PRIOR               Page Up 键                    0x21

VK_NEXT                Page Down 键                  0x22

VK_END                 End 键                        0x23

VK_HOME                Home 键                       0x24

VK_LEFT                左箭头键                      0x25

VK_UP                  上箭头键                      0x26

VK_RIGHT               右箭头键                      0x27

VK_DOWN                下箭头键                      0x28

VK_SNAPSHOT            print screen 键               0x2C

VK_Insert              Insert 键                     0x2D

VK_Delete              Delete 键                     0x2E

'0' – '9'             数字 0 - 9                    0x30 - 0x39

'A' – 'Z'             字母 A - Z                    0x41 - 0x5A

VK_LWIN                左WinKey(104键盘才有)         0x5B

VK_RWIN                右WinKey(104键盘才有)         0x5C

VK_APPS                AppsKey(104键盘才有)          0x5D

VK_NUMPAD0            小键盘 0 键                    0x60

VK_NUMPAD1            小键盘 1 键                    0x61

VK_NUMPAD2            小键盘 2 键                    0x62

VK_NUMPAD3            小键盘 3 键                    0x63

VK_NUMPAD4            小键盘 4 键                    0x64

VK_NUMPAD5            小键盘 5 键                    0x65

VK_NUMPAD6            小键盘 6 键                    0x66

VK_NUMPAD7            小键盘 7 键                    0x67

VK_NUMPAD8            小键盘 8 键                    0x68

VK_NUMPAD9            小键盘 9 键                    0x69

VK_F1 - VK_F24        功能键F1 – F24               0x70 - 0x87

VK_NUMLOCK            Num Lock 键                   0x90

VK_SCROLL             Scroll Lock 键                0x91

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!