• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4029
  • Last Modified:

VB's SendKeys and Remote Desktop

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?

1 Solution
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?

X_IvarAuthor Commented:
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.
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(http://www.mentalis.org/apilist/FindWindow.shtml) it's not that complicated...

hopefully the first one will work because he's the easiest one.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

X_IvarAuthor Commented:

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.

X_IvarAuthor Commented:
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
    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

Great..  thought it might.  Glad it did.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now