Simulate Key Press at hardware level - Windows

Anti Earth picture Anti Earth · Jul 22, 2012 · Viewed 13.1k times · Source

I'm looking for a language or library to allow me to simulate key strokes at the maximum level possible, without physically pressing the key.

(My specific measure of the level of the keystroke is whether or not it will produce the same output as a physical Key Press when my computer is already running key listeners (such as MouseKeys and StickyKeys)).

I've tried many methods of keystroke emulation; The java AWT library, Java win32api, python win32com sendKeys, python ctypes Key press, and many more libraries for python and Java, but none of them simulate the key stroke at a close enough level to actual hardware.
(When Windows MouseKeys is active, sending a key stroke of a colon, semi colon or numpad ADD key just produces those characters, where as a physical press performs the MouseKeys click)

I believe such methods must involve sending the strokes straight to an application, rather than passing them just to the OS.

I'm coming to the idea that no library for these high (above OS code) level languages will produce anything adequate. I fear I might have to stoop to some kind of BIOS programming.

Does anybody have any useful information on the matter whatsoever?
How would I go about emulating key presses in lower level languages?
Should I be looking for a my-hardware-specific solution (some kind of Fujitsu hardware API)?

I almost feel it would be easier to program a robot to simply sit by the hardware and press the keys.

Thanks!

Answer

rfinz picture rfinz · Jul 22, 2012

Second solution, super convoluted, a ton of virtualization, diabolical, totally untested, but theoretically should work, unless the datasnip program doesn't actually write to the keyboard buffer, but instead simulates keystrokes like you've been trying to. That would suck, and I would find the description of their product to be highly misleading.

You'll need:

And some knowledge of writing to com ports... which, looks like there's a good python module here: http://pyserial.sourceforge.net/

First, write a small program that will send characters, hexcode, etc. as necessary to the COM port of your choosing. Second, create a virtual COM port pair using com0com. At this point, connect your program to one of the COM ports created, and datasnip to the other, making sure that both sides of the communication are using identical baud rates, parity and stop bits, and data length parameters.

At this point you should have a keyboard that is identical to a hardware one, as far as the OS can tell.