I'm trying to write an application that can reliably send key presses to another application.
The second app has a text field which opens when 9 is pressed.
Text can then be typed and then the text field is closed when Enter is pressed.
If I use SendKeys to try to do this the field does not open if I send 9, but if the field is already open my tool can send text ok, but then it does not close if I send an Enter.
I'm guessing this is because a lower-level interaction with the keyboard driver or similar is being used.
Is there a reliable way I can simulate actual keyboard input to the application from another C# app?
I have found a few potential solutions online but these are generally incomplete with either missing references etc. or missing code elsewhere.
I will continue to search and I will post the solution here if I find it!
is it an option to invoke on screen keyboard provided by os. your app, calling app will loose or cant track the key events, however if you open your second application and invove osk.exe, then the purpose of robust keyboard sending keys to an app could be fullfilled.
I explained this here
EDIT: ignoring on screen keyboard
You can try using RegisterHotKey to open the textpad on a specific sequence of key(s) and persist the handle in a instance variable. On the sequence of second hot key(s) you can close the textpad (you already know the handle)
there is a video explaining this
Related
I'm trying to create an application that will process selected text when a keyboard shortcut is pressed. I used part of this method to get the currently selected text (and there seems to be no other way to get the selected text from any application:
string GetSelectedText()
{
var oldClip = Clipboard.GetText();
SendKeys.SendWait("^c");
string selection = Clipboard.GetText();
Clipboard.SetText(oldClip); // preserve clipboard
return selection;
}
My first thought was to use a system tray icon. When clicked, the icon would use the above method to get the current selection, and process it. However, using the clipboard like that requires the application to be in focus, whereas clicking a system tray icon brings the icon into focus before the method can run.
My next thought was to use a global keyboard shortcut. I came across this article about getting a global hotkey, and it could in fact listen to keyboard combinations, but only within one app, which is not what I want - I want to listen to the keyboard shortcut across any app.
I have already read this article (which came from this answer) which does manage to listen for keys across any app but only manages to listen to one key at a time (e.g. a single LControl, or Shift, or C, etc).
My question here is: How can I listen for a certain combination of keys, no matter what application is in focus, and execute the method above to get the current selection?
If there is a better way to retrieve the current selection, or if it can be done when the application is not in focus, answers explaning how it is possible are also appreciated; the main focus, however, is listening for a keyboard combination across all applications.
I ended up using the library that Chris Dunaway mentioned, MouseKeyboardLibrary.
As you may have seen in the comment thread where he brought it up, it initially didn't work for me. It turned out that the library didn't work for me because of an invalid module IntPtr, so I changed the library to use IntPtr.Zero where it previously used Marshal to get the module pointer, which made it work as expected.
I want to get key input in c#.net in console application but the console will be invisible like freeconsole in c# and it will get what you type and set it to a string named like idk input then messagebox.show(input); then everytime you type a key it messagees it in the background,
i want to learn to make games so yeah and i want it to work in background.
The way Windows works is that when a key on the keyboard is hit, Windows sends a message (actually, several) to the Window which currently has the focus. If your Console app is in the background, then by definition it will not have the focus.
All is not lost, though. You can setup what's called a global keyboard hook. This does mean that you'll get ALL keyboard hits, though.
And as #AaronM.Eshbach says, this is a keylogger and generally not a good thing...
I'm currently practicing hotkeys and writing a small background app that instantly googles the selected text on the ctrl+shift+alt+N hotkey.
It all works very well. I register the global HK, I intercept the message via WndProc() etc.
What does not work is the way I'm trying to copy the selected text in the currently focused window by sending it a CTRL+C keyboard input.
Here is my current method, in a nutshell:
IntPtr fWin = GetForegroundWindow();
SetForegroundWindow(fWin);
//InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
SendKeys.SendWait("^(c)"); //I tried both ways, with the InputSimulator lib, and the standard SendKeys. None work.
Thread.Sleep(1000); //I wait a little bit for the clipboard to get the text.
Console.WriteLine(Clipboard.GetText()); //This is where I get disappointed...
Process.Start("firefox.exe", "-new-tab http://www.google.com/search?q=" + Clipboard.GetText().Replace(" ", "%20"));
This method works for some applications only (like the one we use in the office: Trados Studio which is quite the complex text management software). However, I'm unable to make it work with applications like Word, Outlook or Notepad.
Believe it or not, I'm able to send them letters like this:
string txt = "Y U NO WORK?";
SendWait(txt);
//OR
InputSimulator.SimulateTextEntry(txt);
But when it comes up to CTRL+C, nothing happens.
Do you guys have an idea of what's going on? I hope there isn't some MS restrictions on this one... Thanks in advance!
I'm not able to help you directly but may I suggest that you read about OLE/COM.
https://msdn.microsoft.com/en-us/library/19z074ky.aspx
I know that you can use this to accesses word, excel etc from other applications.
The solution was to make sure that the keys were resolved and released before sending CTRL+C input to the focused application to avoid Hotkey overlap. Otherwise, the focused application is sent a messed up HK that it doesn't understand or handles weirdly.
It would explain why Word inserted a date or inserted character "i" or copyright instead of copying to clipboard.
Hope it makes sense!
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.