Send keystrokes to other applications - c#

I'm trying to write a program in C# that lets me send a keystroke to another program that's open but is not in the foreground. The purpose of the program is to let me start recording gameplay footage from my Xbox 360 using TotalMedia Extreme while also starting recording in audacity to record from the PC mic. That way I can get both tracks to be synced instead of starting gameplay recording and quickly switching to audacity and clicking record.
Basically all I need this program to do is detect a mouse click while TotalMedia Extreme is in the foreground and when the mouse is clicked, send the hotkey command to Audacity to start recording. Does anyone have an idea for how I could get started with this?
This seems like it might be a good place to start, but I don't think it will do the same thing I need to do:
Send fast textinput to another process (Window)

Check this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx
I suppose you could do it through Windows Hooks, in particular WH_KEYBOARD_LL

Just use AutoHotKey. The command SendMessage will send keystrokes to a non-active window, but you could also just have AHK activate the right window before sending keystrokes. Once you are done you can wrap it into a handy little hotkey or even compile it into an exe file.

AutoHotKey is definitely the rout I'd go. You can use a function called ControlSend to send keystrokes to specific controls of specific applications, even if they're in the background. Sounds like it would work perfectly for what you're needing. The forums over at AHKScript.org are really helpful too, so you might check them out as well!

Related

In Windows how to get highlighted text in a window that belongs to another process using C++ or C#?

It's in a C++ translation program. When user selects some text in some other program without pressing CTRL-C, I want to get the highlighted text so that I could translate it. C# solutions are welcome because this(text acquisition) is a relatively standalone component.
One possible solution is to add hook to mouse event. I detect the following messages: mouse drag with left button hold, then release left button. Then I send CTRL-C to the program and get the text from clipboard.
I save and restore clipboard so most programs would work well with the method. But I have observed some programs would process events like WM_KEYUP in their message loops. For these programs I send fake messages! Is there another safer way to get the highlighted text?
I was trying to find some API call to get text between two mouse cursor positions, but I cannot find one.
The right way to write a Windows program that interacts with another programs GUI interface is to use UI Automation. Specifically, you'll write a UI Automation Client.
It requires a bit of work, but a UI Automation solution will work with almost every other application. A hacky solution built on hooks and messages will also be a lot of work, but it will be very fragile. It will only work with certain kinds of applications in certain contexts. Lots of things can go wrong.

My app won't recognize keystrokes sent from SendKeys or SendInput, but does from the Keyboard

I've got a homegrown app (Master) that has a bunch of hotkeys defined. I need to automate the app with another external app (Control). I cannot rebuild the Master app, it runs, is already installed and can't be messed with.
Using hotkeys on Master works like a charm if I use a keyboard.
So, I figure I an use SendKeys.SendWait or SendKeys.Send to send the commands. While my code works fine with other apps (such as Notepad) it is not working with my custom app, Master. This could be for a number of reasons - Master is older, has .NET, C++ mixed... some low level video controls... who knows what's under the covers.
I need to get something higher level. I need to be able to send the keystrokes as if they are coming from the keyboard itself. I don't want the computer to be able to differentiate between the physical keyboard sending the keys and my app sending the keys.
What can I use and how will it be different?
The problem was not SendKeys.
I tried SendKeys.Send, SendKeys.SendWait, SendInput and keybd_event.
I later realized that the time between activating the application and the time sending the keystrokes was too short. I was waiting 200ms but simply by increasing this to 500ms I solved my problem.
The application took a long time to draw because of the embedded video players. Giving it another 300ms allowed it to be ready for the keystrokes I was sending it.

Disabling paste in console application

