Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all. I am trying to write simple c# program,but all tutorials and snippets on the net doesn't work for me(I have only very basic knowledge of C#). I come to you guys for help if possible please.

What I am trying to do is send some keystroke to application in the background (The rest of the computer to be usable ) based either on the pid of the application or the name. Here comes the hard part (if based on name) I Would like to open few of the same applications and obviously they will have the same name so the work around that I believe would be just to send the same keys to all windows with that name.

I am posting below my initial code from Python which I know it works, but would love to convert it to C#. Also I would like for the program to have 2 button for start and stop, nothing fancy just the basic form1 app will do. Here is the code:

Python
#you will need the win32 libraries for this snippet of code to work, Links below
				
                import win32gui
				import win32con
				import win32api
				from time import sleep

				#[hwnd] No matter what people tell you, this is the handle meaning unique ID, 
				#["Notepad"] This is the application main/parent name, an easy way to check for examples is in Task Manager
				#["test - Notepad"] This is the application sub/child name, an easy way to check for examples is in Task Manager clicking dropdown arrow
				#hwndMain = win32gui.FindWindow("Notepad", "test - Notepad") this returns the main/parent Unique ID
				hwndMain = win32gui.FindWindow("Notepad", "test - Notepad")

				#["hwndMain"] this is the main/parent Unique ID used to get the sub/child Unique ID
				#[win32con.GW_CHILD] I havent tested it full, but this DOES get a sub/child Unique ID, if there are multiple you'd have too loop through it, or look for other documention, or i may edit this at some point ;)
				#hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD) this returns the sub/child Unique ID
				hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD)

				#print(hwndMain) #you can use this to see main/parent Unique ID
				#print(hwndChild)  #you can use this to see sub/child Unique ID

				#While(True) Will always run and continue to run indefinitely
				while(True):
					#[hwndChild] this is the Unique ID of the sub/child application/proccess
					#[win32con.WM_CHAR] This sets what PostMessage Expects for input theres KeyDown and KeyUp as well
					#[0x44] hex code for D
					#[0]No clue, good luck!
					#temp = win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x44, 0) returns key sent
					temp = win32api.PostMessage(hwndChild, win32con.WM_CHAR, 0x44, 0)

					#print(temp) prints the returned value of temp, into the console
					print(temp)
					#sleep(1) this waits 1 second before looping through again
					sleep(1)


What I have tried:

I've tried pretty much every youtube or first page google tutorials, but nothing can do exactly what I described above, please help!
Posted
Updated 15-Dec-16 4:40am
Comments
[no name] 14-Dec-16 15:33pm    
Okay so what is it that you expect us to do for you? We aren't going to write your code for you and we aren't a code translation service.
Member 12904911 14-Dec-16 15:51pm    
Wow That's a nice welcome to community I tried for first time.... never did I ask for you to write the whole code for me, but to rather point me in the right direction:

what libs do I need? How to approach the problem? Stuff like that

Let's hope most people around here are not like you....
Dave Kreskowiak 14-Dec-16 16:13pm    
You haven't said what the problem is nor have you asked a question so I have no idea what you're looking for.
Member 12904911 14-Dec-16 17:02pm    
Sorry I thought I mentioned it above. I am trying to simulate keystrokes in background application. And while I know how to do that in python I would love to do it in c# but never touched the language before. Just want to make nice looking little form with start and stop button and simulate press of 1,2,3 for example in backgrounded application.
Dave Kreskowiak 14-Dec-16 19:53pm    
That's not a description of a problem. That's a description of what you want to do. Just leaving it at that gives no indication of what exactly you're having a problem with, other than "I don't know C# or Windows development".

In order to send keys to a window that doesn't currently have the input focus, you have to send messages directly to the window handle of the window you are targeting. That is what your Python code is doing.

The problem for you is converting this code into C#, defining and setting up the function calls into Win32 and defining all the constants that code is using.

You can check out PInvoke.net for defining the functions calls into Win32. You already have the function names to use in your Python code, FindWindow, GetWindow, and PostMessage.

1 solution

So what? the Code you showed has 3 Windows-API Calls that are exactly the same called by C# (for C# is's a call over Platform-Invoke (PInvoke) - the same you do in Python. Than you have one .NET call to System.Threading.Thread.Sleep(Milliseconds).
In the time you wrote all the ... comments here you could have finished that 4 lines of Code - no? You don't know C# but want to write Code that has nothing do to for .NET framework - just calling an external C++ API - then go through a simple C# tutorial or read about the needed functions on MSDN - how did you do that in Python? Does your code afford any knowledge of Python? No, you just have to know Win32-API - like in C#.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900