Hi.
I am trying to code a program to send keys to a certain window.

For example, in notepad I want the program to write "test" over and over again, in the notepad window. However, I do not want this window to be an active window. Is it possible to make it send keys through a background method?

I hope you understand what I mean, i have trouble explaining it:(

Dani AI

Generated

Short summary and a practical route: sending keystrokes to a real, interactive control in the background is possible for simple Windows programs (Notepad, other standard Win32 edit controls) by locating the target window and sending Win32 messages directly to its edit control. That is more reliable than trying to hide a form and call SendKeys (as suggested) — SendKeys needs the target to be active and will steal the user's keyboard (as warned). For a game, however, the situation is different and far more restricted.

Concrete approach for standard apps (Notepad-style)

  • Find the target window (FindWindow / process enumeration), find its child Edit control (FindWindowEx), then send text with SendMessage using WM_SETTEXT (replace whole text) or PostMessage/SendMessage with WM_CHAR/EM_REPLACESEL to simulate typing. The usual .NET route is P/Invoke to user32.dll; example usage is short and direct:
' P/Invoke declarations (VB.NET)
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, lParam As String) As IntPtr
End Function

' Then locate Notepad's Edit control and call SendMessage(editHandle, WM_SETTEXT, IntPtr.Zero, "test")

Why this often fails for games or protected processes

  • Many games read input from DirectInput/RawInput or use exclusive fullscreen and ignore WM_KEY*/WM_CHAR. Windows also enforces UIPI/integrity levels (messages won’t cross to a higher‑privileged process), and foreground rules make some approaches unreliable. Anti-cheat systems flag DLL injection or driver-level input; doing that can break terms of service and lead to bans.

Practical recommendation

  • For automating a simple editor, use the SendMessage route above. For a game, prefer official scripting/plugins or an exposed API; otherwise expect to need advanced techniques (DLL hooks, driver inject) that are complex and carry legal/ethical risks. For custom projects, add an IPC/automation endpoint so external tools can interact without fragile UI hacks.

Recommended Answers

All 4 Replies

set the forms visible property to false
call the forms .focus method
then use the send keys command it should allow you to
send keys to an invisible form. or call the process using the process class and focus to the process example notepad.exe you might need to get the window handle first. then send keys. I would simply write to a textbox field in a form and send the results to a txt file using streamwriter class.

any time you call "" send key "" the user will know,, it will take control of your
computer.

best do what jdsurgeon said and write to a text file and update that text file without bothering the user by taking control of their keyboard.

does this answer your question or do you need to know how to set focus on the text file? then send a key ?

I ad the idea of a game automation program. Which clicked keys at a certain time but without it being the active window:(

you probably can do this with code but the hard thing will be trying to figure
out what command to send because its a game, you can try to use software such as
network monitors, process monitors to dissasemble or see whats being sent as far
as commands but i think that might be alittle out of the scope.

other then that,, you can still do this if you need to by ubot studio and or imacro
but it will take command over the console and keyboard and have to have focus on the game.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.