So, here's a weird question: Is there a way for me to disable the menu you get when pressing the "C:\" button at the top left of the console?
I am making a game, for fun, but it would take away alot of the fun if players were able to paste words into the command-line.
I haven't found any topics about it, so I was wondering if anyone has an idea on how to get this done, or atleast a work-around.
Thanks in advance!
Edit: Would a simple reg-ex for any ^ character work? Or does the console not use ^C and ^V?
a) Use GetConsoleWindow to get window handle; use subclassing and hooks to filter mouse and keyboard events. Though this approach probably has a lot of hidden obstacles, because of Windows console implementation specifics.
b) Use timing analysis. Measure time interval between keystrokes and ignore input if this interval is unreasonably short. You may have to experiment to find the criterion that works best. Maybe it's better to measure input duration of entrie word, not inter-character intervals.
Note that pasting text to Windows console is implemented via keyboard input simulation, i.e. when you paste something to a console, the console application observes a sequence of key-down/key-up events.
I'm not sure if the console supports pasting via a keyboard combo, you can paste either via the menu or just right clicking in the console (this depends on how the console is set up though).
I'd suggest that a better solution would be to create a winforms app instead where you can control the input/output easier than in a console app.
Otherwise, one workaround might be to call Clipboard.Clear every 100ms or so to make sure that they'll never have time to copy and paste anything until you've cleared it out, but this could of course upset and/or confuse the users if they have your application running in the background and are trying to do some work in some other software.

"Click" button of a program from an application I'm building

Boring background:
I have been working with UltraVNC to control some PC's at work and it does the job great but in order to simplify things I created a program that interfaces with it in C#. Basically I take advantage of the commands the viewer offers to connect, control, watch or transmit to each PC.
Problem is anyone can access the PC's since it has one main account (no domain controller). I need everyone to sign for the PC before they can use it, so to make my job easier I open each PC and block the inputs + blank the screen that way there obligated to sign before use.
Opening each pc and press the block button can be hassle especial when you’re helping someone and a user leaves, others come (btw I work at an electronic library). UltraVNC doesn’t have a command for this; it’s been requested but I don’t think it’s much of a priority for them and the code seems very intimidating for a novice like me so I thought I could try a hack to get what I want.
Problem: I want to “click” a button in a program I use, from an application that I am building in c#. I can currently use the process class to get the handle and identify the specific window I want to use but I have no way to find the button handle which I read is what I need. I found stuff about using findwindow and sendkeys for this but I don’t see how that’ll work unless the button had a keystroke assigned to it which it doesn’t.
So can anyone point me in the right direction?
Why not use something like Eficium Cybercafe SurfShop to achieve what you want? After teh user finished you log the session out, and before someone can log in, they have to sign in.

calling the user attention outside of app

I want to create an alarm app for myself. On certain conditions (i need to poll websites) i want my app to inform me and make it HARD TO MISS. So i can take appropriate action or ignore it if i need to do something else.
I wrote a test app and using a BalloonTip (ShowBalloonTip with notifyIcon) isnt great. One of my previous apps brings the window in front of you and does a MessageBox however that doesnt always work or work well (if i somehow miss it or accidentally forget to click ok no futher messages will occur).
What are your suggestions?
You could also make it a system tray application and change the icon out if there is something which requires attention, a la a messenger application. That may not be "hard to miss" but I am trained (for better or worse) to look down at the icon tray when I see something blinking.
Where I work, we have a TimeTracker application (built in house) with which developers are supposed to log what we worked on and when. I am notorious for not using it. So, I wrote my own (Windows Forms) version for my own use which, every hour opens up and takes over my screen:
It is a frameless dialog which consumes the entire screen.
TopMost = True.
On resize, it sets WindowsState = Normal and resizes to fill the screen.
While it is open, it polls for taskmgr.exe and procexp.exe and kills them if found.
It disables the start menu to prevent cmd.exe commands from the menu in Windows 7.
The only way to close it is to enter a log, only then is the OK button shown!
So far, it's working out well - no one has been able to break it!
My less drastic suggestion would be to have a notification which pops up momentarily above the system tray. After a second or two, fade it out. Keep showing the notification every 30 seconds or so until it is dismissed.
Always-on-top window in the corner of the screen?
You could always set your window to be a top most window, make it full screen, and activate it. It would be very, very hard to miss...
Granted, it would also be very annoying, and not something I'd do to other users...
My "real" suggestion would be to use sound along with standard notification methods if this is going to be used by other people, as that's an easy way to grab attention without necessarily killing their workflow. A modeless window that appears in a corner of the screen, especially if combined with sound and color, can be very effective to grab attention.
The industry has been adopting these ambient orb devices and variations of it when such a hard-to-miss notification is required. It is used for tracking the stock-market and for broken-daily-builds.
http://www.ambientdevices.com/cat/orb/MAN_Ambient%20Orb_3-23-03.pdf
Regards

Categories