simulating keypresses (not through windows)
2 questions
1. How may I change the value on port 60h? I tryed using _outp, when I check with _inp it turns to -2.
2. How may I generate aninterupt 9 to invoke the function(s) that execute on this event?
[221 byte] By [
answer] at [2007-11-18 17:45:12]

# 1 Re: simulating keypresses (not through windows)
Is it possible to simulate a keypress without sending messages. Some programs do not process the keyboard messages (e.g. Games) What they do is they look up the state of the keyboard. It should be possible to simulate keypresses port 60h is where windows reads the keyboard and when interupt 9 is generated windows scans port 60h. I learned how to read the keyboard, now I want to set port 60h and generate an interupt 9 which will make windows believe that a key has changed state. Windows also keeps the keydata in memory. How may I go about similutating keypresses without using sendmessage/postmessage?
answer at 2007-11-11 1:25:53 >

# 4 Re: simulating keypresses (not through windows)
NO
Requirements
Windows NT/2000 or later: Requires Windows NT 4.0 SP3 or later.
Windows 95/98/Me: Requires Windows 98 or later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
# 5 Re: simulating keypresses (not through windows)
I found a way to do this without talking to windows which means you can fool any program that wants to read the status of the keyboard. I searched this forum and there apeared to be lots of post on this topic. I tryed to post in the FAQ for visual c++ forum but its note there.
Good thing there is a back button on the browser. I copyed an pasted everything that I wrote and hopefully this will help others.
I think this should go in the FAQ section
/////////////////////////////////////////////
Its about time someone found out how to do this. This is very simple to do before continuing I suggest you know how to access to ports and scancodes (atleast the key that you want to simulate).
I will be using the function _outp and _inp other compilers the name of these functions can vary e.g. outportb.
Now to the simulating part first you must tell the keyboard that you want to simulate a key press or a key release. How first lets declare a variable
unsigned char scankey;
the first 6bits of the "scankey" variable is the scancode of the key. the final bit determins if the key is released or is it pressed. If bit7 is 1 key is released else its pressed. Lets take the key "a" for an example;
scankey = 30; // << that is the scan code for "a"
now we want to simulate released this key or pressing
for releaseing
scankey = scankey | (1<<7);
will set bit7 to 1 for release. Nothing is done to simulate key press. Now we communicate
_outp(0x64, 0xD2)
that tells the keyboard we are going to simulate a key event.
_outp(0x60, scankey)
that tells the scancode of the key and to simulate key press or release. Now an interupt 9 is generated and every program will believe that a key is pressed hahahaha.
TO read the curently pressed key (or simulated pressed key)/released just do
_inp(0x60)
and that returns the scancode in the same format as we send to keyboard (bit7 determines if key is pressed or not)
On windowsNT if you don't know how to do this you can alway's use the inpout.dll to comunicate. The inpout dll asks windows permision see its source for how its done.
//////////////////////////////////////////////
answer at 2007-11-11 1:29:50 >

# 6 Re: simulating keypresses (not through windows)
I am creating an application where I take sensor data from the legs and use it to make a character walk forward, to the side, etc. I am trying to get my application to output keystrokes so I can test it on video games.
I tried your code, but when I get to "_outp(0x64, 0xD2) ", I get an unhandled exception 0xC0000096 Privileged Instruction error. I am using WindowsXP (Home). Any idea what the problem is?
# 7 Re: simulating keypresses (not through windows)
Yes, according to my copy of the MSDN, this function is only compatible with Win9x.
I'm guessing that this function directly access ports on the computer. If so, then this is no longer possible with Windows XP (as it was not possible with WinNT/2K).
Viggy