2009-02-13 9 views
15

मैं बहुत की तरह एक समारोह लिखना चाहते हैं,किसी वर्ण को समकक्ष System.Windows.Input.Key Enum मान में कैसे परिवर्तित करें?

 public System.Windows.Input.Key ResolveKey(char charToResolve) 
     { 
      // Code goes here, that resolves the charToResolve 
      // in to the Key enumerated value 
      // (For example with '.' as the character for Key.OemPeriod) 

     } 

मुझे लगता है मैं एक बहुत बड़ा स्विच-केस चरित्र मैच के लिए लिख सकते हैं पता है, लेकिन क्या कोई अन्य तरीका है? इस बात की बात यह है कि कुंजी enum की स्ट्रिंग चरित्र के साथ मेल नहीं खा सकती है, इसलिए Enum.IsDefined काम नहीं करेगा

कोई विचार?

अपडेट: यह विंडोज़ परिवेश में है

+0

पढ़ें पूर्ण विवरण के वातावरण क्या है? WinForms/asp.net? ऐसी चीज करने का मकसद क्या है? – shahkalpesh

उत्तर

23
[DllImport("user32.dll")] 
static extern short VkKeyScan(char ch); 

static public Key ResolveKey(char charToResolve) 
{ 
    return KeyInterop.KeyFromVirtualKey(VkKeyScan(charToResolve)); 
}
+1

'VkKeyScan()' संशोधक कुंजी राज्यों को एन्कोड करने के लिए उच्च बाइट का उपयोग करता है, इसलिए यह पूंजी अक्षरों में विफल रहता है क्योंकि 'VkKeyScan() 'शिफ्ट ध्वज में जोड़ता है। यदि आप और उन झंडे को साफ़ करने के लिए '0xFF' के साथ' VkKeyScan() 'के वापसी मूल्य को पूंजी अक्षरों के साथ काम करेंगे। – shf301

7

System.Windows.Input.KeyConverter वर्ग के ConvertFrom विधि का उपयोग कर प्रयास करें।

+0

'।' के साथ काम नहीं कर रहा है – Avram

+0

इसका उपयोग इस तरह से नहीं किया जाना है। प्रतिबिंबक में उस पर एक नज़र डालें और आप देखेंगे कि यह केवल अक्षरों, अंकों और कुछ अन्य कुंजी का समर्थन करता है। उल्लेखनीय अपवादों में शामिल हैं?, जो सही तरीके से परिवर्तित करने के लिए स्ट्रिंग "ओम क्वेस्टियन" होने की आवश्यकता होगी। – OwenP

1

हाय बस कन्वर्ट कि जिस तरह से

Dim KeyConverter As New Forms.KeysConverter  
Dim S As String = KeyConverter.ConvertToString(e.Key) 
Dim O As System.Windows.Forms.Keys = KeyConverter.ConvertFrom(S) 
Dim ChValue As Integer = CType(O, Integer) 

मेरे मामले में मैं अपने कीबोर्ड पर "Enter" दबाएं, OENTER {13} में जा रहा है और ChValue चरित्र संहिता में जा रहा है 13 TAB कुंजी मैं लूंगा लिए उदाहरण के लिए कैरेक्टर कोड 9 प्राप्त करें।

1

हाल ही में मैंने पाया एक महान answer for similar question from Jon Hanna जो नियंत्रण कुंजी राज्यों के साथ संभाल कर सकते हैं:

यह एक और अधिक आसानी से किसी और किसी भी चीज से एक उदाहरण के कार्यक्रम से समझाया जा सकता है:

namespace KeyFinder 
{ 
    class Program 
    { 
    [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
    static extern short VkKeyScanEx(char ch, IntPtr dwhkl); 
    [DllImport("user32.dll")] 
    static extern bool UnloadKeyboardLayout(IntPtr hkl); 
    [DllImport("user32.dll")] 
    static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags); 
    public class KeyboardPointer : IDisposable 
    { 
     private readonly IntPtr pointer; 
     public KeyboardPointer(int klid) 
     { 
     pointer = LoadKeyboardLayout(klid.ToString("X8"), 1); 
     } 
     public KeyboardPointer(CultureInfo culture) 
     :this(culture.KeyboardLayoutId){} 
     public void Dispose() 
     { 
     UnloadKeyboardLayout(pointer); 
     GC.SuppressFinalize(this); 
     } 
     ~KeyboardPointer() 
     { 
     UnloadKeyboardLayout(pointer); 
     } 
     // Converting to System.Windows.Forms.Key here, but 
     // some other enumerations for similar tasks have the same 
     // one-to-one mapping to the underlying Windows API values 
     public bool GetKey(char character, out Keys key) 
     { 
     short keyNumber = VkKeyScanEx(character, pointer); 
     if(keyNumber == -1) 
     { 
      key = System.Windows.Forms.Keys.None; 
      return false; 
     } 
     key = (System.Windows.Forms.Keys)(((keyNumber & 0xFF00) << 8) | (keyNumber & 0xFF)); 
     return true; 
     } 
    } 
    private static string DescribeKey(Keys key) 
    { 
     StringBuilder desc = new StringBuilder(); 
     if((key & Keys.Shift) != Keys.None) 
     desc.Append("Shift: "); 
     if((key & Keys.Control) != Keys.None) 
     desc.Append("Control: "); 
     if((key & Keys.Alt) != Keys.None) 
     desc.Append("Alt: "); 
     return desc.Append(key & Keys.KeyCode).ToString(); 
    } 
    public static void Main(string[] args) 
    { 
     string testChars = "Aéש"; 
     Keys key; 
     foreach(var culture in (new string[]{"he-IL", "en-US", "en-IE"}).Select(code => CultureInfo.GetCultureInfo(code))) 
     { 
     Console.WriteLine(culture.Name); 
     using(var keyboard = new KeyboardPointer(culture)) 
      foreach(char test in testChars) 
      { 
      Console.Write(test); 
      Console.Write('\t'); 
      if(keyboard.GetKey(test, out key)) 
       Console.WriteLine(DescribeKey(key)); 
      else 
       Console.WriteLine("No Key"); 
      } 
     } 
     Console.Read();//Stop window closing 
    } 
    } 
} 

आउटपुट:

he-IL 
A Shift: A 
é No Key 
ש A 
en-US 
A Shift: A 
é No Key 
ש No Key 
en-IE 
A Shift: A 
é Control: Alt: E 
ש No Key 

(हालांकि अपने खुद के सांत्वना हो सकता है गंदगी ש और/या सेटिंग्स और फोंट के आधार पर ई)।

से referenced answer

संबंधित मुद्दे