Notifications
Clear all

[Closed] Sending keystroke to application

Hi all.
Here I asked a question about sending keyboard data to another application. I thought that I can do it with importing some functions from user32.dll, but I don’t know exactly how it is works, and our codemonster DenisT said that it may be a wrong way (if I understood him right).
Where do I start?
Thanks.

5 Replies
1 Reply
(@denist)
Joined: 10 months ago

Posts: 0

you are right about using a user32 help. but it’s not as simple as just add a sendmessage method.

the task looks as:

find a target’s window hwnd

activate or not (depends on what you want to do) it

send a key pressed message with a specified key code

send a key released message

and probably (likely) send not a release message but a postmessage

that’s why i said that just an adding sendmessage method is pretty far from a final solution

with most recent max versions (2012+) i hope we can do everything using pure mxs without additional .net help

hmm… i see your want to send a keystroke. it needs more details.
because i don’t believe that an application (main window) can get a keystroke (string) with any sense. (set a title is only a reason i see)

 lo1

It’s simpler to just activate the window HWND and use dotnet SendKeys class.

Hey guys,
Thanks for explanation of this mechanism. I thought that I only should set window active by setfocus and then sendmessage only.
2 lo:
Yep I thought about this method, tried it without any sucsess.
I used this code (for Windows Media Player classic , If you havent it installed you can try with any another soft, Notepad etc.)


global _user32 =
(
	source = "using System;
"
	source += "using System.Runtime.InteropServices;
"
	source += "class User32
"
	source += "{
"
	source += " [DllImport(\"user32.dll\")]
"
	source += " public static extern IntPtr GetActiveWindow();
"
	source += "}
"

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

	compilerParams.GenerateInMemory = on
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	compilerResults.CompiledAssembly.CreateInstance "User32"
)

	WindHandle=  for w in (windows.getchildrenhwnd 0) where matchpattern w[5] pattern: "*Media Player Classic*" do exit with w[1]

	intPtrWindHandle = dotnetobject "System.IntPtr" WindHandle
	WM_SETFOCUS = 0x7 
	windows.sendmessage WindHandle WM_SETFOCUS 0 0

	SendKeys=dotNetclass "System.Windows.Forms.SendKeys"
	SendKeys.SendWait "1"



Thanks.

i have SME window in focus
how can I send “Alt + Key” to SME editor? or any other way to access those menus

SendKeys.SendWait “%” works and activating first menu item but if “%E” is used nothing seems to happen