Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

MapVirtualKey (user32)
 
.
Summary
Summary

C# Signature:

[DllImport("user32.dll")]
static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl);
static extern uint MapVirtualKey(uint uCode, uint uMapType);

or

OR the following using the VB comments taken from below

[DllImport("user32.dll")]
static extern uint MapVirtualKeyEx(uint uCode, MapVirtualKeyMapTypes uMapType, IntPtr dwhkl);
    /// <summary>
    /// The MapVirtualKey function translates (maps) a virtual-key code into a scan
    /// code or character value, or translates a scan code into a virtual-key code    
    /// </summary>
    /// <param name="uCode">[in] Specifies the virtual-key code or scan code for a key.
    /// How this value is interpreted depends on the value of the uMapType parameter
    /// </param>
    /// <param name="uMapType">[in] Specifies the translation to perform. The value of this
    /// parameter depends on the value of the uCode parameter.
    /// </param>
    /// <returns>Either a scan code, a virtual-key code, or a character value, depending on
    /// the value of uCode and uMapType. If there is no translation, the return value is zero
    /// </returns>
    [DllImport("user32.dll")]
    private static extern int MapVirtualKey(MapVirtualKeyMapTypes uCode, uint uMapType);

User-Defined Types:

C# User-Defined Types:

const uint MAPVK_VK_TO_VSC = 0x00;
const uint MAPVK_VSC_TO_VK = 0x01;
const uint MAPVK_VK_TO_CHAR = 0x02;
const uint MAPVK_VSC_TO_VK_EX = 0x03;
const uint MAPVK_VK_TO_VSC_EX = 0x04;

or

OR the following using the VB comments taken from below

/// <summary>
/// The set of valid MapTypes used in MapVirtualKey
/// </summary>
public enum MapVirtualKeyMapTypes : uint
{
     /// <summary>
     /// uCode is a virtual-key code and is translated into a scan code.
     /// If it is a virtual-key code that does not distinguish between left- and
     /// right-hand keys, the left-hand scan code is returned.
     /// If there is no translation, the function returns 0.
     /// </summary>
     MAPVK_VK_TO_VSC = 0x00,
    /// <summary>
    /// The set of valid MapTypes used in MapVirtualKey
    /// </summary>
    public enum MapVirtualKeyMapTypes : uint
    {
        /// <summary>
        /// uCode is a virtual-key code and is translated into a scan code.
        /// If it is a virtual-key code that does not distinguish between left- and
        /// right-hand keys, the left-hand scan code is returned.
        /// If there is no translation, the function returns 0.
        /// </summary>
        MAPVK_VK_TO_VSC = 0x00,

        /// <summary>
        /// uCode is a scan code and is translated into a virtual-key code that
        /// does not distinguish between left- and right-hand keys. If there is no
        /// translation, the function returns 0.
        /// </summary>
        MAPVK_VSC_TO_VK = 0x01,

        /// <summary>
        /// uCode is a virtual-key code and is translated into an unshifted
        /// character value in the low-order word of the return value. Dead keys (diacritics)
        /// are indicated by setting the top bit of the return value. If there is no
        /// translation, the function returns 0.
        /// </summary>
        MAPVK_VK_TO_CHAR = 0x02,

        /// <summary>
        /// Windows NT/2000/XP: uCode is a scan code and is translated into a
        /// virtual-key code that distinguishes between left- and right-hand keys. If
        /// there is no translation, the function returns 0.
        /// </summary>
        MAPVK_VSC_TO_VK_EX = 0x03,

        /// <summary>
        /// Not currently documented
        /// </summary>
        MAPVK_VK_TO_VSC_EX = 0x04
    }

VB Signature:

