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

VkKeyScan (user32)
 
.
Summary

C# Signature:

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

User-Defined Types:

    [StructLayout( LayoutKind.Explicit )]
    public struct INPUT
    {
    [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:

None.

Tips & Tricks:

Please add some!

Sample Code:

    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 );
    short key = 0;

    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 );
        input.ki.wVk = (ushort) key;
        SendInput( 1, ref input, Marshal.SizeOf( input ) );
    }
    }

    [DllImport( "user32.dll" )]
    public static extern bool SetForegroundWindow (IntPtr hWnd);

    [DllImport( "user32.dll", SetLastError = true )]
    public static extern uint SendInput (uint nInputs, ref INPUT pInputs, int cbSize);
    public const int INPUT_KEYBOARD = 1;

    [DllImport( "user32.dll" )]
    public static extern IntPtr GetMessageExtraInfo ();

Alternative Managed API:

Do you know one? Please contribute it!

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