I have a keyboard with media keys on it, the usual stuff, play/pause, stop, previous, next, as well as volume control.
I would like to mimick this type of behavior, only from my own program. Meaning that if Spotify is running, playing a song, and my program says "Pause" I would like Spotify to pause. All this without me hardcoding support for Spotify.
I know that I can use something like "SendKeys" to fake the user pushing the corresponding key on the keyboard, I've seen AutoHotKeys scripts for this.
But what is the API functions that I would use directly from my program? Do they even exist?
An easy option would be to use the AutoHotkey dll. I've found amazing-andrew's repo AutoHotkey.Interop very useful for this.
Then you can execute ControlSend like so:
//grab a copy of the AutoHotkey singleton instance
var ahk = AutoHotkeyEngine.Instance;
//execute any raw ahk code
ahk.ExecRaw("ControlSend, , {Media_Play_Pause}, ahk_class SpotifyMainWindow"); // send directly to the window
Related
Is there a way to start a process in a separate instance with it's own mouse and keys that don't interfere with the use of other processes.
For Example: Let's say I want to make a program that automates a program and it requires clicking on locations and trying in keys. While this is happening I want to be able to do other things on the computer like nothing is happening.
Attempt At Clarity:
So this separate instance would be given inputs for the program based on what it sees or something like that. No separate output from the user. More like the program is running the process in the background while youre using your computer like normal
You can use .NET for input simulation using WinForms.
For keyboard input you need SendKeys and for the Mouse you can use Cursor. Although it should be noted that the OS - usually - only includes one mouse pointer, and so setting it's position is machine wide.
For a better simulation of mouse input, you will need to resource to p/invoke. The recommended pattern is to use SendInput.
Alternatively, you could look for existing solutions that emply this technique, such as InputSimulator.
I'm trying to send virtual button presses to an app. I tried using SendKeys.Send("{6}");, but the app receives it like a string or something similar because it doesn't act. The app I want to send the key to is VisualBoy Advance. My code simply allows me to play remotely using a GUI, so when I press left on the GUI the VBA should receive the keystroke.
A part of the code:
case "derecha":
SendKeys.SendWait("{4}");
break;
The { and } characters are special escape characters. I don't think 6 is an escape value. Have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx for the known escapes. For example, SendKeys.SendWait("{Right}"); would send a right arrow key.
However,
Many games and emulators will be using low level keyboard hooks to read the keyboard state. It's quite rare to find games that use the Windows events model for keyboard reading. SendKeys only sends to the Windows messaging system. You may need a more low-level way of sending events.
If the target app is using DirectInput, you might be able to use the SendInput function from user32.dll:
Docs - http://msdn.microsoft.com/en-gb/library/windows/desktop/ms646310%28v=vs.85%29.aspx
Example - http://www.ownedcore.com/forums/mmo/warhammer-online/186390-sendinput-example-c.html
There is also the Interception library, but I've never used it myself:
http://oblita.com/interception.html
My app (in C#) need to interface with a USB bar-code scanner, which is basically working like a keyboard. It inputs the bar-code with an enter key at the end.
The app need to be work even when it's at background, so I am using low level keyboard hook to get and filter the bar-code out in the global key events. This part is already working.
Here is my problem: I don't want other apps to get the keyboard(scanner) inputs if it is a bar-code. And the normal key events should not be interfered. In one word, block the key events selectively. Is this possible?
My app is in C#, but I have no problem with C++ or more native solutions as long as it's easy to integrate in C#.
Thanks.
Additional Information:
The whole idea is working at background, even when it's not active. It watches the global key events stream and spot the bar-code sequence (already implemented with Hook). And most importantly, it do NOT interfere with normal keyboard events nor other applications' operation. That's why I cannot block all the key events or make it top-most.
I already can get the bar-code. I need to prevent other applications from getting the bar-code.
At the end of your keyboard hook you would call CallNextHookEx to execute next hook in the chain.
I would suggest that put some unique signature as a preamble for your barcode so that your keyboard hook procedure can detect it as a valid barcode input from your scanner. Now, when you get this data, just skip the call to 'CallNextHookEx' so that the chain will be discontinued and other programs won't get your barcode. Otherwise - call 'CallNextHookEx' so the chain can continue.
Note: This is my theory, I have never tried the exact same thing myself. I have however, written hooks in C++ and C#.
Check this project out
http://globalmousekeyhook.codeplex.com/
It is in C# as well so will make your coding easier. Sounds like all you need is to hook up the global key press event and suppress it by setting the Handled value or something similar.
I want to send an Application Key Presses, To Automate some stuff that has to be done repeatedly and So I don't always have to cramp my fingers.
In C#, it's nice to use SendKeys.Send(), but this won't work because the Application doesn't take Windows Messages. SendKeys.SendWait() does nothing at all.
How would I STILL Simulate the Keyboard events?
Come To Think of It, I was going to use some P/Invoke to simulate Mouse Events too, but If it takes no messages, How Can I get around that?
EDIT - I can use mouse and keyboard to interact with the program, I just cannot manipulate it with Windows Messages sent from my own Code.
Have you tried AutoIt?
Is it a console app? If so, maybe you should be SendKeys'ing to the command shell instance it is running in.
I know something about MACROS. I don't mean the ASSEMBLY language kind. I am talking about those programs that you can use perform repetitions actions on another program. I am talking about those programs that you can use to record a series of events on your computer, like, mouse movements and button clicks and then you can play them back. Some of them are elaborate enough to run only on a paricular app that you designate.
I wrote one of sorts once. It was a program that launched an Excel sessions and then used the dynamic data exchage pipe of some kind to feed the excell session script commands. It worked.
But something on the level of the operating system, I imagine, is a whole different story.
How does someone go about writing a "macro" in C#?
I think the approach I will take is to use the spy routine that comes with the development environment to get a list of the proper messages and parameters (wm_lbuttondown for example) and then use dynamic data exchange to send those messages to the app.
So I have three questions.
Is this the best way to do this?
How do I get a handle to an app that is already running?
How do I send user-like messages to an app that is already running?
There are different answers based on many following factors:
is it 3rd party or your own
application?
does it have automation interface
GUI toolkit used in app
If it is a 3rd party app then you need to work on Windows API level via PInvoke - subclassing WinMain proc, capturing and sending input messages, etc. There are 3rd party library for that task. C# obviously is not a right choice for such task.
In case application has automation model (like Excel) it's a pretty straight forward to write program that will be interact with this app.
If it's your own application you want to enhance with macros functionality then you should take this into account on design state. If you use Command pattern from the beginning then it's not hard to program macro recording.
You should provide more details to get a better answer.
Oh, I almost forgot to answer those three questions
Is this the best way to do this?
Depends on concrete scenario
How do I get a handle to an app that is already running?
Depends on application. If it's a native Win app you can easily get process Id and window's handle via WinApi.
How do I send user-like messages to an app that is already running?
Once again it depends on application type. For native win apps you can easily send WM_XXX messages via WinAPI
Unless its something you need to add in your own program you can just download a keyboard/mouse macro program and use it to perform these repeatable actions.
On the other hand to perform macro's in your own program you would want to find a way to record the buttons clicked and write them to a temporary list that can be saved and then run the list by clicking the buttons (programmically).