Love this site? Hate it? Leave us some comments.

Beginner C/C++

Moderators: Lundin
Number of threads: 4744
Number of posts: 15496

This Forum Only
Post New Thread

Report
Simulating Keyboard presses in games. Posted by ColacX on 24 Nov 2007 at 11:27 PM
Hey I need some help. Using Borland C++Builder 6

I just programmed a sort of autotyper. I tell the program what to type then I activate it and it'll start typing that.

My problem is that the program works fine for example notepad or msword but doesn't work when playing some games.

Currently I'm using the code
keybd_event(90,0,0,0);


My program was made to simulate keypresses so it should be exactly the same as pressing the keyboard. The game responds to presses on the keyboard but not to my program, why?

Is there a diffrence between using "keybd_event(90,0,0,0);" and actually pressing a key on the keyboard?

Im trying to simulate the letter "z" value equals 90

Also I have a Saitek P990 joypad plugged in that can "simulate" keypresses. Why can't my program do the same?
Report
Re: Simulating Keyboard presses in games. Posted by zibadian on 25 Nov 2007 at 1:25 AM
: Hey I need some help. Using Borland C++Builder 6
:
: I just programmed a sort of autotyper. I tell the program what to
: type then I activate it and it'll start typing that.
:
: My problem is that the program works fine for example notepad or
: msword but doesn't work when playing some games.
:
: Currently I'm using the code
:
: keybd_event(90,0,0,0);
:
:
: My program was made to simulate keypresses so it should be exactly
: the same as pressing the keyboard. The game responds to presses on
: the keyboard but not to my program, why?
:
: Is there a diffrence between using "keybd_event(90,0,0,0);" and
: actually pressing a key on the keyboard?
:
: Im trying to simulate the letter "z" value equals 90
:
: Also I have a Saitek P990 joypad plugged in that can "simulate"
: keypresses. Why can't my program do the same?

There are 3 different kinds of keyboard events: key_down, key_up, and key_pressed. Most games use the first 2 to store the keyboard state and then allow the game loop to perform the necessary actions. This is because then they can detect how long you're holding the key instead of just knowing that you pressed it. The keybd_event() might generate the last 1 (ie key_pressed). Since games don't necessarily watch for that event, it goes by unnoticed.
Report
Re: Simulating Keyboard presses in games. Posted by ColacX on 25 Nov 2007 at 6:57 AM
: There are 3 different kinds of keyboard events: key_down, key_up,
: and key_pressed. Most games use the first 2 to store the keyboard
: state and then allow the game loop to perform the necessary actions.
: This is because then they can detect how long you're holding the key
: instead of just knowing that you pressed it. The keybd_event() might
: generate the last 1 (ie key_pressed). Since games don't necessarily
: watch for that event, it goes by unnoticed.

I changed the code to
keybd_event(90,0,0,0);
keybd_event(90,0,2,0);
keybd_event(90,0,1,0);

The game did notice the first two 'z' but then not. There is a possibility that the game is blocking programs like mine, but if my program is simulating a keystroke then there shouldn't be anyway it could notice the diffrence.

Thanks for your help

I googled a bit.
"keybd_event Function
Windows NT/2000/XP/Vista:This function has been superseded. Use SendInput instead." Does that mean anything? Heres a link.

http://msdn2.microsoft.com/en-us/library/ms646310.aspx
Report
Re: Simulating Keyboard presses in games. Posted by ColacX on 27 Nov 2007 at 4:46 AM
How would I program it so that there is absolutly no diffrence from actually pressing the "z" key?

If ya think about how windows interprets a key press what message do my keyboard actually send when I press a key?

Please help.



 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.