Click to See Complete Forum and Search --> : Send the ALT Key
RajaCode
April 1st, 2008, 03:36 AM
I would like to write a programming that mimics someone pushing and holding the ALT key on their keyboard. I would like to write a console program for this. The reason is that this will be used for another program so I need to have it so that when the other application has loaded (its a game) that the ALT key is pushed down. Would this be possible?
I have written some pseudocode for what I think might work but I am not sure if this is the right approach:
do
flag = true //sets an invinate loop
send alt key command to windows
printf "the program has loaded"
printf "launch the game to see the results!"
while flag = true continue
basically i think all i need is a while loop that is contantly "sending" the ALT key to the computer. Would this be correct? If so how would I actually code this?
Thanks
GNiewerth
April 1st, 2008, 05:05 AM
Please describe exactly what you are trying to do. Do you want to simulate pressing ALT on the keyboard? Do you want to keep ALT pressed down, or do you want to simulate multiple ALT presses?
Which OS are you using? The Win32 SDK offers a function called SendInput, which creates mouse and keyboard event, though they are handled by the active application only. Another way is to obtain the game´s window handle and send appropriate WM_KEYDOWN/WM_KEY events.
RajaCode
April 1st, 2008, 10:54 AM
Please describe exactly what you are trying to do. Do you want to simulate pressing ALT on the keyboard? Do you want to keep ALT pressed down, or do you want to simulate multiple ALT presses?
Which OS are you using? The Win32 SDK offers a function called SendInput, which creates mouse and keyboard event, though they are handled by the active application only. Another way is to obtain the game´s window handle and send appropriate WM_KEYDOWN/WM_KEY events.
The ALT key pressed down. Can this be done in just C and not C++? (this is not too important i am just curious to see if this can be done in C).
Mercury048
April 1st, 2008, 11:19 AM
You do not need to send the ALT key "constantly".
The way keyboards work (simplified version) is that the OS sees a scancode for the "keydown" event (when the user presses a key). This scancode is repeated at a constant rate as long as the key is held down, but this is not necessary to make the OS think the key is down. When the user releases the key the OS sees a "keyup" scancode for that key and only then does it consider they key to be released.
So what you want to do is send an ALT keydown scancode (this can be done via SendInput or the - deprecated, but simpler - keybd_event functions in the Win32 API) to make the OS (and hence your program) think the ALT key is down. Then just send the keyup scancode when you're done.
Caveats:
If you actually press the real ALT key on the keyboard it will generate a keydown and keyup scancode so the ALT will be released.
If the program in question does something really weird with the keyboard (e.g. tracks the state of the ALT key independently of the OS) then the above may not work as expected.
ETA: Yes, this can be done in plain C.
RajaCode
April 1st, 2008, 12:34 PM
You do not need to send the ALT key "constantly".
The way keyboards work (simplified version) is that the OS sees a scancode for the "keydown" event (when the user presses a key). This scancode is repeated at a constant rate as long as the key is held down, but this is not necessary to make the OS think the key is down. When the user releases the key the OS sees a "keyup" scancode for that key and only then does it consider they key to be released.
So what you want to do is send an ALT keydown scancode (this can be done via SendInput or the - deprecated, but simpler - keybd_event functions in the Win32 API) to make the OS (and hence your program) think the ALT key is down. Then just send the keyup scancode when you're done.
Caveats:
If you actually press the real ALT key on the keyboard it will generate a keydown and keyup scancode so the ALT will be released.
If the program in question does something really weird with the keyboard (e.g. tracks the state of the ALT key independently of the OS) then the above may not work as expected.
ETA: Yes, this can be done in plain C.
I have no idea how to code this in C. How can this be done? Is my original idea of looping correct?
Mercury048
April 1st, 2008, 02:02 PM
That depends. You need to provide more information.
How long does the program run?
Does the user need to use the keyboard while it is running?
How can you tell when the program is over?
RajaCode
April 2nd, 2008, 09:33 AM
That depends. You need to provide more information.
How long does the program run?
Does the user need to use the keyboard while it is running?
How can you tell when the program is over?
Well the program should just continuously run. The user will be in a different application and will need to use the keyboard for that application. The user would just close the console to exit the program. We dont need a option in the program to close the application the user would just close the window.
I am actually designing this for a game - dota allstars.
So the users (gamers) will be playing the game while this handy little application will run in the background giving the users the ability to see the all the characters life meters without having to press and hold the ALT key. This program will allow the gamers not to lose vital concentration when making kills.
Mercury048
April 2nd, 2008, 02:51 PM
Well, the best way to learn something like this is by doing. I suggest you read up on SendInput and start coding and see what works. If you run into roadblocks then come back here and explain the problem.
A couple of issues to consider:
-Hold down the ALT key and try clicking on some menus in any standard Windows app. This is the effect you'll be getting with you "loop" strategy.
-You must have your application release the ALT key (by sending a keyup scancode) when it finishes or you must depend on the user to hit the ALT key manually to release it.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.