User32.dll

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig

    #1

    User32.dll

    I'm trying to figure out the fastest/easiest way of sending text to another
    application. I've got the SendInput and keybd_event methods working well
    enough... is one better than the other? Is there something else I should try
    out? Is there a way to send text to another window without giving that
    window focus? Below is the code I'm using.

    Thanks in advance.
    *************** **************
    [DllImport("user 32.dll")]
    static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
    cbSize);

    [StructLayout(La youtKind.Explic it)]
    public struct INPUT
    {
    [FieldOffset(0)]
    public int type;
    [FieldOffset(4)]
    MOUSEINPUT mi;
    [FieldOffset(4)]
    public KEYBDINPUT ki;
    [FieldOffset(4)]
    HARDWAREINPUT hi;
    }

    struct MOUSEINPUT
    {
    int dx;
    int dy;
    int mouseData;
    int dwFlags;
    int time;
    IntPtr dwExtraInfo;
    }

    public struct KEYBDINPUT
    {
    public char wVk;
    public short wScan;
    public int dwFlags;
    public int time;
    public IntPtr dwExtraInfo;
    }

    struct HARDWAREINPUT
    {
    int uMsg;
    short wParamL;
    short wParamH;
    }

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

    [DllImport("user 32.dll")]
    static extern void keybd_event(byt e bVk, byte bScan, uint dwFlags,
    UIntPtr dwExtraInfo);

    [DllImport("user 32.dll", EntryPoint = "FindWindow ")]
    private static extern int FindWindow(stri ng _ClassName, string
    _WindowName);

    [DllImport("User 32.dll")]
    public static extern Int32 SetForegroundWi ndow(int hWnd);

    public const int KEYEVENTF_KEYUP = 0x2;

    static void Main(string[] args)
    {
    int i = FindWindow("Not epad", null);
    SetForegroundWi ndow(i);
    string str1 = "CRAIG";
    for (int j = 0; j < str1.Length-1; j++)
    {
    PressKey((char) str1[j]);
    }
    for (int j = 0; j < str1.Length; j++)
    {
    PressKey2(str2[j]);
    }
    }
    }
    }

    public static void PressKey2(char keyCode)
    {
    INPUT mInput = new INPUT();
    KEYBDINPUT ki = new KEYBDINPUT();
    mInput.type = 1;
    mInput.ki = ki;
    mInput.ki.wVk = keyCode;
    mInput.ki.dwFla gs = 0;
    unsafe
    {
    SendInput(1, ref mInput, sizeof(INPUT));
    }

    mInput.type = 1;
    mInput.ki.wVk = keyCode;
    mInput.ki.dwFla gs = KEYEVENTF_KEYUP ;
    unsafe
    {
    SendInput(1, ref mInput, sizeof(INPUT));
    }
    }

    public static void PressKey(char keyCode)
    {
    const int KEYEVENTF_KEYUP = 0x2;
    keybd_event((by te)VkKeyScan(ke yCode), 0x9e, (uint)0, (UIntPtr)0);
    keybd_event((by te)VkKeyScan(ke yCode), 0x9e,
    (uint)KEYEVENTF _KEYUP, (UIntPtr)0);
    }
  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: User32.dll

    Hi Craig,

    You can also use the SendMessage API to send messages like WM_CHAR or
    WM_KEYDOWN to the application. I am not sure though they will be correctly
    processed without the target window having input focus.

    --
    Sincerely,
    Dmitriy Lapshin [C# / .NET MVP]
    Bring the power of unit testing to the VS .NET IDE today!


    "Craig" <Craig@discussi ons.microsoft.c om> wrote in message
    news:6132F5B2-C464-479C-B029-EFDF136515FA@mi crosoft.com...[color=blue]
    > I'm trying to figure out the fastest/easiest way of sending text to[/color]
    another[color=blue]
    > application. I've got the SendInput and keybd_event methods working well
    > enough... is one better than the other? Is there something else I should[/color]
    try[color=blue]
    > out? Is there a way to send text to another window without giving that
    > window focus? Below is the code I'm using.
    >
    > Thanks in advance.
    > *************** **************
    > [DllImport("user 32.dll")]
    > static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
    > cbSize);
    >
    > [StructLayout(La youtKind.Explic it)]
    > public struct INPUT
    > {
    > [FieldOffset(0)]
    > public int type;
    > [FieldOffset(4)]
    > MOUSEINPUT mi;
    > [FieldOffset(4)]
    > public KEYBDINPUT ki;
    > [FieldOffset(4)]
    > HARDWAREINPUT hi;
    > }
    >
    > struct MOUSEINPUT
    > {
    > int dx;
    > int dy;
    > int mouseData;
    > int dwFlags;
    > int time;
    > IntPtr dwExtraInfo;
    > }
    >
    > public struct KEYBDINPUT
    > {
    > public char wVk;
    > public short wScan;
    > public int dwFlags;
    > public int time;
    > public IntPtr dwExtraInfo;
    > }
    >
    > struct HARDWAREINPUT
    > {
    > int uMsg;
    > short wParamL;
    > short wParamH;
    > }
    >
    > [DllImport("user 32.dll")]
    > static extern short VkKeyScan(char ch);
    >
    > [DllImport("user 32.dll")]
    > static extern void keybd_event(byt e bVk, byte bScan, uint dwFlags,
    > UIntPtr dwExtraInfo);
    >
    > [DllImport("user 32.dll", EntryPoint = "FindWindow ")]
    > private static extern int FindWindow(stri ng _ClassName, string
    > _WindowName);
    >
    > [DllImport("User 32.dll")]
    > public static extern Int32 SetForegroundWi ndow(int hWnd);
    >
    > public const int KEYEVENTF_KEYUP = 0x2;
    >
    > static void Main(string[] args)
    > {
    > int i = FindWindow("Not epad", null);
    > SetForegroundWi ndow(i);
    > string str1 = "CRAIG";
    > for (int j = 0; j < str1.Length-1; j++)
    > {
    > PressKey((char) str1[j]);
    > }
    > for (int j = 0; j < str1.Length; j++)
    > {
    > PressKey2(str2[j]);
    > }
    > }
    > }
    > }
    >
    > public static void PressKey2(char keyCode)
    > {
    > INPUT mInput = new INPUT();
    > KEYBDINPUT ki = new KEYBDINPUT();
    > mInput.type = 1;
    > mInput.ki = ki;
    > mInput.ki.wVk = keyCode;
    > mInput.ki.dwFla gs = 0;
    > unsafe
    > {
    > SendInput(1, ref mInput, sizeof(INPUT));
    > }
    >
    > mInput.type = 1;
    > mInput.ki.wVk = keyCode;
    > mInput.ki.dwFla gs = KEYEVENTF_KEYUP ;
    > unsafe
    > {
    > SendInput(1, ref mInput, sizeof(INPUT));
    > }
    > }
    >
    > public static void PressKey(char keyCode)
    > {
    > const int KEYEVENTF_KEYUP = 0x2;
    > keybd_event((by te)VkKeyScan(ke yCode), 0x9e, (uint)0,[/color]
    (UIntPtr)0);[color=blue]
    > keybd_event((by te)VkKeyScan(ke yCode), 0x9e,
    > (uint)KEYEVENTF _KEYUP, (UIntPtr)0);
    > }[/color]

    Comment

    Working...