SendMessage WM_KEYDOWN?

I’ve been trying to type the character 1 in a notepad with focus using SendMessage and WM_KEYDOWN. I realize I can use SendKeys and other methods to accomplish this but I need to do this either using SendMessage or SendInput(which seems more complicated). I can’t get SendMessage to output the character and I was hoping someone could get me pointed in the right direction because it seems like it should be working. Thanks…

SendMessage:

[code]
#If TargetWindows
Declare Function SendMessageA Lib “User32” (hWnd As Integer, Msg As Integer, wParam As Integer, lParam As Integer) As Integer
Declare Function SendMessageW Lib “User32” (hWnd As Integer, Msg As Integer, wParam As Integer, lParam As Integer) As Integer

#If TargetWin32
If System.IsFunctionAvailable(“SendMessageW”, “User32”) Then
Return SendMessageW(hWnd, Msg, wParam, lParam)
Else
Return SendMessageA(hWnd, Msg, wParam, lParam)
End If
#EndIf
#EndIf[/code]

FindWindow:

[code]
#If TargetWindows
Soft Declare Function FindWindowA Lib “User32” (className As Integer, title As CString) As Integer
Soft Declare Function FindWindowW Lib “User32” (className As Integer, title As WString) As Integer

Dim handle As Integer
If System.IsFunctionAvailable(“FindWindowW”, “User32”) Then
handle = FindWindowW(0, title)
Else
handle = FindWindowA(0, title)
End If

If handle <> 0 Then
Declare Sub SetForegroundWindow Lib “User32” (hwnd As Integer)
Return Handle
else
Return -1
End If
#EndIf[/code]

Button Action:

[code]
Const VK_1 = &h31
Const WM_KEYDOWN = &H0100

Dim intMsg As Integer = SendMessage(FindWindow(“Untitled - Notepad”), WM_KEYDOWN, VK_1, 0)[/code]

Here you go:

https://www.dropbox.com/s/3giiqrsjza36k23/PostMessageToNotepad.xojo_binary_project?dl=1

Three different methods included.

[quote=394324:@]Here you go:

https://www.dropbox.com/s/3giiqrsjza36k23/PostMessageToNotepad.xojo_binary_project?dl=1

Three different methods included.[/quote]
I did try PostMessage as well but couldn’t get it working either. I’ll take a look at the examples thanks.

@

So my code was almost identical line by line to yours except for FindWindowEx. I thought maybe I wasn’t getting the exact window handle I needed to get this working. I’m guessing that’s what was going on and what FindWindowEx has corrected. This is fantastic, doesn’t even require focus. Thanks.

Yup, you were sending the key to the window and not the control responsible for the text :slight_smile:

I really dig WM_SETTEXT but to get it working you have to change the declare a little bit in case anyone wants to send a long string which is a lot easier than each character. wString is your friend.

’ SendMessageT

#If TargetWindows Declare Function SendMessageA Lib "User32" (hWnd As Integer, Msg As Integer, wParam As Integer, lParam As wString) As Integer Declare Function SendMessageW Lib "User32" (hWnd As Integer, Msg As Integer, wParam As Integer, lParam As wString) As Integer #If TargetWin32 If System.IsFunctionAvailable("SendMessageW", "User32") Then Return SendMessageW(hWnd, Msg, wParam, lParam) Else Return SendMessageA(hWnd, Msg, wParam, lParam) End If #EndIf #EndIf

Dim w As Integer = FindWindow("Untitled - Notepad") Dim c As Integer = FindWindowEx(w, 0, "edit", "") Dim intMsg As Integer = SendMessageT(c, WM_SETTEXT, 0, "Hello World!")

@

One last question. How do I send alt + (any character)? I’ve been trying to use WM_SYSKEYDOWN but it’s not happening…

What are you trying to do with ALT? Are you trying to navigate the menus? I don’t think there’s many uses for ALT inside the Edit control of notepad,

intMsg = PostMessage(c, WM_CHAR, asc("a"), ShiftLeft(1, 29))

[quote=394338:@]What are you trying to do with ALT? Are you trying to navigate the menus? I don’t think there’s many uses for ALT inside the Edit control of notepad,

intMsg = PostMessage(c, WM_CHAR, asc("a"), ShiftLeft(1, 29))

I’m not really using notepad, I can’t debug easily on the actual app I am writing for so I am just using notepad to test this in advance.

I needed to send a alt + O to actually send the command to the app once it received all the chars.

I understand how the lParam works now thanks to this example, which is what I was missing.

Thanks again for all your help. :slight_smile:

[quote=394338:@]What are you trying to do with ALT? Are you trying to navigate the menus? I don’t think there’s many uses for ALT inside the Edit control of notepad,

intMsg = PostMessage(c, WM_CHAR, asc("a"), ShiftLeft(1, 29))

Couldn’t get WM_CHAR to work to send the alt + char but after a lot of research and trial and error I figured out how to configure the entire message and using WM_SYSKEYDOWN this works to open the file menu in notepad.

Thanks for the ShiftLeft hint Julian.

[code] Dim repeatCount As UInt8 = 0
Dim scanCode As UInt8 = 0
Dim extended As UInt8 = 1
Dim context As UInt8 = 1
Dim previousState As UInt8 = 0
Dim transition As UInt8 = 0

Dim lParam As UInt32
lParam = repeatCount +_
ShiftLeft(scanCode, 16) +_
ShiftLeft(extended, 24) +_
ShiftLeft(context, 29) +_
ShiftLeft(previousState, 30) +_
ShiftLeft(transition, 31)

intMsg = PostMessage(c, WM_SYSKEYDOWN, &h46, lParam)[/code]