Findwindow and SendMessage

findwindowsendmessagevb6

I am trying to figure out why this is not sending a ALT+F to notepad,

Private Declare Function FindWindow1 Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_KEYUP = &H101
Private Const WM_KEYDOWN = &H100
Private Const WM_SYSKEYDOWN = &H104

Private Sub Command_Click()
Dim parenthwnd As Long
Dim hwnd As Long

  parenthwnd = FindWindow1(vbNullString, "Untitled - Notepad")
  retvalue = SendMessage(parenthwnd, WM_SYSKEYDOWN, VK_MENU, 1&)
  retvalue = SendMessage(parenthwnd, WM_KEYDOWN, VK_F, 1&)
End Sub

I get a value for parenthwnd but not for any of the retvalue values (0).

What am I missing?

Best Solution

You can try to send/post these messages all you want. Unfortunately they don't effect the return value of GetAsyncKeyState - which is what the system uses, while processing the VK_F message - to see if the alt is down.

As a result you can't fake keystrokes with modifiers to other applications using this mechanism.

To get around this, you need to use SendInput - but this requires that the application to receive the keystrokes is the foreground/focus window.

Related Question