none
在WPF中的PreviewKeyDown事件中的keyCode问题 RRS feed

  • 问题

  • 以前程序是winform的.在winform中的KeyDown事件有e.KeyValue 可以获得对应的键值,

    比如:keycode    66 = b B
    keycode    67 = c C

    我想问下,在wpf中有没有相应的方法.获取键值的.

    谢谢各位大大了.

    2011年1月24日 8:10

答案

  • 你好 wpf001,

    我想你自己已经解决的你的问题, 我觉你可以标记你的问题为已解决,这样对论坛上其他有同样问题的成员通过你这个帖子找到答案或者有帮助的回复是很有利的。

    对于你的这个问题:

    难道中间是不是有转换的方法”,很遗憾的告诉你,据我所知,这个是没有的,因为Winform的keys和WPF的keys是不同的,这个你已经知道了,但是,我以前用过一个方法可以转换大部分的Key,你可以尝试一看,看看能否满足你的需求:

     

    public static System.Windows.Input.Key WinformsToWPFKey(System.Windows.Forms.Keys inputKey) 

            { 

                // Put special case logic here if there's a key you need but doesn't map... 

                try

                { 

                    return (System.Windows.Input.Key)Enum.Parse(typeof(System.Windows.Input.Key), inputKey.ToString()); 

                } 

                catch

                { 

                    // There wasn't a direct mapping...   

                    return System.Windows.Input.Key.None; 

                } 

            }

     

    然后我还有一个方法你可以考虑,就是KeyInterop这个静态类,你可以参考这个链接:

    http://msdn.microsoft.com/zh-cn/library/system.windows.input.keyinterop.aspx

    在这个类里面有两个方法,KeyFromVirtualKey 和 VirtualKeyFromKey,你也可以考虑一下:

    http://msdn.microsoft.com/zh-cn/library/system.windows.input.keyinterop_methods.aspx

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 wpf001 2011年1月25日 4:35
    2011年1月25日 4:07
    版主

