Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
1` DF
Helpful Tools
Suggested Reading
Website TODO List
Download Visual Studio Add-In

VkKeyScan (user32)

C# Signature:

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern short VkKeyScanEx(char ch, IntPtr dwhkl);

VB.Net Signature:

<DllImport("user32.dll", CharSet := CharSet.Unicode)> _
Private Shared Function VkKeyScanEx(ch As Char, dwhkl As IntPtr) As Short
End Function

User-Defined Types:



The CharSet.Unicode is needed on the DllImport declaration for the marshaler to correctly marshal unicode chars

Tips & Tricks:

Please add some!

Sample Code:

A sample program to explain usage (from:

    namespace KeyFinder
      class Program
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    static extern short VkKeyScanEx(char ch, IntPtr dwhkl);
    static extern bool UnloadKeyboardLayout(IntPtr hkl);
    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)
      public void Dispose()
      // 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)))
        using(var keyboard = new KeyboardPointer(culture))
          foreach(char test in testChars)
        if(keyboard.GetKey(test, out key))
          Console.WriteLine("No Key");
      Console.Read();//Stop window closing


    A  Shift: A
    é  No Key
    ש  A
    A  Shift: A
    é  No Key
    ש  No Key
    A  Shift: A
    é  Control: Alt: E
    ש  No Key

(Though your own console might mess up ש and/or é depending on settings and fonts).

Alternative Managed API:

Do you know one? Please contribute it!

VkKeyScanEx on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version