VB's SendKeys and Remote Desktop

Posted on 2004-11-16
Last Modified: 2009-06-01
Why is it that I can´t send keystrokes with VB6 "SendKeys" to Windows XP Remote Desktop? Works fine with every other window and apps except Remote Desktop. Anyone has the same experience? Solutions anyone?

Question by:X_Ivar
    LVL 8

    Expert Comment

    Never dealt with this before, given your description, it sound's like a security measure MS built in. Have you tried the spy utility to make sure your key strokes are being sent to the correct window?


    Author Comment

    I use Appactivate so I can see that Remote Desktop is activated with Notepad on top before the SendKeys command is launched. I've also tried with differens kind of delays to be sure that for the window is activated before I use SendKeys. If I leave it like that and use the keyboard the characters will show in Notepad ok, but using VB's SendKeys just sends the characters to cyberspace. Sometimes I see that the Sendkeys command will disturb the caret's blink rate in Notepad on the other machine but that's all. Also if I use the Windows XP Screen keyboard it works. Why not the SendKeys command? I've searched for an api call to use instead and tried sendmessage but it doesn't work either.

    Expert Comment

    You can try this:
    1.use win32 API keyb_event, example:
    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Sub SendKeyStroke()
    keybd_event 27, 0, 0, 0
    keybd_event 27, 0, KEYEVENTF_KEYUP, 0
    End Sub

    this should send an escape stroke...
    just dont forget to call it again with the KEYEVENTF_KEYUP.

    2.use win32 API SendMessage. example:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Private Sub SendKeyStroke2()
    SendMessage hWnd,WM_KEYDOWN,27,0
    End Sub

    the hWnd you can get with FindWindow( it's not that complicated...

    hopefully the first one will work because he's the easiest one.

    Author Comment


    Your method 1 works (as VB's SendKeys) with other windows (like Notepad for example) but I can't make it work with XP's Remote Desktop. Do you? Also I changed 27 (Esc) to 65 (A) and that works just the same (although it sends lower case).

    Method 2: I can't get that working at all, that is not with any window (although I've only tested Notepad). I can find the window handle but neither 27 or 65 is sent to the window. Same thing for Remote Desktop.

    Furthermore I've realized that Remote Desktop is really a Terminal Services client - if that has something to do with anything.

    LVL 3

    Accepted Solution


    Author Comment

    Your link finally led me to a solution so I will accept it as an answer.

    The followin code now works for my purposes:

    Private Sub MySendKey(vKey As Long, KeyTypeAction As Long)
    '* vKey is the virtual keycode I want to send
      Dim MyInput(1) As INPUT_TYPE
      Dim KeySent As KEYBDINPUT
        With KeySent
          .wVk = vKey And &HFF
          If (vKey = VK_SHIFT) Then
            .wScan = 42
            .wScan = MapVirtualKey(.wVk, 0)
          End If
          .dwFlags = KeyTypeAction
          .time = 0
          .dwExtraInfo = GetMessageExtraInfo
        End With
        MyInput(0).dwType = INPUT_KEYBOARD
        CopyMemory MyInput(0).xi(0), KeySent, Len(KeySent)
        SendInput 1, MyInput(0), Len(MyInput(0))
    End Sub

    LVL 3

    Expert Comment

    Great..  thought it might.  Glad it did.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now