9

I'm writing an app (in C#) which as a part of it must simulate and send some key strokes to another application. I'm using http://inputsimulator.codeplex.com/ project for simulating keys, and it works in many applications, but in some it doesn't - i.e. Mortal Combat 4.

I've googled about it, and found many answers varying from 'it's impossible' to 'you must use XXX library' etc. Those answered scared me a lot, and even nearly convinced I'm not able to do it at that time, BUT...

M$ Virtual Keyboard works. It works in ALL applications. So it IS possible... Does anyone of you, clever guys, know how can I achieve this?

Fenix Voltres
  • 3,448
  • 1
  • 29
  • 36
  • what code do you actually have so far that you have tried ..? – MethodMan Jan 06 '12 at 15:47
  • I'm using project I've mentioned above. The lowest method there is [DllImport("user32.dll", SetLastError = true)] internal static extern UInt32 SendInput(UInt32 numberOfInputs, InputMsg[] inputs, Int32 sizeOfInputStructure); and it works perfectly in many apps, but not all. :/ – Fenix Voltres Jan 06 '12 at 15:54
  • 4
    @FenixVoltres: I reverse engineered the Windows On-Screen Keyboard and it uses the same input method as that library you linked to (`SendInput`). I hooked all of the keyboard-related functions that came to mind (`keybd_event`, `SendInput`, `SetKeyboardState`, etc.) and the only function that was called was `SendInput`). Try double-checking the library's code against the MSDN documentation. – Neal P Jan 06 '12 at 16:13
  • Wow, respect for that. :) OK, so this ensures me it's possible. Strange that it's not working, though. I'll try to even triple read all the documentation about that. Maybe it is some matter not connected directly with keyboard, for instance focusing? I do not set any focus anywhere (but it works in some apps). – Fenix Voltres Jan 06 '12 at 16:23
  • Could you tell me what program have you used to reverse-engineer On-Screen Keyboard? I'll try to get the difference from my code... (Tried http://wiki.sharpdevelop.net/ILSpy.ashx but it cannot do it). – Fenix Voltres Jan 06 '12 at 17:10
  • 1
    At first, I was going to disassemble OSK and hook all the keyboard-related functions; I decided that it would be easier to monitor program execution with API Monitor (http://www.rohitab.com/apimonitor) since there are only a handful of functions capable of producing input in that manner. You should log those functions above, as well as any functions relating to scan codes (they should be in the same category). – Neal P Jan 07 '12 at 03:08
  • Thank you very much for info on this tool, it's very useful, and helped me solve the problem - more info in my answer below. – Fenix Voltres Jan 07 '12 at 16:17

3 Answers3

6

Ok, I think I finally got it to work. I used API Monitor recommended by Neal P and it showed just minimal differences between OSK calls and mine. A bit later I've tried to make my calling thread sleep some time between sending messages with press and release key and that was it.

Fenix Voltres
  • 3,448
  • 1
  • 29
  • 36
  • For anyone else attempting to use API Monitor it will only show the first array item in the INPUT array, i.e. if you pass an array of 4 items to SendItem (for example OSK does this when it closes down to simulate LWIN keydown, U keydown, U keyup, LWIN keyup). To be able to see the whole array in the parameters window edit the User32.xml file in the "\API\Windows" directory where API Monitor is installed and change the line to – Julius Dec 08 '14 at 13:00
  • How did you manage to make it work ? I am trying to figure out the same problem but simply putting the thread to sleep for some time doesn't really solve anything. `InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_0);` `System.Threading.Thread.Sleep(1000);` `InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_0);` – Bogdan Molinger Aug 19 '16 at 00:55
  • any news on the comment from @BogdanMolinger? – MMM Aug 30 '17 at 13:05
1

Although you were able to achieve your purpose, the way you achieved it does not fundamentally answer your question: How to simulate keyboard input in ALL applications?

There's a bunch of situations where the common user mode Microsoft API already mentioned does not work, like game applications that use the DirectInput API or protected games.

I have built a library that can help in this situations, it provides a simple C API that internally communicates with device filter drivers. It is able to send input for DirectInput based games and also is able to bypass some game protections. I have checked and it is still able to bypass some known game protections by using the x64 version of the library. Game protections commonly hook only the x86 system's api. Well, at last now, 18 February 2012, this is what I'm seeing happening.

oblitum
  • 11,380
  • 6
  • 54
  • 120
  • You're right, DirectInput's my pain in the (_ ! _). I had to stop develop my app for a while, but I'll definitely check your library in next few days, it seems to be something I was looking for. – Fenix Voltres Feb 20 '12 at 14:38
  • @FenixVoltres Also, as currently I'm supporting a plain C API only, a user has contributed with a C# project that P/Invoke's it at github, you may check it [here](https://github.com/candera/kchordr/blob/master/Program.cs) – oblitum Feb 20 '12 at 15:37
0

Take a look at SendKeys on MSDN

Bueller
  • 2,336
  • 17
  • 11
  • 2
    I gave it a shot, but the same situation happens - SendKeys.Send(string) works, but not in every application. Notepad or Explorer handles it correctly, but I couldn't get it to work in any fullscreen game. – Fenix Voltres Jan 06 '12 at 16:07
  • Most newer online games take great pains to try to foil people writing "bot" programs so it is a little more difficult than say sending keystrokes to Notepad. You can still bypass some of their efforts by using the low level Win32.dll api calls but you open yourself to liability, of course depending on where you live so I leave the research up to you to figure it out. – Bueller Jan 06 '12 at 16:19
  • Well, could you spare a word or two if those methods aren't those I did use? Actually this project is a part of wider work which's my Bachelor's degree final project at AGH University of Science and Technology in Cracow, Poland, I don't want to break any law. :P BTW doing a virtual keyboard (or a new method of computer input in my case) is completely legal, I think? – Fenix Voltres Jan 06 '12 at 16:38