如题。因为我有些操作不是控件事件KeyPress可以判断得到的。就好像我在某控件下的
KeyPress(object ...., KeyPressEventArg e)
{
            if (e.KeyChar == '\r')//ENTER键,回车键:确定操作
            {
                MessageBox.Show("OK");//有反应
            }
            if (e.KeyChar == Convert.ToChar(27))//ESC键:取消操作
            {
                MessageBox.Show("Cancle");//无反应,所以我只想到用Windows的ESC键下按的消息来做处理好了。但怎么写呢。
            }
}

解决方案 »

  1.   

    It is better to use KeyDown event.
      

  2.   

    可以判断啊
    你用这个
    if (e.KeyValue == 27)
      

  3.   


    I had try it before you case...   Enter Key will tigger Both KeyDown and KeyPress the Event...
         
           But ,   ESC Key would not tigger these two events...   You can try it ... before you will leave this message...
      

  4.   


    KeyDown KeyPress都试过,就Enter键是可以的。ESC的按下,都不会触发这两个事件。
      

  5.   

     public class KeyHook
        {
            [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
            public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
            public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
            public static extern bool UnhookWindowsHookEx(int idHook);        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);        // Fields 
            private static int hKeyboardHook;
            private HookProc KeyboardHookProcedure;
            public const int WH_KEYBOARD_LL = 13;        // Methods 
            static KeyHook()
            {
                hKeyboardHook = 0;
            }        private int code = 0;        public KeyHook()
            {
                this.Start();        }        public KeyHook(int keyCode)
            {
                this.Start();
                this.code = keyCode;
            }        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
            {
                KeyboardHookStruct struct2 = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                KeyEventArgs args = new KeyEventArgs((Keys)struct2.vkCode);
                Console.WriteLine(args.KeyValue.ToString());
                //屏蔽
                //91,92 ==> win键 
                //93 ==> app键
                // 9 ==> tab 键
                //18 ==> alt 键
                if (args.KeyValue == code || args.KeyValue == 9 || args.KeyValue == 91 || args.KeyValue == 92 || args.KeyValue == 93)
                {
                    return 1;
                }
                return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
            }
            public void Hook()
            {
                this.Start();
            }
            public void UnHook() 
            { 
                this.Stop(); 
            }
            public void Start()
            {
                if (hKeyboardHook == 0)
                {
                    this.KeyboardHookProcedure = new HookProc(this.KeyboardHookProc);
                    Module m = Assembly.GetExecutingAssembly().GetModules()[0];
                    IntPtr p = Marshal.GetHINSTANCE(m);
                    hKeyboardHook = SetWindowsHookEx(13, this.KeyboardHookProcedure, p, 0);
                    if (hKeyboardHook == 0)
                    {
                        this.Stop();
                        Log.WriteErLog("KeyHook:SetWindowsHookEx ist failed.");
                        //throw new Exception("SetWindowsHookEx ist failed.");
                    }
                }
            }        public void Stop()
            {
                bool flag = true;
                if (hKeyboardHook != 0)
                {
                    flag = UnhookWindowsHookEx(hKeyboardHook);
                    hKeyboardHook = 0;
                }
                if (!flag)
                {
                    Log.WriteErLog("KeyHook:SetWindowsHookEx ist failed.");
                    //throw new Exception("UnhookWindowsHookEx failed.");
                }
            }
            ~KeyHook()
            {
                this.Stop();
            }
            [StructLayout(LayoutKind.Sequential)]
            public class KeyboardHookStruct
            {
                public int vkCode;
                public int scanCode;
                public int flags;
                public int time;
                public int dwExtraInfo;            public KeyboardHookStruct()
                {
                }
            }
        }
      

  6.   

    我没搞明白你们这是在干什么?用KeyDown可以啊。
     private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Escape)
                {
                    //do something;
                }
            }
    keys是个枚举类型,里面包含了所有的键值。
      

  7.   


     private void Form2_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode  == Keys.Enter )
                {
                }
                if (e.KeyCode == Keys.Escape)
                {
                }        }        private void Form2_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar ==(char )Keys.Enter )
                {
                }            if (e.KeyChar ==(char )Keys .Escape )
                {
                }        }
           这样应该可以了吧