2015-08-29 66 views
0

你好堆棧溢出社區!按下Shift鍵時可以替換字符嗎?

這是我的函數來捕獲每一個按鍵:我使用這個功能,取代了我的字符與新

public static string GetBuffKeys() 
     { 
      string buffer = ""; 
      foreach (System.Int32 i in Enum.GetValues(typeof(Keys))) 
      { 
       if (GetAsyncKeyState(i) == -32767) 
        buffer += Enum.GetName(typeof(Keys), i); 
      } 
      return buffer; 
     } 

有一點格式化:

public static class KeyControl 
    { 
     [DllImport("User32.dll")] 
     private static extern short GetAsyncKeyState(int vKey); 

     public static string ReplaceChars(string text) 
     { 
      text = text.Replace("Space", " "); 
      text = text.Replace("Delete", "<Del>"); 
      text = text.Replace("LShiftKey", ""); 
      text = text.Replace("ShiftKey", ""); 
      text = text.Replace("OemQuotes", "!"); 
      text = text.Replace("Oemcomma", "?"); 
      text = text.Replace("D8", "á"); 
      text = text.Replace("D2", "ě"); 
      text = text.Replace("D3", "š"); 
      text = text.Replace("D4", "č"); 
      text = text.Replace("D5", "ř"); 
      text = text.Replace("D6", "ž"); 
      text = text.Replace("D7", "ý"); 
      text = text.Replace("D9", "í"); 
      text = text.Replace("D0", "é"); 
      text = text.Replace("D1", "+"); 
      text = text.Replace("Back", "<=="); 
      text = text.Replace("LButton", ""); 
      text = text.Replace("RButton", ""); 
      text = text.Replace("NumPad", ""); 
      text = text.Replace("OemPeriod", "."); 
      text = text.Replace("OemSemicolon", ","); 
      text = text.Replace("Oem4", "/"); 
      text = text.Replace("LControlKey", ""); 
      text = text.Replace("ControlKey", ""); 
      text = text.Replace("Enter", "<ENT>"); 
      text = text.Replace("Shift", ""); 
      text = text.Replace("CapsLock", ""); 
      text = text.Replace("Oem6", "("); 
      return text; 
     } 

但我想更換D *(例如D1)帶數字,如果按下Shift鍵。有可能的?如果沒有,關鍵日誌記錄比緩衝所有按下的鍵更好的方法是什麼?非常感謝!

+1

是從['System.Windows.Input.KeyEventArgs.Key'](https://msdn.microsoft該文本。 com./en/us/library/system.windows.input.keyeventargs.key%28v = vs.110%29.aspx)in ['System.Windows.Input.KeyEventArgs'](https://msdn.microsoft.com /en-us/library/System.Windows.Input.KeyEventArgs%28v=vs.110%29.aspx)(WPF)或從['System.Windows.Forms.Keys'](https://msdn.microsoft。 com./en/us/library/system.windows.forms.keys.aspx)在['System.Windows.Forms.KeyEventArgs'](https://msdn.microsoft.com/en-us/library/System.Windows .Forms.KeyEventArgs%28v = vs.110%29.aspx)(winforms)? – dbc

+0

@dbc不,我在man線程中添加了我的函數,請檢查它。 – PepinCZ

+0

您可以使用['Keyboard.Modifiers'](https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.modifiers%28v=vs.110%29.aspx)來查看當前按下了哪些修飾鍵。那是你要的嗎? – dbc

回答

0

您可以使用Control.ModifierKeys(WinForms)或Keyboard.Modifiers(WPF)查看當前按下的修飾鍵。例如:

 var modifiers = Control.ModifierKeys; 
     if (modifiers != Keys.None) 
     { 
      text = text = "[" + modifiers.ToString() + "]"; 
     } 

請注意,Keyboard.Modifiers是一個位掩碼字段。因此,下面的代碼清除特定的改性劑時,他們當前的按鍵事件對應於改性劑:

public static string GetBuffKeys() 
    { 
     var allModifierKeys = new Dictionary<Keys, Keys> { { Keys.ControlKey, Keys.Control }, {Keys.ShiftKey, Keys.Shift}, {Keys.Menu, Keys.Alt }}; 
     var modifiers = Control.ModifierKeys; 

     string buffer = ""; 
     foreach (var key in Enum.GetValues(typeof(Keys)).OfType<Keys>().Distinct()) // The enum seems to have duplicated values for Enter and Return. Uniquify them. 
     { 
      if (GetAsyncKeyState((int)key) == -32767) 
      { 
       buffer += ReplaceChars(Enum.GetName(typeof(Keys), key)); 
       if (allModifierKeys.Keys.Contains(key)) 
        modifiers &= ~(allModifierKeys[key]); // Remove this modifier to prevent double printing. 
      } 
     } 
     if (modifiers != Keys.None) 
     { 
      buffer = buffer + " [" + modifiers.ToString() + "]"; 
     } 
     return buffer; 
    } 
+0

謝謝,我現在試試吧!但我有一個小問題:對於Shift鍵組合使用Keys.Shift或Keys.ShiftKey? – PepinCZ

+0

@PepinCZ - 字典allModifierKeys給出了從修飾鍵到修飾符的映射。 – dbc

+0

男人但是...當我用shift和altgr打字時,他得到了他的結果:: – PepinCZ

相關問題