Link to home
Start Free TrialLog in
Avatar of X_Ivar
X_Ivar

asked on

VB's SendKeys and Remote Desktop

Hi!
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?

/X_Ivar
Avatar of mugman21
mugman21

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?

Mugman
Avatar of X_Ivar

ASKER

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)
Const KEYEVENTF_KEYUP = &H2
Const
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.
Avatar of X_Ivar

ASKER

IlanProd!

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.

ASKER CERTIFIED SOLUTION
Avatar of CAVcc
CAVcc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of X_Ivar

ASKER

CAVrr!
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
'* KeyTypeAction can be 0, KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY
  Dim MyInput(1) As INPUT_TYPE
  Dim KeySent As KEYBDINPUT
    With KeySent
      .wVk = vKey And &HFF
      If (vKey = VK_SHIFT) Then
        .wScan = 42
      Else
        .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.