全部回复

  • 使用WinForm的KeysConverter把WPF的Key枚举转换成WinForm的Keys枚举,这样就可以获得KeyCode了

    注:要添加System.Windows.Forms引用

    void Window_KeyDown(object sender, KeyEventArgs e)
    {
      Content = new System.Windows.Forms.KeysConverter().ConvertFromString(e.Key.ToString());
    }

    2011年1月24日 8:41
  • 谢谢.

    不想在去引用Forms的Dll, 现在我自己写一个keys的枚举,枚举出现在所有键盘按键对应的值, (keys枚举来自System.Windows.Forms.PreviewKeyDownEventArgs
    )应该很全.

    现在在想办法套进去.

    帖子继续留在,求解更好的答案.

     

    2011年1月24日 8:58
  • 对ms做法糊涂了. wpf的key也是用枚举,但是枚举的值和winform 不一致.

    大家也可以去看一下. System.Windows.Input.Key

    winform的keys

    public enum Keys
        {
          A = 0x41,
          Add = 0x6b,
          Alt = 0x40000,
          Apps = 0x5d,
          Attn = 0xf6,
          B = 0x42,
          Back = 8,
          BrowserBack = 0xa6,
          BrowserFavorites = 0xab,
          BrowserForward = 0xa7,
          BrowserHome = 0xac,
          BrowserRefresh = 0xa8,
          BrowserSearch = 170,
          BrowserStop = 0xa9,
          C = 0x43,
          Cancel = 3,
          Capital = 20,
          CapsLock = 20,
          Clear = 12,
          Control = 0x20000,
          ControlKey = 0x11,
          Crsel = 0xf7,
          D = 0x44,
          D0 = 0x30,
          D1 = 0x31,
          D2 = 50,
          D3 = 0x33,
          D4 = 0x34,
          D5 = 0x35,
          D6 = 0x36,
          D7 = 0x37,
          D8 = 0x38,
          D9 = 0x39,
          Decimal = 110,
          Delete = 0x2e,
          Divide = 0x6f,
          Down = 40,
          E = 0x45,
          End = 0x23,
          Enter = 13,
          EraseEof = 0xf9,
          Escape = 0x1b,
          Execute = 0x2b,
          Exsel = 0xf8,
          F = 70,
          F1 = 0x70,
          F10 = 0x79,
          F11 = 0x7a,
          F12 = 0x7b,
          F13 = 0x7c,
          F14 = 0x7d,
          F15 = 0x7e,
          F16 = 0x7f,
          F17 = 0x80,
          F18 = 0x81,
          F19 = 130,
          F2 = 0x71,
          F20 = 0x83,
          F21 = 0x84,
          F22 = 0x85,
          F23 = 0x86,
          F24 = 0x87,
          F3 = 0x72,
          F4 = 0x73,
          F5 = 0x74,
          F6 = 0x75,
          F7 = 0x76,
          F8 = 0x77,
          F9 = 120,
          FinalMode = 0x18,
          G = 0x47,
          H = 0x48,
          HanguelMode = 0x15,
          HangulMode = 0x15,
          HanjaMode = 0x19,
          Help = 0x2f,
          Home = 0x24,
          I = 0x49,
          IMEAccept = 30,
          IMEAceept = 30,
          IMEConvert = 0x1c,
          IMEModeChange = 0x1f,
          IMENonconvert = 0x1d,
          Insert = 0x2d,
          J = 0x4a,
          JunjaMode = 0x17,
          K = 0x4b,
          KanaMode = 0x15,
          KanjiMode = 0x19,
          KeyCode = 0xffff,
          L = 0x4c,
          LaunchApplication1 = 0xb6,
          LaunchApplication2 = 0xb7,
          LaunchMail = 180,
          LButton = 1,
          LControlKey = 0xa2,
          Left = 0x25,
          LineFeed = 10,
          LMenu = 0xa4,
          LShiftKey = 160,
          LWin = 0x5b,
          M = 0x4d,
          MButton = 4,
          MediaNextTrack = 0xb0,
          MediaPlayPause = 0xb3,
          MediaPreviousTrack = 0xb1,
          MediaStop = 0xb2,
          Menu = 0x12,
          Modifiers = -65536,
          Multiply = 0x6a,
          N = 0x4e,
          Next = 0x22,
          NoName = 0xfc,
          None = 0,
          NumLock = 0x90,
          NumPad0 = 0x60,
          NumPad1 = 0x61,
          NumPad2 = 0x62,
          NumPad3 = 0x63,
          NumPad4 = 100,
          NumPad5 = 0x65,
          NumPad6 = 0x66,
          NumPad7 = 0x67,
          NumPad8 = 0x68,
          NumPad9 = 0x69,
          O = 0x4f,
          Oem1 = 0xba,
          Oem102 = 0xe2,
          Oem2 = 0xbf,
          Oem3 = 0xc0,
          Oem4 = 0xdb,
          Oem5 = 220,
          Oem6 = 0xdd,
          Oem7 = 0xde,
          Oem8 = 0xdf,
          OemBackslash = 0xe2,
          OemClear = 0xfe,
          OemCloseBrackets = 0xdd,
          Oemcomma = 0xbc,
          OemMinus = 0xbd,
          OemOpenBrackets = 0xdb,
          OemPeriod = 190,
          OemPipe = 220,
          Oemplus = 0xbb,
          OemQuestion = 0xbf,
          OemQuotes = 0xde,
          OemSemicolon = 0xba,
          Oemtilde = 0xc0,
          P = 80,
          Pa1 = 0xfd,
          Packet = 0xe7,
          PageDown = 0x22,
          PageUp = 0x21,
          Pause = 0x13,
          Play = 250,
          Print = 0x2a,
          PrintScreen = 0x2c,
          Prior = 0x21,
          ProcessKey = 0xe5,
          Q = 0x51,
          R = 0x52,
          RButton = 2,
          RControlKey = 0xa3,
          Return = 13,
          Right = 0x27,
          RMenu = 0xa5,
          RShiftKey = 0xa1,
          RWin = 0x5c,
          S = 0x53,
          Scroll = 0x91,
          Select = 0x29,
          SelectMedia = 0xb5,
          Separator = 0x6c,
          Shift = 0x10000,
          ShiftKey = 0x10,
          Sleep = 0x5f,
          Snapshot = 0x2c,
          Space = 0x20,
          Subtract = 0x6d,
          T = 0x54,
          Tab = 9,
          U = 0x55,
          Up = 0x26,
          V = 0x56,
          VolumeDown = 0xae,
          VolumeMute = 0xad,
          VolumeUp = 0xaf,
          W = 0x57,
          X = 0x58,
          XButton1 = 5,
          XButton2 = 6,
          Y = 0x59,
          Z = 90,
          Zoom = 0xfb
        }
    

    wpf的key

    public enum Key
    {
      A = 0x2c,
      AbntC1 = 0x93,
      AbntC2 = 0x94,
      Add = 0x55,
      Apps = 0x48,
      Attn = 0xa3,
      B = 0x2d,
      Back = 2,
      BrowserBack = 0x7a,
      BrowserFavorites = 0x7f,
      BrowserForward = 0x7b,
      BrowserHome = 0x80,
      BrowserRefresh = 0x7c,
      BrowserSearch = 0x7e,
      BrowserStop = 0x7d,
      C = 0x2e,
      Cancel = 1,
      Capital = 8,
      CapsLock = 8,
      Clear = 5,
      CrSel = 0xa4,
      D = 0x2f,
      D0 = 0x22,
      D1 = 0x23,
      D2 = 0x24,
      D3 = 0x25,
      D4 = 0x26,
      D5 = 0x27,
      D6 = 40,
      D7 = 0x29,
      D8 = 0x2a,
      D9 = 0x2b,
      DbeAlphanumeric = 0x9d,
      DbeCodeInput = 0xa7,
      DbeDbcsChar = 0xa1,
      DbeDetermineString = 0xa9,
      DbeEnterDialogConversionMode = 170,
      DbeEnterImeConfigureMode = 0xa5,
      DbeEnterWordRegisterMode = 0xa4,
      DbeFlushString = 0xa6,
      DbeHiragana = 0x9f,
      DbeKatakana = 0x9e,
      DbeNoCodeInput = 0xa8,
      DbeNoRoman = 0xa3,
      DbeRoman = 0xa2,
      DbeSbcsChar = 160,
      DeadCharProcessed = 0xac,
      Decimal = 0x58,
      Delete = 0x20,
      Divide = 0x59,
      Down = 0x1a,
      E = 0x30,
      End = 0x15,
      Enter = 6,
      EraseEof = 0xa6,
      Escape = 13,
      Execute = 0x1d,
      ExSel = 0xa5,
      F = 0x31,
      F1 = 90,
      F10 = 0x63,
      F11 = 100,
      F12 = 0x65,
      F13 = 0x66,
      F14 = 0x67,
      F15 = 0x68,
      F16 = 0x69,
      F17 = 0x6a,
      F18 = 0x6b,
      F19 = 0x6c,
      F2 = 0x5b,
      F20 = 0x6d,
      F21 = 110,
      F22 = 0x6f,
      F23 = 0x70,
      F24 = 0x71,
      F3 = 0x5c,
      F4 = 0x5d,
      F5 = 0x5e,
      F6 = 0x5f,
      F7 = 0x60,
      F8 = 0x61,
      F9 = 0x62,
      FinalMode = 11,
      G = 50,
      H = 0x33,
      HangulMode = 9,
      HanjaMode = 12,
      Help = 0x21,
      Home = 0x16,
      I = 0x34,
      ImeAccept = 0x10,
      ImeConvert = 14,
      ImeModeChange = 0x11,
      ImeNonConvert = 15,
      ImeProcessed = 0x9b,
      Insert = 0x1f,
      J = 0x35,
      JunjaMode = 10,
      K = 0x36,
      KanaMode = 9,
      KanjiMode = 12,
      L = 0x37,
      LaunchApplication1 = 0x8a,
      LaunchApplication2 = 0x8b,
      LaunchMail = 0x88,
      Left = 0x17,
      LeftAlt = 120,
      LeftCtrl = 0x76,
      LeftShift = 0x74,
      LineFeed = 4,
      LWin = 70,
      M = 0x38,
      MediaNextTrack = 0x84,
      MediaPlayPause = 0x87,
      MediaPreviousTrack = 0x85,
      MediaStop = 0x86,
      Multiply = 0x54,
      N = 0x39,
      Next = 20,
      NoName = 0xa9,
      None = 0,
      NumLock = 0x72,
      NumPad0 = 0x4a,
      NumPad1 = 0x4b,
      NumPad2 = 0x4c,
      NumPad3 = 0x4d,
      NumPad4 = 0x4e,
      NumPad5 = 0x4f,
      NumPad6 = 80,
      NumPad7 = 0x51,
      NumPad8 = 0x52,
      NumPad9 = 0x53,
      O = 0x3a,
      Oem1 = 140,
      Oem102 = 0x9a,
      Oem2 = 0x91,
      Oem3 = 0x92,
      Oem4 = 0x95,
      Oem5 = 150,
      Oem6 = 0x97,
      Oem7 = 0x98,
      Oem8 = 0x99,
      OemAttn = 0x9d,
      OemAuto = 160,
      OemBackslash = 0x9a,
      OemBackTab = 0xa2,
      OemClear = 0xab,
      OemCloseBrackets = 0x97,
      OemComma = 0x8e,
      OemCopy = 0x9f,
      OemEnlw = 0xa1,
      OemFinish = 0x9e,
      OemMinus = 0x8f,
      OemOpenBrackets = 0x95,
      OemPeriod = 0x90,
      OemPipe = 150,
      OemPlus = 0x8d,
      OemQuestion = 0x91,
      OemQuotes = 0x98,
      OemSemicolon = 140,
      OemTilde = 0x92,
      P = 0x3b,
      Pa1 = 170,
      PageDown = 20,
      PageUp = 0x13,
      Pause = 7,
      Play = 0xa7,
      Print = 0x1c,
      PrintScreen = 30,
      Prior = 0x13,
      Q = 60,
      R = 0x3d,
      Return = 6,
      Right = 0x19,
      RightAlt = 0x79,
      RightCtrl = 0x77,
      RightShift = 0x75,
      RWin = 0x47,
      S = 0x3e,
      Scroll = 0x73,
      Select = 0x1b,
      SelectMedia = 0x89,
      Separator = 0x56,
      Sleep = 0x49,
      Snapshot = 30,
      Space = 0x12,
      Subtract = 0x57,
      System = 0x9c,
      T = 0x3f,
      Tab = 3,
      U = 0x40,
      Up = 0x18,
      V = 0x41,
      VolumeDown = 130,
      VolumeMute = 0x81,
      VolumeUp = 0x83,
      W = 0x42,
      X = 0x43,
      Y = 0x44,
      Z = 0x45,
      Zoom = 0xa8
    }
    
    

     

    难道中间是不是有转换的方法.

    希望大大告诉下.

    2011年1月24日 9:58
  • 你好 wpf001,

    我想你自己已经解决的你的问题, 我觉你可以标记你的问题为已解决,这样对论坛上其他有同样问题的成员通过你这个帖子找到答案或者有帮助的回复是很有利的。

    对于你的这个问题:

    难道中间是不是有转换的方法”,很遗憾的告诉你,据我所知,这个是没有的,因为Winform的keys和WPF的keys是不同的,这个你已经知道了,但是,我以前用过一个方法可以转换大部分的Key,你可以尝试一看,看看能否满足你的需求:

     

    public static System.Windows.Input.Key WinformsToWPFKey(System.Windows.Forms.Keys inputKey) 

            { 

                // Put special case logic here if there's a key you need but doesn't map... 

                try

                { 

                    return (System.Windows.Input.Key)Enum.Parse(typeof(System.Windows.Input.Key), inputKey.ToString()); 

                } 

                catch

                { 

                    // There wasn't a direct mapping...   

                    return System.Windows.Input.Key.None; 

                } 

            }

     

    然后我还有一个方法你可以考虑,就是KeyInterop这个静态类,你可以参考这个链接:

    http://msdn.microsoft.com/zh-cn/library/system.windows.input.keyinterop.aspx

    在这个类里面有两个方法,KeyFromVirtualKey 和 VirtualKeyFromKey,你也可以考虑一下:

    http://msdn.microsoft.com/zh-cn/library/system.windows.input.keyinterop_methods.aspx

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 wpf001 2011年1月25日 4:35
    2011年1月25日 4:07
    版主
  • 哦,谢谢版主,谢谢提供的链接.

    • 已编辑 wpf001 2011年1月25日 4:38 ps
    2011年1月25日 4:36