    ''' <summary>The MapVirtualKey function translates (maps) a virtual-key code into a scan
    ''' code or character value, or translates a scan code into a virtual-key code
    ''' </summary>
    ''' <param name="uCode">[in] Specifies the virtual-key code or scan code for a key.
    ''' How this value is interpreted depends on the value of the uMapType parameter</param>
    ''' <param name="uMapType">[in] Specifies the translation to perform. The value of this
    ''' parameter depends on the value of the uCode parameter.</param>
    ''' <returns>Either a scan code, a virtual-key code, or a character value, depending on
    ''' the value of uCode and uMapType. If there is no translation, the return value is zero</returns>
    ''' <remarks></remarks>
    <DllImport("User32.dll", SetLastError:=False, CallingConvention:=CallingConvention.StdCall, _
           CharSet:=CharSet.Auto)> _
    Public Shared Function MapVirtualKey(ByVal uCode As UInt32, ByVal uMapType As MapVirtualKeyMapTypes) As UInt32
    End Function

VB User-defined types:

    ''' <summary>The set of valid MapTypes used in MapVirtualKey
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum MapVirtualKeyMapTypes As UInt32
    ''' <summary>uCode is a virtual-key code and is translated into a scan code.
    ''' If it is a virtual-key code that does not distinguish between left- and
    ''' right-hand keys, the left-hand scan code is returned.
    ''' If there is no translation, the function returns 0.
    ''' </summary>
    ''' <remarks></remarks>
    MAPVK_VK_TO_VSC = &H0

    ''' <summary>uCode is a scan code and is translated into a virtual-key code that
    ''' does not distinguish between left- and right-hand keys. If there is no
    ''' translation, the function returns 0.
    ''' </summary>
    ''' <remarks></remarks>
    MAPVK_VSC_TO_VK = &H1

    ''' <summary>uCode is a virtual-key code and is translated into an unshifted
    ''' character value in the low-order word of the return value. Dead keys (diacritics)
    ''' are indicated by setting the top bit of the return value. If there is no
    ''' translation, the function returns 0.
    ''' </summary>
    ''' <remarks></remarks>
    MAPVK_VK_TO_CHAR = &H2

    ''' <summary>Windows NT/2000/XP: uCode is a scan code and is translated into a
    ''' virtual-key code that distinguishes between left- and right-hand keys. If
    ''' there is no translation, the function returns 0.
    ''' </summary>
    ''' <remarks></remarks>
    MAPVK_VSC_TO_VK_EX = &H3

    ''' <summary>Not currently documented
    ''' </summary>
    ''' <remarks></remarks>
    MAPVK_VK_TO_VSC_EX = &H4
    End Enum

VB notes:

You don't have to use the user-defined type - I use it as a convenience for remembering

those magic numbers.

Note that I have specified UInt32 as the types of the variables - this would need to be

changed for 64-bit code.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode, uint uMapType);

     /// <summary>
     /// uCode is a scan code and is translated into a virtual-key code that
     /// does not distinguish between left- and right-hand keys. If there is no
     /// translation, the function returns 0.
     /// </summary>
     MAPVK_VSC_TO_VK = 0x01,
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

     /// <summary>
     /// uCode is a virtual-key code and is translated into an unshifted
     /// character value in the low-order word of the return value. Dead keys (diacritics)
     /// are indicated by setting the top bit of the return value. If there is no
     /// translation, the function returns 0.
     /// </summary>
     MAPVK_VK_TO_CHAR = 0x02,
public const int KEYEVENTF_EXTENDEDKEY = 0x01;
public const int KEYEVENTF_KEYUP = 0x02;

     /// <summary>
     /// Windows NT/2000/XP: uCode is a scan code and is translated into a
     /// virtual-key code that distinguishes between left- and right-hand keys. If
     /// there is no translation, the function returns 0.
     /// </summary>
     MAPVK_VSC_TO_VK_EX = 0x03,
byte key = 0x41;
uint scanCode = MapVirtualKey((uint)key, 0);
keybd_event(key, (byte)scanCode, 0, 0);
keybd_event(key, (byte)scanCode, KEYEVENTF_KEYUP, 0);

     /// <summary>
     /// Not currently documented
     /// </summary>
     MAPVK_VK_TO_VSC_EX = 0x04
}

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
Documentation

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 PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions