Skip to content

Instantly share code, notes, and snippets.

@oleksabor
Last active January 2, 2020 11:03
Show Gist options
  • Save oleksabor/e7f92e30f347da9616fda175c4fd33a9 to your computer and use it in GitHub Desktop.
Save oleksabor/e7f92e30f347da9616fda175c4fd33a9 to your computer and use it in GitHub Desktop.
mapping virtual key codes
static void Main(string[] args)
{
uint[] keys = new uint[] { 56, 82, 79, 77, 72 };
var scanCodes = keys.Select(_ => MapVirtualKey(_, MapVirtualKeyMapTypes.MAPVK_VSC_TO_VK));
Console.WriteLine("mapped `{0}` to `{1}`", AsString(keys), AsString(scanCodes));
Console.WriteLine("done");
Console.ReadLine();
}
[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode, MapVirtualKeyMapTypes uMapType);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
public const int KEYEVENTF_EXTENDEDKEY = 0x01;
public const int KEYEVENTF_KEYUP = 0x02;
static string AsString(IEnumerable<uint> values)
{
return string.Join(",", values);
}
/// <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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment