Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
namespace KeyFinder
[StructLayout( LayoutKind.Explicit )]
public struct INPUT
{
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;
}
[FieldOffset( 0 )]
public int type;
[FieldOffset( 4 )]
public KEYBDINPUT ki;
}
[StructLayout( LayoutKind.Sequential )]
public struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
Notes:
The charset is needed on the DllImport declaration for the marshaler to correctly marshal unicode chars
Tips & Tricks:
Please add some!
The VkKeyScanEx function allows to specify a locale.
This function allows you to map the contents of a string or a Textbox into keystrokes, as shown in the sample code
Sample Code:
This sample code shows how to read the text from a string and send it as keystrokes to Windows. It uses the SetForegroundWindow, SendInput, VkKeyScan functions.
void Main () {
string text = "abc123"; // text we will send
Process notepad = Process.Start( "notepad" ); // launch notepad, we'll write into it
Thread.Sleep( 2000 ); //give notepad some time to launch
SetForegroundWindow( notepad.MainWindowHandle );// bring that notepad to the foreground
short key = 0; //variable where we will hold the value of each key
// create an INPUT structure with default values
INPUT input = new INPUT();
input.type = INPUT_KEYBOARD;
input.ki = new KEYBDINPUT();
input.ki.dwExtraInfo = GetMessageExtraInfo();
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.wScan = 0;
foreach ( char c in text ) {
key = VkKeyScan( c ); //get the key for this character
input.ki.wVk = (ushort) key;//update the input structure
SendInput( 1, ref input, Marshal.SizeOf( input ) );//send the key to notepad
}
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
}
}
}
Output:
[DllImport( "user32.dll" )]
public static extern bool SetForegroundWindow (IntPtr hWnd);
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
[DllImport( "user32.dll", SetLastError = true )]
public static extern uint SendInput (uint nInputs, ref INPUT pInputs, int cbSize);
public const int INPUT_KEYBOARD = 1;
(Though your own console might mess up ש and/or é depending on settings and fonts).
[DllImport( "user32.dll" )]
public static extern IntPtr GetMessageExtraInfo ();
Alternative Managed API:
Do you know one? Please contribute it!
The VkKeyScanEx API
2/15/2016 10:31:02 PM - -93.117.19.8
The VkKeyScanEx API
2/15/2016 10:34:54 PM - -82.103.64.61
Click to read this page
4/6/2008 7:23:14 AM - anonymous
TODO - a short description
3/16/2007 8:02:38 AM - -89.49.254.174
TODO - a short description
3/16/2007 8:02:38 AM - -89.49.254.174
The VkKeyScanEx API
2/15/2016 10:31:02 PM - -93.117.19.8
The VkKeyScanEx API
2/15/2016 10:34:54 PM - -82.103.64.61
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).